<template>
  <PageWrapper contentBackground>
    <div class="desc-wrap-BasicTable">
      <BasicTable @register="registerTable">
        <template #toolbar>
          <a-button type="primary" v-if="getCheckPerm('device-connect')" @click="deviceLink"> 设备关联</a-button>
          <a-button type="primary" @click="back">返回</a-button>
        </template>
        <template #action="{ record }">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '解绑',
                ifShow:getCheckPerm('device-unbind') && Boolean(record.userName),
                color: 'error',
                onClick: handleUnbind.bind(null, record),
              },
            ]"
          />
        </template>
      </BasicTable>
    </div>
    <DeviceLinkModal @register="registerLinkModal" />
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, h, reactive, toRefs, onMounted } from 'vue';
  import { isBoolean } from 'lodash';
  import {
    BasicTable,
    useTable,
    TableAction,
    BasicColumn,
    TableImg,
    FormProps,
  } from '/@/components/Table';
  import { usePermissionStore } from '/@/store/modules/permission';
  import { PageWrapper } from '/@/components/Page';
  import { Descriptions } from 'ant-design-vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { cameraList } from '/@/api/customer';
  import DeviceLinkModal from './modal/DeviceLinkModal.vue';
  import { useModal } from '/@/components/Modal';
  import { useRouter } from 'vue-router';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { UnbindCameraApi } from '/@/api/account';
  import { isBoolean } from 'lodash';
  const { t } = useI18n();
  export default defineComponent({
    components: {
      BasicTable,
      TableAction,
      PageWrapper,
      TableImg,
      DeviceLinkModal,
      [Descriptions.name]: Descriptions,
      [Descriptions.Item.name]: Descriptions.Item,
    },
    setup() {
      const { createMessage, createConfirm } = useMessage();
      const [registerLinkModal, { openModal: openLinkModal }] = useModal();
      const router = useRouter();
      const companyId: Number = router.currentRoute.value.params.id - 0;
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      onMounted(() => {
        // console.log(router.currentRoute.value.params.id);
      });
      const columns: BasicColumn[] = [
        {
          title: 'SN码',
          dataIndex: 'snCode',
          width: 180,
        },
        {
          title: 'wifi名称',
          dataIndex: 'wifiName',
          width: 150,
        },
        {
          title: '设备类型',
          dataIndex: 'type',
          ellipsis: false,
          width: 80,
          customRender: ({ record }) => {
            let typeObj ={
              // '0':'旧双目相机',
              // '1':'四维看看',
              // '2':'四维看看lite',
              '9':'四维看见',
              // '10':'四维深时',
            }
            return typeObj[record.type]
          }
        },

        {
          title: '激活时间',
          dataIndex: 'activatedTime',
          width: 180,
        },
        {
          title: '出库类型',
          dataIndex: 'outType',
          width: 80,
          customRender: ({ record }) => {
            let typeObj ={
              '0':t('routes.product.outType.0'),
              '4':t('routes.product.outType.4'),
              '1':t('routes.product.outType.1'),
              '2':t('routes.product.outType.2'),
              '3':t('routes.product.outType.3'),
            }
            return typeObj[record.outType]
          }
        },
        {
          title: '客户名称',
          dataIndex: 'companyName',
          width: 80,
        },
        {
          title: '绑定账号',
          dataIndex: 'userName',
          width: 180,
        },

        {
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          ifShow: true,
          fixed: 'right',
          flag: 'ACTION',
          width: 60,
        },
      ];
      const searchForm: Partial<FormProps> = {
        labelWidth: 100,
        schemas: [
          {
            field: 'snCode',
            label: 'SN码',
            component: 'Input',
            componentProps: {
              maxLength: 100,
            },
            colProps: {
              xl: 4,
              xxl: 4,
            },
          },
          {
            field: 'type',
            label: '设备类型',
            component: 'Select',
            componentProps: {
              maxLength: 100,
              options: [
                // {
                //   label: '旧双目相机',
                //   value: '0',
                // },
                // {
                //   label: '四维看看',
                //   value: '1',
                // },
                // {
                //   label: '四维看看lite',
                //   value: '2',
                // },
                {
                  label: '四维看见',
                  value: '9',
                },
                // {
                //   label: '四维深时',
                //   value: '10',
                // },
              ],
            },
            colProps: {
              xl: 4,
              xxl: 4,
            },
          },
          {
            field: 'activatedTime',
            label: '激活时间',
            component: 'RangePicker',
            componentProps: {
              maxLength: 100,
              valueFormat:'YYYY-MM-DD',
              format: 'YYYY-MM-DD',
            },
            colProps: {
              xl: 7,
              xxl: 7,
            },
          },
          {
            field: 'userName',
            label: '绑定账号',
            component: 'Input',
            componentProps: {
              maxLength: 100,
            },
            colProps: {
              xl: 4,
              xxl: 4,
            },
          },
        ],
      };
      const [registerTable, { reload }] = useTable({
        api: cameraList,
        // title: '四维深时场景列表',
        // titleHelpMessage: ['已启用expandRowByClick', '已启用stopButtonPropagation'],
        columns: columns,
        searchInfo: { companyId },
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        rowKey: 'id',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        canResize: false,
      });
      async function handleUnbind(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: '解绑后用户将看不到该相机拍摄的场景。<br/>确定解绑吗?',
          onOk: async () => {
            await UnbindCameraApi({cameraId:record.id})
            createMessage.success(t('common.optSuccess'));
            reload()
          },
        });
      }

      function deviceLink() {
        openLinkModal(true);
      }
      function back() {
        router.push('/customer/index');
      }
      return {
        registerTable,
        handleUnbind,
        deviceLink,
        registerLinkModal,
        back,
        getCheckPerm,
        isBoolean,
      };
    },
  });
</script>
<style lang="less" scoped>
  .desc-wrap-BasicTable {
    background-color: #f0f2f5;
    .vben-basic-table-form-container {
      padding: 0;
    }
  }
</style>
