<template>
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <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" @click="view" v-auth="'ywkz-kgpsxx-view'"> 查看</a-button>
      <a-button type="primary" preIcon="ant-design:export-outlined" v-auth="'ywkz-kgpsxx-dc'" @click="onExportXls"> 导出</a-button>
    </template>
  </BasicTable>
  <!--查看详情 -->
  <Record @register="registerModal" @success="handleSuccess" />
</template>
<script setup lang="ts">
  import { BasicTable } from '@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import { DkpsColumns, DkpsFormSchema, useCategoryData, getUserLevel } from './data';
  import { dkpsxxtj, dkpsxxtjExcel } from './api';
  import Record from './model/Record.vue';

  const { createMessage } = useMessage();
  const { tableContext, onExportXls } = useListPage({
    tableProps: {
      title: '',
      api: dkpsxxtj,
      columns: DkpsColumns,
      canResize: false,

      rowSelection: { type: 'checkbox' },
      rowKey: 'wrdkid',
      clickToRowSelect: true,
      showActionColumn: false,

      formConfig: {
        labelWidth: 120,
        schemas: DkpsFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: '地块评审信息统计',
      url: dkpsxxtjExcel,
    },
  });
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload }, { rowSelection, selectedRows }] = tableContext;
  const useCategory = useCategoryData();
  const treeData = useCategory.data; // 行业类别-数据
  const Options = getUserLevel().data;
  function change(model) {
    model.cityCode = '';
  }
  function view() {
    if (selectedRows.value.length !== 1) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    openModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  function handleSuccess() {
    reload();
  }
</script>
