<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection" @selection-change="rowClick">
      <template #bodyCell="{ column, record, text }">
        <template v-if="column.dataIndex === 'wrdkmc'">
          <a @click="skip(record)">{{ text }}</a>
        </template>
      </template>
      <template #tableTitle>
        <a-button type="primary" @click="handle('czwc')" v-auth="'trwy-czwcbs-jswc'">处置完成填报</a-button>
        <a-button type="primary" @click="handle('dgszbm')" v-auth="'trwy-czwcbs-dgszbm'" :disabled="szdshow">地块所在地部门确认</a-button>
        <a-button type="primary" @click="handle('jsdbm')" v-auth="'trwy-czwcbs-jsdbm'" :disabled="jsshow">接收地部门确认</a-button>
      </template>
    </BasicTable>
    <!--接收完成填报 -->
    <czfillingModal @register="registerModal" @success="handleSuccess" />
    <!--部门确认 -->
    <depconfirmationModal @register="registerbmModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="dkgl-investigate-Acceptance" setup>
  import { ref } from 'vue';
  import { BasicTable } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { CZwccolumns } from './data';
  import czfillingModal from './modal/czfillingModal.vue';
  import depconfirmationModal from './modal/depconfirmationModal.vue';
  import { zyjhCzwcList } from './api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import { useUserStore } from '/@/store/modules/user';

  const { createMessage } = useMessage();

  const [registerModal, { openModal }] = useModal();
  const [registerbmModal, { openModal: openbmModal }] = useModal();

  const userStore: any = useUserStore();
  const szdshow = ref(true);
  const jsshow = ref(true);
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '重点监管地块',
      api: zyjhCzwcList,
      columns: CZwccolumns,
      canResize: false,
      //定义rowSelection的类型，默认是checkbox多选，可以设置成radio单选
      rowKey: 'id',
      clickToRowSelect: true,
      showActionColumn: false,
      rowSelection: { type: 'radio' },
      useSearchForm: false,

      actionColumn: {
        width: 120,
      },
    },
  });
  const [registerTable, { reload }, { rowSelection, selectedRowKeys, selectedRows }] = tableContext;
  function handle(v) {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    const obj = { ...selectedRows.value[0] };
    if (v == 'czwc') {
      // 需要加个判断判断
      let flg = true;
      if (obj.flag == 1) {
        flg = false;
      }
      openModal(true, {
        record: obj,
        isUpdate: true,
        showFooter: flg,
      });
    } else if (v == 'dgszbm') {
      obj.type = '2';
      obj.zglx = 'szd';
      openbmModal(true, {
        record: obj,
        isUpdate: true,
        showFooter: true,
        title: '地块所在部门确认',
      });
    } else if (v == 'jsdbm') {
      obj.type = '2';
      obj.zglx = 'jsd';
      openbmModal(true, {
        record: obj,
        isUpdate: true,
        showFooter: true,
        title: '接收地部门确认',
      });
    }
  }
  function rowClick({ rows }) {
    if (rows[0]) {
      // flag=1代表提交的
      if (rows[0].flag == 1) {
        // 所在地部门未确认且当前用户是该所在地部门用户
        if (rows[0].szdFlag != 1 && rows[0].szdzgbm == userStore.userInfo.username) {
          szdshow.value = false;
        } else {
          szdshow.value = true;
        }
        // 接收地部门未确认且当前用户是该接收部门用户
        if (rows[0].jsdFlag != 1 && rows[0].jsdzgbm == userStore.userInfo.username) {
          jsshow.value = false;
        } else {
          jsshow.value = true;
        }
      } else {
        // flag=2接收转运处置
        szdshow.value = true;
        jsshow.value = true;
      }
    }
  }
  function skip(record) {
    openModal(true, {
      record: record,
      isUpdate: true,
      showFooter: false,
      isDetail: true,
    });
  }
  function handleSuccess() {
    reload();
    selectedRowKeys.value = [];
    selectedRows.value = [];
  }
</script>
<style scoped></style>
