<template>
  <PageWrapper contentBackground>
    <template #footer>
      <a-tabs v-model:activeKey="tableType" @change="changeTable">
        <a-tab-pane :key="0" :tab="t('routes.spares.tableType.10')" />
        <a-tab-pane :key="1" :tab="t('routes.spares.tableType.21')" />
        <a-tab-pane :key="2" :tab="t('routes.spares.tableType.22')" /> </a-tabs
    ></template>
    <div class="desc-wrap-BasicTable">
      <BasicTable @register="registerTable">
        <template #action="{ record }">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '详情',
                ifShow: getCheckPerm('work_detail'),
                onClick: handleDetail.bind(null, record),
              },
              {
                label: '添加备件',
                ifShow: getCheckPerm('maintenance_add') && tableType == 1 && record.status == 60,
                onClick: handleAdd.bind(null, record),
              },
              {
                label: '完成维修',
                ifShow: getCheckPerm('maintenance_finish') && tableType == 1 && record.status == 60,
                onClick: handleOut.bind(null, record),
              },
              {
                label: '检测登记',
                ifShow: getCheckPerm('maintenance_testing') && tableType == 0,
                onClick: handleRecover.bind(null, record),
              },
              {
                label: '修改定损',
                ifShow: getCheckPerm('maintenance_testing') && tableType == 1 && record.status == 20,
                onClick: handleRecover.bind(null, record),
              },
            ]"
          />
        </template>
      </BasicTable>
      <checkModel @update="reload" @register="registerRecovery" />
      <addAccessoryModel @update="reload" @register="registerAdd" />
      <outModal @update="reload" @register="registerOut" />

      <!-- 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 checkModel from './checkModel.vue';
  import outModal from './outModal.vue';
  import addAccessoryModel from './addAccessoryModel.vue';
  import { useModal } from '/@/components/Modal';
  import { useRouter } from 'vue-router';
  import { saleOrderList, repairOrderList } from '/@/api/spares';
  export default defineComponent({
    name: '维修管理',
    components: {
      BasicTable,
      TableAction,
      TableImg,
      checkModel,
      outModal,
      PageWrapper,
      addAccessoryModel,
      [Tabs.name]: Tabs,
      [Tabs.TabPane.name]: Tabs.TabPane,
    },
    setup() {
      const { t } = useI18n();
      const permissionStore = usePermissionStore();
      const router = useRouter();
      const { getCheckPerm } = permissionStore;
      const tableType = ref<number>(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,
        },
        {
          title: '产品类型',
          dataIndex: 'cameraType',
          width: 80,
          customRender: ({ record }) => {
            return t(`routes.scene.tableType.${record.cameraType}`);
          },
        },
        {
          title: '产品SN码',
          dataIndex: 'cameraSnCode',
          width: 100,
        },
        {
          title: '故障描述',
          dataIndex: 'faultMsg',
          width: 100,
        },
        {
          title: '售后工程师',
          dataIndex: 'saleName',
          width: 100,
        },
        {
          title: '接单日期',
          dataIndex: 'orderReceivingTime',
          width: 100,
        },
        {
          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: 200,
        },
      ];
      const searchForm: Partial<FormProps> = {
        labelWidth: 120,
        schemas: [
          {
            field: 'cameraType',
            label: '设备类型',
            component: 'Select',
            componentProps: {
              options: [
                {
                  label: t('routes.scene.tableType.0'),
                  value: '0',
                },
                {
                  label: t('routes.scene.tableType.1'),
                  value: '1',
                },
                {
                  label: t('routes.scene.tableType.2'),
                  value: '2',
                },
              ],
            },
            colProps: {
              xl: 6,
              xxl: 6,
            },
          },
          {
            field: 'cameraSnCode',
            component: 'Input',
            label: t('routes.device.snCode'),
            colProps: {
              xl: 7,
              xxl: 7,
            },
          },
          {
            field: 'repairId',
            component: 'Input',
            label: '工单号',
            colProps: {
              xl: 7,
              xxl: 7,
            },
          },
        ],
      };

      const [registerRecovery, { openModal }] = useModal();
      const [registerOut, { openModal: openOutModal }] = useModal();
      const [registerAdd, { openModal: openAddModal }] = useModal();
      const [registerTable, { reload }] = useTable({
        api: repairOrderList,
        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, record);
      }
      function handleAdd(record: Recordable) {
        openAddModal(true, record);
      }
      function handleOut(record: Recordable) {
        openOutModal(true, record);
      }
      function handleOrder() {
        openModal(true);
      }
      function changeTable(val: string) {
        tableType.value = val;
        reload();
      }
      return {
        registerTable,
        reload,
        t,
        tableType,
        openAddModal,
        registerOut,
        registerAdd,
        changeTable,
        handleOrder,
        getCheckPerm,
        handleDetail,
        handleAdd,
        handleOut,
        handleRecover,
        registerRecovery,
      };
    },
  });
</script>
<style lang="less" scoped>
  .desc-wrap-BasicTable {
    background-color: #f0f2f5;
    .vben-basic-table-form-container {
      padding: 0;
    }
  }
</style>
