<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection" @selection-change="rowClick">
      <template #bodyCell="{ column, text, record }">
        <template v-if="column.dataIndex === 'wrdkmc'">
          <a @click="skip(record)">{{ text }}</a>
        </template>
      </template>
      <template #tableTitle>
        <a-button type="primary" @click="handle('zyjh')" v-auth="'trwy-zyjs-zyjh'">转运计划填报</a-button>
        <a-button type="primary" @click="handle('jswc')" v-auth="'trwy-zyjs-jswc'">接收完成填报</a-button>
        <a-button type="primary" @click="handle('dgszbm')" v-auth="'trwy-zyjs-dgszbm'" :disabled="szdshow">地块所在地部门确认</a-button>
        <a-button type="primary" @click="handle('jsdbm')" v-auth="'trwy-zyjs-jsdbm'" :disabled="jsshow">接收地部门确认</a-button>
      </template>
      
      <template #jsdFlag="{ record }">
        <span>{{ (record.jsdFlag || record.jsdFlag === 0) ? (record.jsdFlag == '1' ? '是' : record.jsdFlag == '-1' ? '待确认' : record.jsdFlag == '2' ? '进行中' : '否') : '' }}</span>
        <a-button type="link" v-if="record.jsdFlag == '2'" @click="handleQrxx(record)">查看</a-button>
      </template>
    </BasicTable>
    <!--接收完成填报 -->
    <fillingendModal @register="registerModal" @success="handleSuccess" />
    <!--部门确认 -->
    <depconfirmationModal @register="registerbmModal" @success="handleSuccess" />

    <a-modal v-model:visible="openvisible" title="确认部门信息" @ok="openvisible=false" :footer="false">
      <a-table :dataSource="modalList" :columns="modalcolumns" />
    </a-modal>
  </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 { columns } from './data';
  import fillingendModal from './modal/fillingendModal.vue';
  import depconfirmationModal from './modal/depconfirmationModal.vue';
  import { useRouter } from 'vue-router';
  import { list, findSzdzg, queryZyjh } from './api';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import { useUserStore } from '/@/store/modules/user';

  const { createMessage } = useMessage();
  const userStore: any = useUserStore();
  let router = useRouter();
  const [registerModal, { openModal }] = useModal();
  const [registerbmModal, { openModal: openbmModal }] = useModal();
  const openvisible = ref(false)
  const szdshow = ref(true);
  const jsshow = ref(true);
  const modalcolumns = ref([{
    title: '接受部门名称',
    dataIndex: 'name',
    algin:'center',
    width:'200'
  },{
    title: '确认状态',
    dataIndex: 'zt',
    algin:'center',
    width:'200'
  }])
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '重点监管地块',
      api: list,
      columns: columns,
      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;

  async function handle(v) {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    const obj = { ...selectedRows.value[0] };
    if (v == 'jswc') {
      // 需要加个判断判断两个主管部门都确认才行
      if (obj.szdFlag != 1 || obj.jsdFlag != 1) {
        createMessage.info('两地主管部门确认完成之后，才能进行接收完成报送数据的填报');
        return;
      }
      if (obj.flag == 1) {
        openModal(true, {
          record: obj,
          isUpdate: true,
          showFooter: true,
        });
      } else if (obj.flag == 2){
        findSzdzg
        const szd = await findSzdzg({ wrdkbm: obj.wrdkbm });
        const res = await queryZyjh({ id: obj.id });
        for (let i = 0; i < res.czwcList.length; i++) {
          const element = res.czwcList[i];
           element.szdRealname = szd.realname
           element.jsdRealname = obj.jsdRealname.split(',')[i]?obj.jsdRealname.split(',')[i]:''
        }
        obj.czwcList = res.czwcList
        openModal(true, {
          record: obj,
          isUpdate: true,
          showFooter: false,
        });
      }
      
    } else if (v == 'dgszbm') {
      obj.type = '1';
      obj.zglx = 'szd';

      openbmModal(true, {
        record: obj,
        isUpdate: true,
        showFooter: true,
        title: '地块所在部门确认',
      });
    } else if (v == 'jsdbm') {
      obj.type = '1';
      obj.zglx = 'jsd';

      openbmModal(true, {
        record: obj,
        isUpdate: true,
        showFooter: true,
        title: '接收地部门确认',
      });
    } else {
      router.push(
        `/dkgl/soil/components/filling?id=${selectedRowKeys.value[0]}&flag=${selectedRows.value[0].flag}&wrdkbm=${selectedRows.value[0].wrdkbm}`
      );
    }
  }
  function rowClick({ rows }) {
    if (rows[0]) {
      // flag=1代表提交的
      if (rows[0].flag == 1) {
        // 所在地部门确认为是并且接收地确认为是的时候
        // if (rows[0].szdFlag == 1 && rows[0].jsdFlag == 1) {
        //   jswc.value = false;
        // }
        // 所在地部门未确认且当前用户是该所在地部门用户
        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.includes(userStore.userInfo.username) && !rows[0].jsdQr.includes(userStore.userInfo.username)) {
          jsshow.value = false;
        } else {
          jsshow.value = true;
        }
      } else if (rows[0].flag == 2) {
        // flag=2接收转运处置
        szdshow.value = true;
        jsshow.value = true;
        // jswc.value = true;
      }
    }
  }
  // 点击详情调整
  function skip(record) {
    router.push(`/dkgl/soil/components/filling?id=${record.id}&flag=1&wrdkbm=${record.wrdkbm}&type=detail`);
  }
  const modalList = ref<any>([])
  //查看接受地确认企业信息
  function handleQrxx(record) {
    openvisible.value = true
    modalList.value = []
    const listName = (record.jsdRealname).split(',')
    for (let i = 0; i < listName.length; i++) {
      const element = listName[i];
      let obj = {
        name: element,
        zt: record?.jsdQrName?.indexOf(element)>-1?'已确认':'未确认'
      }
      modalList.value.push(obj)
    }
  }
  function handleSuccess() {
    reload();
    selectedRowKeys.value = [];
    selectedRows.value = [];
  }
</script>
<style scoped></style>
