<template>
  <div class="add-range flex flex-col h-full">
    <SearchForm :fields="fields" :params="params"></SearchForm>

    <div class="flex-1 page-wrap">
      <div class="btn-group">
        <t-button
          theme="success"
          :disabled="!selectedRowKeys.length"
          @click="handlerBatchBind"
          >批量划定</t-button
        >
      </div>
      <t-table
        size="small"
        row-key="id"
        :columns="columns"
        :data="tableData"
        bordered
        :pagination="{
          defaultCurrent: 1,
          defaultPageSize: 10,
          onChange,
          current: pagination.page,
        }"
        v-loading="tableLoading"
        :selected-row-keys="selectedRowKeys"
        select-on-row-click
        @select-change="selectChange"
      >
        <template #type="{ col, row }">
          {{ customerTypeFilter(row[col.colKey]) }}
        </template>
        <template #begin-time="{ col, row }">
          {{ timestampFilter(row[col.colKey]) }}
        </template>
        <template #exam-start-time="{ col, row }">
          {{ timestampFilter(row[col.colKey]) }}
        </template>
        <template #exam-end-time="{ col, row }">
          {{ timestampFilter(row[col.colKey]) }}
        </template>
        <template #create-time="{ col, row }">
          {{ timestampFilter(row[col.colKey]) }}
        </template>
      </t-table>
    </div>

    <!-- SelectServiceUnitDialog -->
    <select-service-unit-dialog
      v-model:visible="showSelectServiceUnitDialog"
      :crm-ids="selectedRowKeys"
      @success="search"
    ></select-service-unit-dialog>
  </div>
</template>

<script setup name="AddRange">
import { reactive, ref } from 'vue';
import { omit } from 'lodash';
import { MessagePlugin } from 'tdesign-vue-next';
import { serviceScopeUnbindCrmQueryApi } from '@/api/service-unit';
import useFetchTable from '@/hooks/useFetchTable';
import { CUSTOMER_TYPE } from '@/config/constants';
import { customerTypeFilter, timestampFilter } from '@/utils/filter';
import SelectServiceUnitDialog from './select-service-unit-dialog.vue';

let showSelectServiceUnitDialog = ref(false);

const fields = ref([
  {
    prop: 'crmUserId',
    label: '派单人',
    type: 'select',
    labelWidth: 80,
    colSpan: 5,
    attrs: {
      clearable: true,
    },
  },
  {
    prop: 'productType',
    label: '客户类型',
    type: 'select',
    labelWidth: 80,
    colSpan: 5,
    attrs: {
      clearable: true,
    },
  },
  {
    prop: 'customName',
    label: '客户名称',
    labelWidth: 80,
    colSpan: 5,
    options: dictToOptionList(CUSTOMER_TYPE),
    attrs: {
      clearable: true,
    },
  },
  {
    prop: 'crmNo',
    label: '项目单号',
    labelWidth: 80,
    colSpan: 5,
    attrs: {
      clearable: true,
    },
  },
  {
    type: 'buttons',
    colSpan: 2,
    children: [
      {
        type: 'button',
        text: '查询',
        onClick: () => {
          search();
        },
      },
    ],
  },
  {
    prop: 'crmTime',
    label: '派单时间',
    type: 'daterange',
    labelWidth: 80,
    colSpan: 10,
    attrs: {
      clearable: true,
    },
  },
]);
const params = reactive({
  crmUserId: '',
  productType: '',
  customName: '',
  crmNo: '',
  crmTime: [],
});
const computedParams = computed(() => {
  let data = omit(params, ['crmTime']);
  data.startTime = params.crmTime[0];
  data.endTime = params.crmTime[1];
  return data;
});

const selectedRowKeys = ref([]);
const selectChange = (value) => {
  selectedRowKeys.value = value;
};
const columns = [
  {
    colKey: 'row-select',
    type: 'multiple',
    width: 50,
    fixed: 'left',
  },
  { colKey: 'crmNo', title: '项目单号', minWidth: 80 },
  { colKey: 'beginTime', title: '派单时间', width: 170, cell: 'begin-time' },
  { colKey: 'crmUserName', title: '派单人', minWidth: 80 },
  { colKey: 'productType', title: '客户类型', width: 90, cell: 'type' },
  { colKey: 'customName', title: '客户名称', minWidth: 100 },
  { colKey: 'productName', title: '项目名称', minWidth: 80 },
  { colKey: 'productName', title: '实施产品', minWidth: 80 },
  {
    colKey: 'examStartTime',
    title: '考试开始时间',
    width: 170,
    cell: 'exam-start-time',
  },
  {
    colKey: 'examEndTime',
    title: '考试结束时间',
    width: 170,
    cell: 'exam-end-time',
  },
  { colKey: 'creatorName', title: '提交人', minWidth: 80 },
  { colKey: 'createTime', title: '提交时间', width: 170, cell: 'create-time' },
];
const {
  loading: tableLoading,
  pagination,
  tableData,
  search,
  onChange,
} = useFetchTable(serviceScopeUnbindCrmQueryApi, {
  fetchDataHandle: () => {
    selectedRowKeys.value = [];
  },
  params: computedParams,
});

const handlerBatchBind = () => {
  if (!selectedRowKeys.value.length) {
    MessagePlugin.error('请选择要划定的记录');
    return;
  }
  showSelectServiceUnitDialog.value = true;
};
</script>
