<template>
  <div class="wait-check">
    <SearchForm :fields="fields" :params="params" showAll :search="search">
      <template #service="{ item, params }">
        <select-service-unit
          v-model="params[item.prop]"
          :filterParams="{ statusList: ['PUBLISH'] }"
        ></select-service-unit>
      </template>
      <template #supplier="{ item, params }">
        <select-supplier v-model="params[item.prop]"> </select-supplier>
      </template>
    </SearchForm>
    <div class="flex-1 page-wrap">
      <t-table
        size="small"
        row-key="id"
        :columns="columns"
        :data="tableData"
        bordered
        :pagination="{
          defaultCurrent: 1,
          defaultPageSize: 10,
          onChange,
          showJumper: true,
          showPageSize: false,
          total: pagination.total,
          current: pagination.pageNumber,
        }"
      >
        <template #code="{ col, row }">
          <more-content
            :content="row[col.colKey]"
            @action="handleDetail(row)"
          ></more-content>
        </template>
        <template #backup-time="{ col, row }">
          {{ timestampFilter(row[col.colKey]) }}
        </template>
        <template #apply-time="{ col, row }">
          {{ timestampFilter(row[col.colKey]) }}
        </template>
        <template #approve-time="{ col, row }">
          {{ timestampFilter(row[col.colKey]) }}
        </template>
        <template #custom-type="{ col, row }">
          {{ customerTypeFilter(row[col.colKey]) }}
        </template>
        <template #attachmentPaths="{ col, row }">
          <attachment-view
            :imageList="getUrls(row[col.colKey], 'image')"
            :fileList="getUrls(row[col.colKey], 'file')"
            :imgSize="60"
          ></attachment-view>
        </template>
      </t-table>
    </div>

    <!-- AbnormalDetailDialog -->
    <abnormal-detail-dialog
      v-model:visible="showAbnormalDetailDialog"
      :row="curRow"
    ></abnormal-detail-dialog>
  </div>
</template>

<script setup name="DoneCheck">
import { reactive, ref, computed } from 'vue';
import { omit } from 'lodash';

import useFetchTable from '@/hooks/useFetchTable';
import { workHoursDoneCheckListApi } from '@/api/work-hours';
import { timestampFilter, customerTypeFilter } from '@/utils/filter';
import { AUDITING_RESULT } from '@/config/constants';
import { dictToOptionList } from '@/utils/tool';
import AbnormalDetailDialog from './abnormal-detail-dialog.vue';

const fields = ref([
  {
    prop: 'serviceId',
    label: '服务单元',
    type: 'select',
    labelWidth: 70,
    colSpan: 6,
    cell: 'service',
  },
  {
    prop: 'name',
    label: '姓名',
    labelWidth: 80,
    colSpan: 6,
    attrs: {
      clearable: true,
    },
  },
  {
    prop: 'supplierId',
    label: '所属供应商',
    type: 'select',
    labelWidth: 95,
    colSpan: 6,
    cell: 'supplier',
  },
  {
    prop: 'dingExceptionApprove',
    label: '审核结果',
    type: 'select',
    labelWidth: 80,
    colSpan: 6,
    options: dictToOptionList(AUDITING_RESULT),
    attrs: {
      clearable: true,
    },
  },
  {
    prop: 'customName',
    label: '客户名称',
    labelWidth: 70,
    colSpan: 6,
    attrs: {
      clearable: true,
    },
  },
  {
    prop: 'exceptionTime',
    label: '异常时间',
    type: 'daterange',
    labelWidth: 80,
    colSpan: 12,
    attrs: {
      clearable: true,
      valueType: 'time-stamp',
    },
  },
  {
    prop: 'applyTime',
    label: '申请时间',
    type: 'daterange',
    labelWidth: 70,
    colSpan: 12,
    attrs: {
      clearable: true,
      valueType: 'time-stamp',
    },
  },
  {
    type: 'buttons',
    colSpan: 3,
    children: [
      {
        type: 'button',
        text: '搜索',
        onClick: () => {
          search();
        },
      },
    ],
  },
]);
const params = reactive({
  serviceId: '',
  name: '',
  supplierId: '',
  dingExceptionApprove: '',
  customName: '',
  exceptionTime: [],
  applyTime: [],
});
const computedParams = computed(() => {
  let data = omit(params, ['exceptionTime', 'applyTime']);
  data.startTime = params.exceptionTime[0];
  data.endTime = params.exceptionTime[1];
  data.applyStartTime = params.applyTime[0];
  data.applyEndTime = params.applyTime[1];
  return data;
});

const getUrls = (data, type) => {
  const list = data ? data.split(',') : [];
  const imgs = ['jpg', 'png', 'jpeg'];
  if (type === 'image') {
    return list.filter((item) => {
      const evt = item.split('.').slice(-1)[0];
      return imgs.includes(evt);
    });
  } else {
    return list.filter((item) => {
      const evt = item.split('.').slice(-1)[0];
      return !imgs.includes(evt);
    });
  }
};

const columns = [
  { colKey: 'code', title: '异常编号', width: 200 },
  { colKey: 'serviceName', title: '服务单元', width: 140 },
  { colKey: 'sopNo', title: 'SOP流水号', width: 200 },
  { colKey: 'createRealName', title: '姓名', width: 140 },
  { colKey: 'supplierName', title: '供应商', width: 140 },
  { colKey: 'customName', title: '客户名称', width: 140 },
  { colKey: 'customType', title: '客户类型', cell: 'custom-type', width: 120 },
  { colKey: 'dingExceptionTypeStr', title: '异常类型', width: 120 },
  { colKey: 'exceptionTime', title: '异常日期', width: 140 },
  { colKey: 'applyTime', title: '补卡时间', cell: 'backup-time', width: 180 },
  { colKey: 'reason', title: '理由' },
  { colKey: 'attachmentPaths', title: '附件/截图', minWidth: 240 },
  { colKey: 'createTime', title: '申请时间', cell: 'apply-time', width: 180 },
  { colKey: 'statusStr', title: '审核状态', width: 120 },
  {
    colKey: 'dingExceptionApprove',
    title: '审核结果',
    width: 100,
  },
  { colKey: 'approveUsersName', title: '审核人', width: 140 },
  {
    colKey: 'approveTime',
    title: '审核时间',
    cell: 'approve-time',
    width: 180,
  },
];
const { pagination, tableData, search, onChange } = useFetchTable(
  workHoursDoneCheckListApi,
  { params: computedParams }
);

const showAbnormalDetailDialog = ref(false);
const curRow = ref(null);
const handleDetail = async (row) => {
  curRow.value = row;
  showAbnormalDetailDialog.value = true;
};
</script>
