<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(0, $event)" :disabled="loading" size="small" type="primary"
              style="color:rgba(255,255,255,1);float:left;margin-right:10px;">退回修改</a-button>
            <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-col>
        </a-row> -->
        <pblm-table
          ref="ywccPblmTable"
          :pblmTableSetting="ywccPendingApproveTableSetting"
          @pageChange="handlePageChange"
          @sizeChange="handleSizeChange"
          @selectionChange="handleSelectionChange"
          @submitYwcs="submitYwcs" />
        <ActivityCommentNew
          v-if="showActivityCommentDialog"
          :isAg="isAg"
          :row="curRow"
          :activityName="actName"
          @saveActivityComment="saveActivityComment"
          @closeActivityCommentDialog="closeActivityCommentDialog" />
      </a-card>
    </a-layout-content>
  </a-layout>
</template>

<script>
import moment from 'moment';
import { formatDateTime2 } from '../../utils/gw_date';
import {
  approvedPblmRectify,
  dispatchPblmRectify,
  formatPblms,
  rejectPblmRectify,
  requestPblmListByQueryParams,
  requestPendingApprovePblm,
} from './Common/rectify_api';
import { formatDeadLineDate } from './Common/rectify_date';
import ActivityCommentNew from './Components/ActivityCommentNew.vue';
import pblmQueryTerms from './Components/pblmQueryTerms';
import pblmTable from './Components/pblmTable';
export default {
  name: 'YwccPendingApprovePblm',
  props: [],
  components: {
    pblmQueryTerms,
    pblmTable,
    ActivityCommentNew,
    // ActivityComment1
  },
  data() {
    return {
      moment,
      curRow: {},
      loading: false,
      currentWorkflowStat: '25', // 整改状态 25=责任处室（单位）待审核问题
      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: '25', // important 25=责任处室（单位）待审核问题
        schdYear: moment(new Date().getFullYear().toString()).format('YYYY'),
      },
      ywccPendingApproveTableSetting: {
        isYwcs: true,
        data: [], // 主数据源
        pagination: {
          pageSize: 10,
          pageNum: 1,
          pblmCount: 0,
        }, // 翻页相关配置项
        height: 500,
        currentPblmId: '', // 当前选中的问题的ID
        selectedPblms: {}, // 多选模式下选中的所有问题集合
        multiSelect: true, // 是否出现行复选框
        enablePblmRectifyInfo: false, // 操作栏是否有整改信息输入按钮
      },
      showActivityCommentDialog: false,
      showActivityCommentDialog1: false,
      isAg: 1, // 流程流转方向 0-正常向后 1=正常向前 2=列入调度清单
      actName: '', // 流程流转方向名称
    };
  },
  created() {
    this.queryParams.workflowStat = this.currentWorkflowStat; // important 使用当前流程状态码来获取对应的问题列表
    this.getPendingApprovePblm();
  },
  mounted() {
    this.$nextTick(function () {
      this.ywccPendingApproveTableSetting.height =
        document.getElementsByClassName('ant-tabs')[0].offsetHeight - 260;
    });
  },
  computed: {
    formatDateTime() {
      return formatDateTime2;
    },
    formatDeadLineDate() {
      return formatDeadLineDate;
    },
  },
  methods: {
    // 按指定条件获取待下发问题列表数据
    queryByParams(val) {
      this.queryParams = val.queryParams;
      this.queryParams.workflowStat = 25; // Important 25=责任处室（单位）待审核问题
      this.$refs.ywccPblmTable.clearSelection(); // 清空原有的选中行信息
      this.getPendingApprovePblm();
    },
    // 获取待下发问题列表数据
    getPendingApprovePblm() {
      this.loading = true;
      this.queryParams.pageSize =
        this.ywccPendingApproveTableSetting.pagination.pageSize;
      this.queryParams.pageNum =
        this.ywccPendingApproveTableSetting.pagination.pageNum;
      this.queryParams.dutyDeptRole = '1';
      requestPblmListByQueryParams(this.queryParams).then((res) => {
        this.loading = false;
        if (res.data !== null) {
          this.ywccPendingApproveTableSetting.data = res.data.list;
          /* this.ywccPendingApproveTableSetting.data.sort(function (x, y) {
             return x.zgDate - y.zgDate;
           }); */
          this.ywccPendingApproveTableSetting.pagination.pblmCount =
            res.data.total;
        } else {
          this.ywccPendingApproveTableSetting.data = [];
          this.ywccPendingApproveTableSetting.pagination.pblmCount = 0;
        }
      });
    },
    // 直接页数跳转
    handlePageChange(val) {
      this.ywccPendingApproveTableSetting.pagination.pageNum = val.pageNum;
      this.getPendingApprovePblm();
    },
    // 调整每页显示记录数
    handleSizeChange(val) {
      this.ywccPendingApproveTableSetting.pagination.pageSize = val.pageSize;
      this.getPendingApprovePblm();
    },
    // 选中行时获取所有已选中行
    handleSelectionChange(val) {
      this.ywccPendingApproveTableSetting.selectedPblms = val.selectedPblms;
    },

    // 执行送监督处归档操作
    approvedAllPblm() {
      if (this.ywccPendingApproveTableSetting.selectedPblms.length > 0) {
        approvedPblmRectify(
          this.ywccPendingApproveTableSetting.selectedPblms,
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('已送达监督处!');
            this.$refs.ywccPblmTable.clearSelection(); // 清空原有的选中行信息
            this.getPendingApprovePblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要送监督处归档的问题!');
      }
    },
    dispatchSelectedPblm() {
      if (this.ywccPendingApproveTableSetting.selectedPblms.length > 0) {
        dispatchPblmRectify(
          this.ywccPendingApproveTableSetting.selectedPblms,
        ).then((res) => {
          this.loading = false;
          this.$message.success('所选问题已放入调度清单!');
          this.$refs.ywccPblmTable.clearSelection(); // 清空原有的选中行信息
          this.getPendingApprovePblm();
        });
      } else {
        this.$message.warning('请选择需要放入调度清单的问题!');
      }
    },
    // 退回所选问题重新整改
    rejectSelectedPblm() {
      if (this.ywccPendingApproveTableSetting.selectedPblms.length > 0) {
        this.loading = true;
        rejectPblmRectify(
          this.ywccPendingApproveTableSetting.selectedPblms,
        ).then((res) => {
          this.loading = false;
          this.$message.success('所选问题已退回到原整改单位!');
          this.$refs.ywccPblmTable.clearSelection(); // 清空原有的选中行信息
          this.getPendingApprovePblm();
        });
      } else {
        this.$message.warning('请选择需要退回的问题!');
      }
    },
    submitYwcs(obj) {
      console.log(obj, 'submitYwcs.obj');
      this.curRow = obj.row;
      this.isAg = obj.isAg;
      this.actName = obj.e.currentTarget.innerText;
      this.showActivityCommentDialog = true;
    },
    openActivityCommentDialog(isAg, e) {
      if (this.ywccPendingApproveTableSetting.selectedPblms.length > 0) {
        this.isAg = isAg;
        this.actName = e.currentTarget.innerText;
        if (isAg == 0) {
          this.showActivityCommentDialog1 = true;
        } else {
          this.showActivityCommentDialog = true;
        }
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
    closeActivityCommentDialog() {
      this.showActivityCommentDialog = false;
    },
    closeActivityCommentDialog1() {
      this.showActivityCommentDialog1 = false;
    },
    saveActivityComment1(val) {
      this.closeActivityCommentDialog1();
      this.approveSelectedPblm1(val);
    },
    saveActivityComment(val) {
      this.closeActivityCommentDialog();
      this.approveSelectedPblm1(val);
    },
    approveSelectedPblm(isAg, activityComment) {
      if (this.ywccPendingApproveTableSetting.isYwcs) {
        this.ywccPendingApproveTableSetting.selectedPblms = [this.curRow];
      }
      if (this.ywccPendingApproveTableSetting.selectedPblms.length > 0) {
        this.loading = true;
        approvedPblmRectify(
          formatPblms(
            this.ywccPendingApproveTableSetting.selectedPblms,
            isAg,
            activityComment,
          ),
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('操作已完成!');
            this.$refs.ywccPblmTable.clearSelection(); // 清空原有的选中行信息
            this.getPendingApprovePblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
    // 驱动整改流程流转
    approveSelectedPblm1(activityComment) {
      console.log(activityComment, 'approveSelectedPblm1');
      if (this.ywccPendingApproveTableSetting.isYwcs) {
        this.ywccPendingApproveTableSetting.selectedPblms = [this.curRow];
      }
      if (this.ywccPendingApproveTableSetting.selectedPblms.length > 0) {
        this.loading = true;
        for (
          let i = 0;
          i < this.ywccPendingApproveTableSetting.selectedPblms.length;
          i++
        ) {
          this.ywccPendingApproveTableSetting.selectedPblms[i].isag =
            activityComment.isAg; // isAg=0 反向流转 isAg=1 正向流转
          this.ywccPendingApproveTableSetting.selectedPblms[i].flowType = 2; // 2=整改流程 1=问题处置流程
          this.ywccPendingApproveTableSetting.selectedPblms[i].auditidea =
            activityComment.Comment;
          this.ywccPendingApproveTableSetting.selectedPblms[i].isPdTmTemp =
            activityComment.isPdTmTemp
              ? moment(activityComment.isPdTmTemp).format('YYYY-MM-DD')
              : null;
          this.ywccPendingApproveTableSetting.selectedPblms[i].delayMTime =
            activityComment.delayMTime
              ? moment(activityComment.delayMTime).format('YYYY-MM-DD')
              : null;
          this.ywccPendingApproveTableSetting.selectedPblms[i].zgDelayEln =
            activityComment.zgDelayEln;
          this.ywccPendingApproveTableSetting.selectedPblms[i].deptagePdEln =
            activityComment.deptagePdEln;
          this.ywccPendingApproveTableSetting.selectedPblms[i].zgRedefineReq =
            activityComment.zgRedefineReq;
          this.ywccPendingApproveTableSetting.selectedPblms[i].zgDate =
            activityComment.zgDate
              ? moment(activityComment.zgDate).format('YYYY-MM-DD')
              : null;
          this.ywccPendingApproveTableSetting.selectedPblms[i].checkYTime =
            activityComment.checkYTime ? activityComment.checkYTime : null;
        }
        console.log(this.ywccPendingApproveTableSetting.selectedPblms);
        // return
        approvedPblmRectify(
          this.ywccPendingApproveTableSetting.selectedPblms,
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('操作已完成!');
            this.$refs.ywccPblmTable.closeDetail();
            this.$refs.ywccPblmTable.clearSelection(); // 清空原有的选中行信息
            this.getPendingApprovePblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
  },
};
</script>

<style lang='less' scoped>

</style>
