<template>
  <BasicTable @register="registerTable">
    <template #form-userLevle="{ model, field }">
      <a-select
        v-model:value="model[field]"
        placeholder="请选择"
        @change="change(model)"
        :fieldNames="{ label: 'text', value: 'value' }"
        :options="Options"
      ></a-select>
    </template>
    <template #form-category="{ model, field }">
      <a-tree-select
        v-model:value="model[field]"
        show-search
        placeholder="请选择"
        allow-clear
        tree-default-expand-all
        :field-names="{ label: 'name', value: 'id', children: 'children' }"
        :tree-data="treeData"
        tree-node-filter-prop="label"
      >
        <template #suffixIcon>
          <SmileOutlined />
        </template>
      </a-tree-select>
    </template>
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:export-outlined" v-auth="'ywkz-wrdkjd-dc'" @click="onExportXls"> 导出</a-button>
    </template>
  </BasicTable>
</template>
<script setup lang="ts">
  import { BasicTable } from '@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';

  import { WrdkColumns, DKdcsearchFormSchema, useCategoryData, getUserLevel } from './data';
  import { wrdkgjdhzqktj, wrdkgjdhzqktjExcel } from './api';

  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '',
      api: wrdkgjdhzqktj,
      columns: WrdkColumns,
      canResize: false,
      beforeFetch(params: any) {
        if (params.hylb) {
          if (params.hylb.length == 1) {
            params.hyml = params.hylb;
          } else if (params.hylb.length == 2) {
            params.hydl = params.hylb;
          } else if (params.hylb.length == 3) {
            params.hyzl = params.hylb;
          } else if (params.hylb.length == 4) {
            params.hyxl = params.hylb;
          }
        }
        if (params.data) {
          const arry = params.data.split(',');
          params.startTime = arry[0];
          params.endTime = arry[1];
        }
        let res = [...WrdkColumns];
        if (params.level == 2) {
          res.splice(1, 0, {
            title: '县',
            dataIndex: 'countryName',
            resizable: true,
          });
          setColumns(res);
        } else {
          setColumns(res);
        }
      },
      rowSelection: { type: 'radio' },
      rowKey: 'wrdkid',
      clickToRowSelect: true,
      showActionColumn: false,

      formConfig: {
        labelWidth: 120,
        schemas: DKdcsearchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: '污染地块各阶段汇总情况统计',
      url: wrdkgjdhzqktjExcel,
    },
  });
  const [registerTable, { setColumns }, {}] = tableContext;
  const useCategory = useCategoryData();
  const treeData = useCategory.data; // 行业类别-数据
  const Options = getUserLevel().data;
  function change(model) {
    model.cityCode = '';
  }
</script>
