<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <!-- 每一个页面都有，无需配置权限 -->
        <a-button type="primary" @click="router.go(-1)">返回</a-button>
        <!-- 7个
                  填报中-查看基础信息：xfpg-tbz-fa-jbxx
                  待受理-查看基础信息：xfpg-dsl-fa-jbxx
                  待评审-查看基础信息：xfpg-dps-fa-jbxx
                  评审已通过-查看基础信息：xfpg-psytg-fa-jbxx
                  评审不通过-查看基础信息：xfpg-psbtg-fa-jbxx
                  评审后待完善-查看基础信息：xfpg-pshdws-fa-jbxx
                  不评审-查看基础信息：xfpg-bps-fa-jbxx
                  -->
        <a-button
          type="primary"
          @click="view"
          v-if="
            hasPermission('xfpg-tbz-fa-jbxx') ||
            hasPermission('xfpg-dsl-fa-jbxx') ||
            hasPermission('xfpg-dps-fa-jbxx') ||
            hasPermission('xfpg-psytg-fa-jbxx') ||
            hasPermission('xfpg-psbtg-fa-jbxx') ||
            hasPermission('xfpg-pshdws-fa-jbxx') ||
            hasPermission('xfpg-bps-fa-jbxx')
          "
          >查看基本信息</a-button
        >
        <template v-if="type == '填报'">
          <a-button type="primary" @click="add" v-auth="'xfpg-tbz-xzfa'">新增方案</a-button>
          <a-button type="primary" @click="filling" v-auth="'xfpg-tbz-zltb'">资料填报</a-button>
          <a-button type="primary" v-auth="'xfpg-tbz-scfa'" @click="del">删除方案</a-button>

          <a-button type="primary" @click="submit('tj')" v-auth="'xfpg-tbz-zxps'">提交</a-button>
        </template>
        <template v-else-if="type == '待受理'">
          <a-button type="primary" @click="handleAdd('ysl')" v-auth="'xfpg-dsl-ysl'">预受理</a-button>
          <a-button type="primary" @click="handleAdd('')" v-auth="'xfpg-dsl-sl'">受理</a-button>
          <!-- <a-button type="primary" @click="backPs" v-auth="'xfpg-dps-fa-th'">退回</a-button> -->
        </template>
        <template v-else-if="type == '待评审'">
          <a-button type="primary" @click="toReview" v-auth="'xfpg-dps-fa-psclsc'">评审材料上传</a-button>
          <a-button type="primary" @click="backPs" v-auth="'xfpg-dps-fa-th'">退回</a-button>
        </template>
        <template v-else-if="type == '评审已通过'">
          <a-button type="primary" @click="submit('bps')" v-auth="'xfpg-dps-thbps'">退回无需评审</a-button>
          <a-button type="primary" @click="submit('cc')" v-auth="'xfpg-psytg-cc'">地块抽查</a-button>
          <a-button type="primary" @click="submit('cclh')" v-auth="'xfpg-psytg-cclh'">地块抽查历史</a-button>
        </template>
        <template v-else-if="type == '评审后待完善'">
          <a-button type="primary" @click="handleComint" v-auth="'xfpg-dqr-dqr'">待确认</a-button>
        </template>
        <template v-else-if="type == '抽查中'">
          <a-button type="primary" v-auth="'gkpg-cc-clps'" @click="submit('psclsc')">抽查评审</a-button>
          <a-button type="primary" v-auth="'gkpg-cc-clsc'" @click="submit('clsc')">材料上传</a-button>
        </template>
        <a-button
          type="primary"
          @click="handelStep"
          v-if="
            hasPermission('xfpg-tbz-fa-lct') ||
            hasPermission('xfpg-dsl-fa-lct') ||
            hasPermission('xfpg-dps-fa-lct') ||
            hasPermission('xfpg-psytg-fa-lct') ||
            hasPermission('xfpg-psbtg-fa-lct') ||
            hasPermission('xfpg-pshdws-fa-lct') ||
            hasPermission('xfpg-bps-fa-lct')
          "
          >流程图</a-button
        >
      </template>
    </BasicTable>
    <a-modal v-model:visible="evaluate" :centered="true" okText="我已了解，继续提交" cancelText="取消" title="信息" @ok="handleEvaluate">
      <dksumitTip />
    </a-modal>
    <a-modal v-model:visible="No" cancelText="取消" okText="确定">
      <template #title>
        <div>
          <a-button @click="No = false" type="primary">确定</a-button>
        </div>
      </template>
      <div class="py-8 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" />
    <!-- 预受理 -->
    <Paccept @register="registerlyslModal" @success="handleSuccess" />
    <!-- 抽查历史 -->
    <CheckHistory @register="registerCcModal" @success="handleSuccess" />
    <Upcheckcl @register="registerUpccModal" @success="handleSuccess" />
    <!-- 流程图 -->
    <Step @register="registerlcModal" />
  </div>
</template>

