<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 #tableTitle>
      <a-button type="primary" preIcon="ant-design:export-outlined" v-auth="'ywkz-scqydkytj-dc'" @click="onExportXls"> 导出</a-button>
    </template>
  </BasicTable>
</template>
<script setup lang="ts">
  import { BasicTable } from '@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { MonthColumns, MonthsearchFormSchema, getUserLevel } from './data';
  import { qyscdkyqktj, qyscdkyqktjExcel } from './api';

  const { createMessage } = useMessage();
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '',
      api: qyscdkyqktj,
      columns: MonthColumns,
      canResize: false,
      beforeFetch(params: any) {
        if (params.startMonth && params.endMonth) {
          if (new Date(params.startMonth) > new Date(params.endMonth)) {
            createMessage.error('开始月份不能大于结束月');
            return false;
          }
        }
        let res = [...MonthColumns];
        if (params.level == 2) {
          res.push({
            title: '县',
            dataIndex: 'countryName',
            resizable: true,
          });
          setColumns(res);
        } else {
          setColumns(res);
        }
      },
      afterFetch(params: any) {
        let columns = [
          {
            title: '市',
            dataIndex: 'cityName',
            resizable: true,
          },
        ];
        if (params[0].countryName) {
          columns.push({
            title: '县',
            dataIndex: 'countryName',
            resizable: true,
          });
        }
        Object.keys(params[0]).forEach((v) => {
          if (v != 'cityName' && v != 'ROW_ID' && v != 'countryCode' && v != 'cityCode' && v != 'countryName') {
            columns.push({
              title: v,
              dataIndex: v,
              resizable: true,
            });
          }
        });
        setColumns(columns);
      },
      rowSelection: { type: 'radio' },
      rowKey: 'wrdkid',
      clickToRowSelect: true,
      showActionColumn: false,

      formConfig: {
        labelWidth: 120,
        schemas: MonthsearchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: '区域删除地块月情况统计',
      url: qyscdkyqktjExcel,
    },
  });
  const [registerTable, { setColumns }, {}] = tableContext;

  const Options = getUserLevel().data;
  function change(model) {
    model.cityCode = '';
  }
</script>
