<template>
  <PageWrapper contentBackground>
    <template #footer>
      <a-tabs v-model:activeKey="tableType" @change="changeTable">
        <a-tab-pane :key="0" tab="待核账" />
      </a-tabs>
    </template>
    <div class="desc-wrap-BasicTable">
      <BasicTable @register="registerTable">
        <template #action="{ record }">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '详情',
                //ifShow: getCheckPerm('U8Sending_detail'),
                onClick: handleDetail.bind(null, record),
              },
              {
                label: '到账登记',
                //ifShow: getCheckPerm('U8Sending_register') && tableType == 0,
                onClick: handleRecover.bind(null, record),
              },
            ]"
          />
        </template>
      </BasicTable>
      <partsListModal @update="reload" @register="registerRecovery" />
      <!-- ifShow: getCheckPerm('device-out') && !Boolean(record.outType), -->
    </div>
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref, onActivated } from 'vue';
  import { PageWrapper } from '/@/components/Page';
  import {
    BasicTable,
    useTable,
    TableAction,
    BasicColumn,
    // TableImg,
    FormProps,
  } from '/@/components/Table';
  import { Tabs } from 'ant-design-vue';
  import { operateSceneList } from '/@/api/operate';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { usePermissionStore } from '/@/store/modules/permission';
  import partsListModal from './ListModal.vue';
  import { useModal } from '/@/components/Modal';
  import { useRouter } from 'vue-router';
  import { checkAccountList } from '/@/api/spares';//getByRoleType
  export default defineComponent({
    name: '维修备件管理',
    components: {
      BasicTable,
      TableAction,
      // TableImg,
      partsListModal,
      PageWrapper,
      [Tabs.name]: Tabs,
      [Tabs.TabPane.name]: Tabs.TabPane,
    },
    setup() {
      const { t } = useI18n();
      const permissionStore = usePermissionStore();
      const router = useRouter();
      const { getCheckPerm } = permissionStore;
      const tableType = ref<Recordable>(0); //0看看 、1看见、2深时
      onMounted(() => {
        // console.log(router.currentRoute.value.params.id);
      });
      const columns: BasicColumn[] = [
        {
          title: '报修日期',
          dataIndex: 'createTime',
          width: 180,
        },
        {
          title: '客户名称',
          dataIndex: 'companyName',
          width: 80,
          // customRender: ({ record }) => {
          //   return t(`routes.equity.operation.${record.operationType || 0}`);
          // },
        },
        {
          title: '产品类型',
          dataIndex: 'cameraType',
          width: 80,
          customRender: ({ record }) => {
            return t(`routes.scene.tableType.${record.cameraType}`);
          },
        },
        {
          title: '产品SN码',
          dataIndex: 'cameraSnCode',
          width: 100,
        },
        {
          title: '接单日期',
          dataIndex: 'orderReceivingTime',
          width: 100,
        },
        {
          title: '维修工程师',
          dataIndex: 'repairManName',
          width: 100,
        },
        {
          title: '故障分析',
          dataIndex: 'checkResult',
          width: 150,
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: 100,
          customRender: ({ record }) => {
            return t(`routes.spares.tableType.${record.status || 0}`);
          },
        },
        {
          title: '工单号',
          dataIndex: 'repairId',
          width: 150,
        },
        {
          title: t('common.operating'),
          dataIndex: 'action',
          slots: { customRender: 'action' },
          ifShow: true,
          fixed: 'right',
          flag: 'ACTION',
          width: 120,
        },
      ];
      const searchForm: Partial<FormProps> = {
        labelWidth: 120,
        autoAdvancedLine: 1,
        actionColOptions: {
          span: 24,
        },
        schemas: [
          {
            field: 'cameraType',
            component: 'Select',
            label: '产品类型',
            colProps: {
              xl: 7,
              xxl: 7,
            },
            componentProps: {
              options: [
                {
                  label: t('routes.device.type.1'),
                  value: 0,
                  key: '0',
                },
                {
                  label: t('routes.device.type.2'),
                  value: 1,
                  key: '1',
                },
                {
                  label: t('routes.device.type.3'),
                  value: 2,
                  key: '2',
                },
              ],
            },
          },
          {
            field: 'cameraSnCode',
            component: 'Input',
            label: t('routes.device.snCode'),
            colProps: {
              xl: 7,
              xxl: 7,
            },
          },
          // {
          //   field: 'repairManId',
          //   label: '维修工程师',
          //   component: 'ApiSelect',
          //   componentProps: {
          //     api: getByRoleType,
          //     numberToString: true,
          //     labelField: 'nickName',
          //     valueField: 'id',
          //     immediate: true,
          //     params: {
          //       roleType: 3,
          //     },
          //   },
          //   colProps: {
          //     span: 7,
          //   },
          // },
          {
            field: 'repairId',
            component: 'Input',
            label: '工单号',
            colProps: {
              xl: 7,
              xxl: 7,
            },
          },
        ],
      };
      const [registerRecovery, { openModal }] = useModal();
      const [registerTable, { reload }] = useTable({
        api: checkAccountList,
        columns: columns,
        useSearchForm: true,
        searchInfo: { statusParam: tableType },
        formConfig: searchForm,
        showTableSetting: true,
        showIndexColumn: false,
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        canResize: false,
      });
      onActivated(() => {
        reload();
      });
      async function handleDetail(record: Recordable) {
        console.log('record', record);
        router.push({ path: `detail/${record.repairId || '20230215174919387'}` });
      }
      async function handleRecover(record: Recordable) {
        openModal(true, {
          tableType: tableType.value,
          ...record,
        });
      }
      function handleOrder() {
        openModal(true);
      }
      function changeTable(val: string) {
        tableType.value = val;
        reload();
      }
      return {
        registerTable,
        reload,
        t,
        tableType,
        changeTable,
        handleOrder,
        getCheckPerm,
        handleDetail,
        handleRecover,
        registerRecovery,
      };
    },
  });
</script>
<style lang="less" scoped>
  .desc-wrap-BasicTable {
    background-color: #f0f2f5;
    .vben-basic-table-form-container {
      padding: 0;
    }
  }
</style>
