<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <!-- 每一个页面都有，无需配置权限 -->
        <a-button type="primary" @click="router.go(-1)">返回</a-button>
        <!-- 7个
                  填报中-查看基础信息：gkpg-tbz-fa-jbxx
                  待受理-查看基础信息：gkpg-dsl-fa-jbxx
                  待评审-查看基础信息：gkpg-dps-fa-jbxx
                  评审已通过-查看基础信息：gkpg-psytg-fa-jbxx
                  评审不通过-查看基础信息：gkpg-psbtg-fa-jbxx
                  评审后待完善-查看基础信息：gkpg-pshdws-fa-jbxx
                  不评审-查看基础信息：gkpg-bps-fa-jbxx
                 -->
        <a-button
          type="primary"
          @click="view"
          v-if="
            hasPermission('gkpg-tbz-fa-jbxx') ||
            hasPermission('gkpg-dsl-fa-jbxx') ||
            hasPermission('gkpg-dps-fa-jbxx') ||
            hasPermission('gkpg-psytg-fa-jbxx') ||
            hasPermission('gkpg-psbtg-fa-jbxx') ||
            hasPermission('gkpg-pshdws-fa-jbxx') ||
            hasPermission('gkpg-bps-fa-jbxx')
          "
          >查看基本信息</a-button
        >
        <template v-if="type == '填报'">
          <a-button type="primary" @click="filling" v-auth="'gkpg-tbz-zltb'">资料填报</a-button>
          <a-button type="primary" @click="add" v-auth="'gkpg-tbz-xzfa'">新增报告</a-button>
          <a-popconfirm title="确定要删除此报告？" ok-text="确定" cancel-text="取消" @confirm="del">
            <a-button type="primary" v-auth="'gkpg-tbz-scbg'">删除报告</a-button>
          </a-popconfirm>
          <a-button type="primary" @click="submit('tj')" v-auth="'gkpg-tbz-tj'">提交</a-button>
        </template>
        <template v-else-if="type == '待受理'">
          <a-button type="primary" @click="handleAdd" v-auth="'gpkg-dsl-sl'">受理</a-button>
          <!-- <a-button type="primary" @click="backPs" v-auth="'gpkg-dps-fa-th'">退回</a-button> -->
        </template>
        <template v-else-if="type == '待评审'">
          <a-button type="primary" @click="toReview" v-auth="'gpkg-dps-fa-psclsc'">评审材料上传</a-button>
          <a-button type="primary" @click="backPs" v-auth="'gpkg-dps-fa-th'">退回</a-button>
        </template>
        <template v-else-if="type == '评审已通过'">
          <a-button type="primary" @click="submit('thbsp')" v-auth="'gpkg-dps-thbps'">退回无需评审</a-button>
        </template>
        <template v-else-if="type == '评审后待完善'">
          <a-button type="primary" @click="handleComint" v-auth="'gpkg-dqr-dqr'">待确认</a-button>
        </template>
      </template>
    </BasicTable>
    <a-modal v-model:visible="evaluate" :centered="true" okText="我已了解，继续提交" cancelText="取消" title="信息" @ok="handleEvaluate">
      <div class="py-4 ml-5">
        按照《建设用地土壤污染风险管控和修复从业单位和个人执业情况信用记录管理办法》第十五条，土壤污染状况调查、土壤污染风险评估.(试行)管控方案编制、修复方案编制、风险管控效果评估、修复效果评估、风险管控施工修复施工、工程监理、土壤和地下水监测的从业单位，应当在项实施所涉及的土壤污染状况调查报告、风险评估报告、效果评估报告岌凤险管控写修夏方案依据《中举火民共和国生最污梁防治法》提审或备案前，在信用记录系统记录单位和个人业绩情况信息
      </div>
    </a-modal>

    <AcceptanceModal @register="registerModal" @success="handleSuccess" />
    <backModal @register="registerBackModal" @success="handleSuccess"></backModal>
    <SendBack @register="registerNoModal" @success="handleSuccess" />
    <ConfirmedModal @register="registerConfirmedModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" name="dkgl-gkpg-DetailList" setup>
  import { ref } from 'vue';
  import { BasicTable } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useRouter } from 'vue-router';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Modal } from 'ant-design-vue';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { useListPage } from '/@/hooks/system/useListPage';
  import AcceptanceModal from './modal/AcceptanceModal.vue';
  import backModal from '@/components/customComponent/backModal.vue';
  import SendBack from '/@/components/customComponent/SendBack.vue';
  import { Fxcolumns } from './data';
  import { faList, addXgpg, delXgpg, psOrZxps, submitXgpg } from '/@/api/common/api';
  import ConfirmedModal from '@/components/customComponent/ConfirmedModal.vue';

  const router = useRouter();
  const { createMessage } = useMessage();
  const { hasPermission } = usePermission();
  const [registerModal, { openModal }] = useModal();
  const [registerConfirmedModal, { openModal: openConfirmedModal }] = useModal();
  const [registerBackModal, { openModal: openBackModal }] = useModal();
  const [registerNoModal, { openModal: openNoModal }] = useModal();
  const wrdkid = ref(router.currentRoute.value.query.wrdkid);
  const type = ref(router.currentRoute.value.query.type);
  const isnyd2yzlg = ref(router.currentRoute.value.query.isnyd2yzlg);
  const jdnzt = ref(router.currentRoute.value.query.jdnzt);
  const evaluate = ref(false);
  const No = ref(false);
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      title: '基本信息列表',
      api: faList,
      beforeFetch(params) {
        params.wrdkid = wrdkid.value;
        params.scjdbm = 'S6';
        params.jdnzt = jdnzt.value;
      },
      columns: Fxcolumns,
      canResize: false,
      useSearchForm: false,
      rowKey: 'id',
      rowSelection: { type: 'radio' },
      clickToRowSelect: true,
      showActionColumn: false,
      actionColumn: {
        width: 120,
      },
    },
  });
  const [registerTable, { reload }, { rowSelection, selectedRowKeys, selectedRows }] = tableContext;

  // 资料填报
  function filling() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (selectedRows.value[0].tbzt == 't2' || selectedRows.value[0].tbzt == '已评审' || selectedRows.value[0].tbzt == '待评审') {
      createMessage.info('当前状态能填报资料');
      return;
    }
    router.push(`/dkgl/gkpg/FillingInfor?wrdkid=${wrdkid.value}&id=${selectedRowKeys.value[0]}&pgtype=${selectedRows.value[0].pgtype}`);
  }
  // 点击新增报告
  async function add() {
    await addXgpg({ wrdkid: wrdkid.value });
    reload();
  }
  // 删除方案列表
  async function del() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    Modal.confirm({
      title: '确认删除',
      content: '是否删除选中数据',
      centered: true,
      okText: '确认',
      cancelText: '取消',
      onOk: async () => {
        await delXgpg({ id: selectedRowKeys.value[0], wrdkid: wrdkid.value });
        reload();
      },
    });
  }
  // 查看基本信息
  function view() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    router.push(`/dkgl/gkpg/ViewDetail?wrdkid=${selectedRows.value[0].wrdkid}&id=${selectedRows.value[0].id}&pgtype=${selectedRows.value[0].pgtype}`);
  }
  // 点击评审弹框
  async function handleEvaluate() {
    if (!selectedRows.value[0].bt) {
      No.value = true;
      return;
    }
    await psOrZxps({
      zjlz: 1,
      wrdkid: wrdkid.value,
      id: selectedRows.value[0].id,
    });
    reload();
  }

  function handleAdd() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    const obj = selectedRows.value[0];
    obj.jdnzt = jdnzt.value;
    obj.wrdkid = wrdkid.value;
    obj.pgtype = selectedRows.value[0].pgtype;
    openModal(true, {
      record: obj,
      isUpdate: true,
      showFooter: true,
    });
  }
  // 点击退回评审
  function backPs() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    const obj = selectedRows.value[0];
    obj.tbjdbm = jdnzt.value;
    obj.pgtype = selectedRows.value[0].pgtype;
    openBackModal(true, {
      record: obj,
      showFooter: true,
    });
  }

  function handleSuccess() {
    selectedRows.value = [];
    selectedRowKeys.value = [];
    reload();
  }
  // 点击汇总
  async function submit(v) {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (v == 'tj') {
      let obj: any = {};
      obj.id = selectedRowKeys.value[0];
      obj.tbjdbm = jdnzt.value;
      obj.wrdkid = wrdkid.value;
      obj.scjdbm = 'S6';
      obj.pgtype = selectedRows.value[0].pgtype;
      await submitXgpg(obj);
      handleSuccess();
    } else if (v == 'thbsp') {
      const obj: any = selectedRows.value[0];
      obj.wrdkid = wrdkid.value;
      obj.tbjdbm = jdnzt.value;
      openNoModal(true, {
        record: obj,
        showFooter: true,
      });
    }
  }
  function toReview() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    const { value: [row] = [] } = selectedRows;
    const { pgtype, scjdbm } = row || {};
    router.push(`/dkgl/gkpg/modal/ReviewMaterialUpload?wrdkid=${wrdkid.value}&isnyd2yzlg=${isnyd2yzlg.value}&pgtype=${pgtype}&scjdbm=${scjdbm}`);
  }
  function handleComint() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (selectedRows.value[0].tbjdbm == 'DSL' && selectedRows.value[0].sfthxg == 1) {
      openConfirmedModal(true, {
        record: selectedRows.value[0],
        isUpdate: true,
        showFooter: true,
      });
    } else {
      createMessage.info('数据还未提交，请提交后再进行操作');
    }
  }
</script>
<style scoped></style>
