<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!-- 行业分类 -->
      <template #form-suspect="{ model, field }">
        <a-select
          v-model:value="model[field]"
          placeholder="请选择行业分类"
          :fieldNames="{ label: 'text', value: 'value' }"
          :options="isSuspectOptions"
          @change="changeSuspect"
        ></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>
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" @click="skip" v-auth="'zjgl-zj-jbxx'">查看基本信息</a-button>
        <a-button type="primary" @click="handle" v-auth="'zjgl-zj-gryj'">个人意见</a-button>
      </template>
    </BasicTable>
    <OpinionsModel @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="dkgl-investigate-Filling" setup>
  import { BasicTable } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import OpinionsModel from './model/opinions.vue';
  import { searchFormSchema, useSuspectsData, useCategoryData } from '../../dkgl/investigate/data';
  import { columns } from './data';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { list } from './api';
  import { useRouter } from 'vue-router';

  const [registerModal, { openModal }] = useModal();
  let router = useRouter();

  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '地块信息',
      api: list,
      columns,
      canResize: false,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'id',
      clickToRowSelect: true,
      showActionColumn: false,
      rowSelection: { type: 'radio' },
      formConfig: {
        labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: false,
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
    },
  });
  const isSuspectOptions = useSuspectsData().data; // 行业分类
  const useCategory = useCategoryData();
  const treeData = useCategory.data; // 行业类别-数据
  const changeSuspect = useCategory.changeIsSuspect; // 行业类别-方法

  const [registerTable, { reload, updateTableDataRecord }, { rowSelection, selectedRows, selectedRowKeys }] = tableContext;
  const { createMessage } = useMessage();
  const skip = () => {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (selectedRows.value[0].scjdbm == 'S2' || selectedRows.value[0].scjdbm == 'S3') {
      router.push(`/zjgl/expert/ViewDetail?wrdkid=${selectedRows.value[0].wrdkid}&scjdbm=${selectedRows.value[0].scjdbm}&dksdzt=1`);
    } else {
      router.push(
        `/zjgl/expert/faList?wrdkid=${selectedRowKeys.value[0]}&scjdbm=${selectedRows.value[0].scjdbm}&dksdzt=1&jdnzt=${selectedRows.value[0].jdnzt}`
      );
    }
  };
  // 点击个人意见
  function handle() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    openModal(true, {
      record: selectedRows.value[0],
      isUpdate: true,
      showFooter: true,
    });
  }
  function handleSuccess({ isUpdate, values }) {
    isUpdate ? updateTableDataRecord(values.id, values) : reload();
  }
</script>
<style scoped></style>
