<template>
  <a-layout>
    <a-layout-content style="padding: 0;">
      <a-card class="box-card" style="flex: 1; overflow-y: auto; padding: 0 !important;">
        <pblm-query-terms ref="queryTerms" :showRectifyStatus="false" @queryByParams="queryByParams" />
        <a-row type="flex" justify="space-between" style="border-top:1px solid #0014ff2e;">
          <a-col :span="24" class="el-row-operation">
            <a-button
              @click="openActivityCommentDialog(1, $event)"
              :disabled="loading"
              size="small"
              type="primary"
              style="color:rgba(255,255,255,1);float:left;margin-right:10px;">送销号复核</a-button>
            <a-button
              @click="openMonthlyReportDialog(1, $event)"
              :disabled="loading"
              size="small"
              type="primary"
              style="color:rgba(255,255,255,1);float:left;margin-right:10px;">上报整改进度</a-button>
            <a-button
              @click="downloadMonthlyReportExcelTemplate()"
              :disabled="loading"
              size="small"
              type="primary"
              style="color:rgba(255,255,255,1);float:left;margin-right:10px;">下载整改进度月报模板</a-button>
            <a-button
              @click="setBatchXh"
              :disabled="loading"
              size="small"
              type="primary"
              style="color:rgba(255,255,255,1);float:left;margin-right:10px;">归档销号</a-button>
          </a-col>
        </a-row>
        <pblm-table
          ref="sjdwPblmTable"
          :pblmTableSetting="sjdwUnderRectifyTableSetting"
          @query="getUnderRectifyPblm"
          @pageChange="handlePageChange"
          @sizeChange="handleSizeChange"
          @selectionChange="handleSelectionChange"
          :isGDXH="true" />
        <a-modal
          :open="showRectifyUnitDialog"
          @ok="getOk"
          @cancel="closeRectifyUnitDialog"
          title="指派整改单位"
          width="60%"
          class="outerDialog custom_dialog">
          <pblm-rectify-unit ref="rectifyUnitInfoDialog" @saveRectifyUnitInfo="saveRectifyUnitInfo" />
          <span slot="okText">提交</span>
        </a-modal>
        <activity-comment
          v-if="showActivityCommentDialog"
          :isAg="this.isAg"
          :activityName="this.actName"
          @saveActivityComment="saveActivityComment"
          @closeActivityCommentDialog="closeActivityCommentDialog" />
        <monthly-report-comment
          v-if="showMonthlyReportCommentDialog"
          :isAg="this.isAg"
          :activityName="this.actName"
          @saveMonthlyReportComment="saveMonthlyReportComment"
          @closeMonthlyReportCommentDialog="closeMonthlyReportCommentDialog" />
      </a-card>
    </a-layout-content>
  </a-layout>
</template>