<script lang="ts" name="dkgl-fxgk-Filling" setup>
  import { ref } from 'vue';
  import { Modal } from 'ant-design-vue';
  import { BasicTable } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useRouter } from 'vue-router';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { useListPage } from '/@/hooks/system/useListPage';
  import AcceptanceModal from '@/components/customComponent/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, checkXgpgTb } from '/@/api/common/api';
  import ConfirmedModal from '@/components/customComponent/ConfirmedModal.vue';
  import CheckHistory from '/@/components/customComponent/CheckHistory.vue'; // 新组件
  import Paccept from '@/components/customComponent/Pacceptance.vue';
  import Upcheckcl from '/@/components/customComponent/Upcheckcl.vue';
  import { spotCheck, oneTip, chTip } from '@/utils/checkTip';
  import Step from '/@/components/customComponent/steps/step.vue';
  import { tbzjxx } from '/@/api/common/api';
  import dksumitTip from '../components/dksumitTip.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 [registerCcModal, { openModal: openCcModal }] = useModal();
  const [registerUpccModal, { openModal: openNoUpccModal }] = useModal();
  const [registerlcModal, { openModal: openlcModal }] = useModal();
  const [registerlyslModal, { openModal: openlyslModal }] = useModal();

  const wrdkid = ref(router.currentRoute.value.query.wrdkid);
  const type = ref(router.currentRoute.value.query.type);
  const jdnzt = ref(router.currentRoute.value.query.jdnzt);
  const isnyd2yzlg = ref(router.currentRoute.value.query.isnyd2yzlg);
  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;
        if (type.value == '评审已通过') {
          let res = [...Fxcolumns];
          res.splice(4, 0, {
            title: '地块是否超标',
            dataIndex: 'sfcb',
            resizable: true,
            customRender: ({ record }: any) => {
              return record?.sfcb==='1'?'是':record?.sfcb==='0'?'否':'未调查';
            },
          });
          res.splice(5, 0, {
            title: '地块锁定状态',
            dataIndex: 'dksdztText',
            resizable: true,
          });
          setColumns(res);
        } else if (type.value == '抽查中') {
          let res = [...Fxcolumns];
          res.splice(4, 0, {
            title: '地块是否超标',
            dataIndex: 'sfcb',
            resizable: true,
            customRender: ({ record }: any) => {
              return record?.sfcb==='1'?'是':record?.sfcb==='0'?'否':'未调查';
            },
          });
          res.splice(5, 0, {
            title: '抽查状态',
            dataIndex: 'tgjg',
            resizable: true,
          });
          setColumns(res);
        } else if (type.value == '待受理') {
          let res = [...Fxcolumns];
          res.splice(4, 0, {
            title: '完成初审提醒',
            dataIndex: 'tx',
            resizable: true,
          });

          setColumns(res);
        } else if (type.value == '待评审') {
          let res = [...Fxcolumns];
          res.splice(4, 0, {
            title: '组织评审提醒',
            dataIndex: 'tx',
            resizable: true,
          });

          setColumns(res);
        } else if (type.value == '评审后待完善') {
          let res = [...Fxcolumns];
          res.splice(4, 0, {
            title: '完善报告提醒',
            dataIndex: 'tx',
            resizable: true,
          });

          setColumns(res);
        }
      },
      columns: Fxcolumns,
      canResize: false,
      useSearchForm: false,
      rowKey: 'id',
      rowSelection: type.value != '评审已通过' ? { type: 'radio' } : { type: 'checkbox' },
      clickToRowSelect: true,
      showActionColumn: false,
      actionColumn: {
        width: 120,
      },
    },
  });
  const [registerTable, { reload, setColumns }, { rowSelection, selectedRowKeys, selectedRows }] = tableContext;

  // 资料填报
  async function filling() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (!chTip(selectedRows.value)) return;
    if (selectedRows.value[0].tbzt == 't2' || selectedRows.value[0].tbzt == '已评审' || selectedRows.value[0].tbzt == '待评审') {
      createMessage.info('当前状态能填报资料');
      return;
    }
    // 效果判断方案是否在外运模块里面如果在不可以提交不可以填报
    await checkXgpgTb({ wrdkid: wrdkid.value });

    router.push(`/dkgl/xfpg/FillingInfor?wrdkid=${wrdkid.value}&id=${selectedRowKeys.value[0]}&pgtype=${selectedRows.value[0].pgtype}`);
  }
  // 点击查看流程图
  async function handelStep() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }

    const params = {
      pgtype: selectedRows.value[0].pgtype,
      wrdkid: wrdkid.value,
    };
    openlcModal(true, {
      record: params,
      isUpdate: true,
      showFooter: true,
    });
  }
  // 点击新增方案
  async function add() {
    await addXgpg({ wrdkid: wrdkid.value });
    reload();
  }
  // 删除方案列表
  async function del() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (!chTip(selectedRows.value)) 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/xfpg/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;
    }
    if (!chTip(selectedRows.value)) return;
    await psOrZxps({
      zjlz: 1,
      pgtype: selectedRows.value[0].pgtype,
      wrdkid: wrdkid.value,
      id: selectedRows.value[0].id,
    });
    evaluate.value = false;
    reload();
  }

  function handleAdd(v) {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (!chTip(selectedRows.value)) return;
    const obj: any = {};
    obj.id = selectedRowKeys.value[0];
    obj.jdnzt = jdnzt.value;
    obj.wrdkid = wrdkid.value;
    obj.pgtype = selectedRows.value[0].pgtype;
    obj.scjdbm = 'S6';
    if (v == 'ysl') {
      openlyslModal(true, {
        record: obj,
        isUpdate: true,
        showFooter: true,
      });
    } else {
      // 判断有没有预售时间
      if (!selectedRows.value[0].tx) {
        createMessage.info('请先进行预处理');
        return;
      }
      openModal(true, {
        record: obj,
        isUpdate: true,
        showFooter: true,
      });
    }
  }

  // 点击退回评审
  function backPs() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (!chTip(selectedRows.value)) return;
    const obj = selectedRows.value[0];
    obj.tbjdbm = jdnzt.value;
    obj.pgtype = selectedRows.value[0].pgtype;
    obj.scjdbm = selectedRows.value[0].scjdbm;
    openBackModal(true, {
      record: obj,
      showFooter: true,
    });
  }
  // 点击提交
  async function submit(v) {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容');
      return;
    }
    // if (!chTip(selectedRows.value)) return;
    if (v == 'tj') {
      if (!chTip(selectedRows.value)) return;
      // 效果判断方案是否在外运模块里面如果在不可以提交不可以填报
      await checkXgpgTb({ wrdkid: wrdkid.value });
      
      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 == 'bps') {
      if (!chTip(selectedRows.value)) return;
      const obj: any = selectedRows.value[0];
      obj.wrdkid = wrdkid.value;
      obj.tbjdbm = jdnzt.value;
      openNoModal(true, {
        record: obj,
        showFooter: true,
      });
    } else if (v == 'cc') {
      if (!chTip(selectedRows.value)) return;
      if (selectedRows.value[0].sftc == '已退出') {
        createMessage.info('地块已退出不能抽查');
        return;
      }

      const params = {
        wrdkid: wrdkid.value,
        sccdmkbm: 'S6',
        faid: selectedRowKeys.value?.toString(),
        scjdbm: 'S6',
        pgtype: selectedRows.value[0].pgtype,
      };
      spotCheck(params, handleSuccess);
    } else if (v == 'cclh') {
      if (!chTip(selectedRows.value)) return;
      if (oneTip(selectedRowKeys.value)) return;

      const params = {
        wrdkid: wrdkid.value,
        scjdbm: 'S6',
        faid: selectedRowKeys.value[0],
        type: 'xfpg',
      };
      openCcModal(true, {
        record: params,
        showFooter: true,
      });
    } else if (v == 'psclsc') {
      if (selectedRowKeys.value.length == 0) {
        createMessage.info('请选择需要处理的内容，每次只能选中一条');
        return;
      }
      if (selectedRows.value[0].tgjg != '待抽查') {
        if (!chTip(selectedRows.value)) return;
      }
      
      const { value: [row] = [] } = selectedRows;
      const { pgtype, scjdbm } = row || {};
      // 评审材料
      router.push(
        `/dkgl/xfpg/modal/ReviewMaterialUpload?wrdkid=${wrdkid.value}&isnyd2yzlg=${isnyd2yzlg.value}&pgtype=${pgtype}&scjdbm=${scjdbm}&dksdzt=${selectedRows.value[0].dksdzt}&faid=${selectedRows.value[0].id}`
      );
    } else if (v == 'clsc') {
      if (!chTip(selectedRows.value)) return;
      if (selectedRows.value[0].tgjg != '不通过') {
        createMessage.info('请选择不通过的数据');
        return;
      }
      openNoUpccModal(true, {
        record: selectedRows.value[0],
        showFooter: true,
      });
    }
  }
  function handleSuccess() {
    selectedRows.value = [];
    selectedRowKeys.value = [];
    reload();
  }
  async function toReview() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (!chTip(selectedRows.value)) return;
    const { value: [row] = [] } = selectedRows;
    const { pgtype, scjdbm } = row || {};
    const params = {
      scjdbm: scjdbm,
      wrdkid: wrdkid.value,
      pgtype: pgtype,
    };
    await tbzjxx(params);
    router.push(`/dkgl/xfpg/modal/ReviewMaterialUpload?wrdkid=${wrdkid.value}&isnyd2yzlg=${isnyd2yzlg.value}&pgtype=${pgtype}&scjdbm=${scjdbm}`);
  }
  function handleComint() {
    if (selectedRowKeys.value.length == 0) {
      createMessage.info('请选择需要处理的内容，每次只能选中一条');
      return;
    }
    if (!chTip(selectedRows.value)) 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>