<script>
import moment from 'moment';
import request from '../../utils/gw_ajax_request';
import { formatDateTime2 } from '../../utils/gw_date';
import { exportModel } from '../../utils/gw_exportModel';
import MonthlyReportComment from '../PblmRectifyMonthlyReport/Components/MonthlyReportComment.vue';
import {
  approvedPblmRectify,
  distributePblmToSpecificOrg,
  formatPblms,
  redistributePblmToSpecificOrg,
  requestUnderRectifyPblm,
} from './Common/rectify_api';
import { formatDeadLineDate } from './Common/rectify_date';
import ActivityComment from './Components/ActivityComment.vue';
import pblmQueryTerms from './Components/pblmQueryTerms';
import pblmRectifyUnit from './Components/pblmRectifyUnit';
import pblmTable from './Components/pblmTable';
export default {
  name: 'SjdwUnderRectifyPblm',
  props: [],
  components: {
    pblmQueryTerms,
    pblmTable,
    pblmRectifyUnit,
    ActivityComment,
    MonthlyReportComment,
  },
  data() {
    return {
      moment,
      loading: false,
      queryParams: {
        hasVedio: '',
        adCode: '',
        pblmStat: '',
        inspPblmCate: '',
        ifCasePblm: '',
        nm: '',
        rsName: '',
        adFullName: '',
        startTime: '',
        endTime: '',
        objType: '', // 为空才能不区分督查类别
        inspPblmName: '',
        dataStat: '2', // 整改阶段必须为2
        persId: localStorage.getItem('persId'),
        villType: '0',
        pageSize: 10,
        pageNum: 1,
        pblmTotal: 0,
        pTypeCode: '', // 督查类别
        pYear: '', // 年度
        zgXianOrgId: null, // 接收单位
        zgWh: '', // 整改文号
        workflowStat: '22', // important 仅获取待市级单位审批的问题
        schdYear: moment(new Date().getFullYear().toString()).format('YYYY'),
      },
      showRectifyUnitDialog: false,
      sjdwUnderRectifyTableSetting: {
        adType: false,
        currentFllow: '22',
        data: [], // 主数据源
        pagination: {
          pageSize: 10,
          pageNum: 1,
          pblmCount: 0,
        }, // 翻页相关配置项
        height: 500,
        currentPblmId: '', // 当前选中的问题的ID
        selectedPblms: {}, // 多选模式下选中的所有问题集合
        multiSelect: true, // 是否出现行复选框
        enablePblmRectifyInfo: true, // 操作栏是否有整改信息输入按钮
        isZGZWTBtn: true, // 是否是整改中问题 整改中问题 查看详情 附件可以删除
        ifChao: true,
      },
      showActivityCommentDialog: false,
      isAg: 1, // 流程流转方向 0-正常向后 1=正常向前 2=列入调度清单
      actName: '', // 流程流转方向名称
      showMonthlyReportCommentDialog: false,
    };
  },
  created() {
    this.getUnderRectifyPblm();
  },
  mounted() {
    this.$nextTick(function () {
      this.sjdwUnderRectifyTableSetting.height =
        document.getElementsByClassName('ant-tabs')[0].offsetHeight - 260;
    });
  },
  computed: {
    formatDateTime() {
      return formatDateTime2;
    },
    formatDeadLineDate() {
      return formatDeadLineDate;
    },
  },
  methods: {
    getOk() {
      this.$refs.rectifyUnitInfoDialog.saveRectifyUnitInfo();
    },
    openMonthlyReportDialog(isAg, e) {
      this.isAg = isAg;
      this.actName = e.currentTarget.innerText;
      this.showMonthlyReportCommentDialog = true;
    },
    closeMonthlyReportCommentDialog() {
      this.showMonthlyReportCommentDialog = false;
    },
    saveMonthlyReportComment(val) {
      this.closeMonthlyReportCommentDialog();
      this.SubmitMonthlyReportForSelectedPblm(
        val.isAg,
        val.Comment,
        val.fileExt,
        val.filePath,
      );
    },
    // 上报整改进度月报
    SubmitMonthlyReportForSelectedPblm(
      isAg,
      activityComment,
      fileExt,
      filePath,
    ) {
      const data = {
        fileName: filePath,
        persId: localStorage.getItem('persId'),
        persName: localStorage.getItem('account1'),
      };
      request.post('/dc/insp/pblm/insertXls', data).then((res) => {
        if (res.data.type) {
          this.$message.error('当前文件不存在！');
        } else {
          this.$message.success(
            '成功:' + res.data.success + '条,失败:' + res.data.false + '条',
          );
        }
      });
    },
    async downloadMonthlyReportExcelTemplate() {
      const datas = JSON.stringify(this.queryParams);
      const url = '/pdcApi/dc/insp/pblm/page/rever/exportxian';
      const type = 'post';
      const exportName = '整改进度月报模板.xls';
      const result = await exportModel(type, url, exportName, datas);
    },
    // 按指定条件获取待下发问题列表数据
    queryByParams(val) {
      this.queryParams = val.queryParams;
      this.queryParams.workflowStat = 22;
      this.$refs.sjdwPblmTable.clearSelection(); // 清空问题列表子组件中原有的选中的行
      this.getUnderRectifyPblm();
    },
    // 获取待下发问题列表数据
    getUnderRectifyPblm() {
      this.loading = true;
      this.queryParams.pageSize =
        this.sjdwUnderRectifyTableSetting.pagination.pageSize;
      this.queryParams.pageNum =
        this.sjdwUnderRectifyTableSetting.pagination.pageNum;
      this.sjdwUnderRectifyTableSetting.selectedPblms = [];
      requestUnderRectifyPblm(this.queryParams).then((res) => {
        this.loading = false;
        if (res.data !== null) {
          this.sjdwUnderRectifyTableSetting.data = res.data.list;
          /* this.sjdwUnderRectifyTableSetting.data.sort(function (x, y) {
            return x.zgDate - y.zgDate;
          }); */
          this.sjdwUnderRectifyTableSetting.pagination.pblmCount =
            res.data.total;
        } else {
          this.sjdwUnderRectifyTableSetting.data = [];
          this.sjdwUnderRectifyTableSetting.pagination.pblmCount = 0;
        }
      });
    },
    // 直接页数跳转
    handlePageChange(val) {
      this.sjdwUnderRectifyTableSetting.pagination.pageNum = val.pageNum;
      this.getUnderRectifyPblm();
    },
    // 调整每页显示记录数
    handleSizeChange(val) {
      this.sjdwUnderRectifyTableSetting.pagination.pageSize = val.pageSize;
      this.getUnderRectifyPblm();
    },
    // 选中行时获取所有已选中行
    handleSelectionChange(val) {
      this.sjdwUnderRectifyTableSetting.selectedPblms = val.selectedPblms;
    },
    // 上报所选问题到上级单位
    approvedSelectedPblm() {
      if (this.sjdwUnderRectifyTableSetting.selectedPblms.length > 0) {
        this.loading = true;
        approvedPblmRectify(
          this.sjdwUnderRectifyTableSetting.selectedPblms,
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$refs.sjdwPblmTable.clearSelection(); // 清空问题列表子组件中原有的选中的行
            this.getUnderRectifyPblm();
            this.$message.success('所选问题已上报到上级单位!');
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要上报的问题!');
      }
    },
    // 上报所选问题到上级单位
    approvedAllPblm() {
      if (this.sjdwUnderRectifyTableSetting.data.length > 0) {
        this.loading = true;
        approvedPblmRectify(this.sjdwUnderRectifyTableSetting.data).then(
          (res) => {
            if (res.success) {
              this.loading = false;
              this.$refs.sjdwPblmTable.clearSelection(); // 清空问题列表子组件中原有的选中的行
              this.getUnderRectifyPblm();
              this.$message.success('所选问题已上报到上级单位!');
            } else {
              this.$message.error(res.message);
            }
          },
        );
      } else {
        this.$message.warning('没有需要上报的问题!');
      }
    },
    openReassignRectifyUnitDialog() {
      if (this.sjdwUnderRectifyTableSetting.selectedPblms.length > 0) {
        this.showRectifyUnitDialog = true;
      } else {
        this.$message.warning('请选择需要重新指派整改单位的问题!');
      }
    },
    // 关闭下派单位选择窗口
    closeRectifyUnitDialog() {
      this.showRectifyUnitDialog = false;
    },
    saveRectifyUnitInfo(val) {
      this.showRectifyUnitDialog = false;
      if (this.sjdwUnderRectifyTableSetting.selectedPblms.length > 0) {
        for (
          let i = 0;
          i < this.sjdwUnderRectifyTableSetting.selectedPblms.length;
          i++
        ) {
          this.sjdwUnderRectifyTableSetting.selectedPblms[i].zgXianOrgId =
            val.zgXianOrgId;
          // this.sjdwUnderRectifyTableSetting.selectedPblms[i].zgXianOrgMemo = val.zgXianOrgMemo;
          this.sjdwUnderRectifyTableSetting.selectedPblms[i].auditidea =
            '重新指派整改单位 - ' + val.activityComment;
          this.sjdwUnderRectifyTableSetting.selectedPblms[i].flowType = 2; // 2=整改流程 1=问题处置流程
          this.sjdwUnderRectifyTableSetting.selectedPblms[i].isag = 2; // 重新指派整改单位
        }
        redistributePblmToSpecificOrg(
          this.sjdwUnderRectifyTableSetting.selectedPblms,
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$refs.sjdwPblmTable.clearSelection(); // 清空问题列表子组件中原有的选中的行
            this.$message.success('重新指派成功!');
            this.getUnderRectifyPblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },

    openActivityCommentDialog(isAg, e) {
      if (this.sjdwUnderRectifyTableSetting.selectedPblms.length > 0) {
        this.isAg = isAg;
        this.actName = e.currentTarget.innerText;
        this.showActivityCommentDialog = true;
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
    closeActivityCommentDialog() {
      this.showActivityCommentDialog = false;
    },
    saveActivityComment(val) {
      this.closeActivityCommentDialog();
      this.approveSelectedPblm(val.isAg, val.Comment);
    },
    // 驱动整改流程流转
    approveSelectedPblm(isAg, activityComment) {
      if (this.sjdwUnderRectifyTableSetting.selectedPblms.length > 0) {
        this.loading = true;
        approvedPblmRectify(
          formatPblms(
            this.sjdwUnderRectifyTableSetting.selectedPblms,
            isAg,
            activityComment,
          ),
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('操作已完成!');
            this.$refs.sjdwPblmTable.clearSelection(); // 清空原有的选中行信息
            this.getUnderRectifyPblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
    setBatchXh() {
      const params = [];
      if (
        JSON.stringify(this.sjdwUnderRectifyTableSetting.selectedPblms) !==
          '{}' &&
        this.sjdwUnderRectifyTableSetting.selectedPblms.length > 0
      ) {
        this.sjdwUnderRectifyTableSetting.selectedPblms.forEach((item) => {
          const obj = {
            isPdSche: item.isPdSche,
            pblmId: item.pblmId,
          };
          params.push(obj);
        });
        console.log(params, 'xxxxx');
        request.post('/bis/insp/zg/pblm/setBatchXh', params).then((res) => {
          if (res.success) {
            this.$message.success('归档销号成功!');
            this.getUnderRectifyPblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.info('请选择需要操作的问题!');
      }
    },
  },
};
</script>

<style lang='less' scoped>

</style>
