<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="ywccDoubleCheckTableSetting"
          @pageChange="handlePageChange"
          @sizeChange="handleSizeChange"
          @selectionChange="handleSelectionChange"
          @submitYwcs="submitYwcs" />
        <activity-comment-new
          v-if="showActivityCommentDialog"
          :isAg="this.isAg"
          :activityName="this.actName"
          :row="curRow"
          @saveActivityComment="saveActivityComment"
          @closeActivityCommentDialog="closeActivityCommentDialog" />
        <activity-comment v-if="showActivityCommentDialog1" :isAg="this.isAg" :activityName="this.actName" @saveActivityComment="saveActivityComment1" @closeActivityCommentDialog="closeActivityCommentDialog1" />
      </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 ActivityComment from './Components/ActivityComment.vue';
import ActivityCommentNew from './Components/ActivityCommentNew.vue';
import pblmQueryTerms from './Components/pblmQueryTerms';
import pblmTable from './Components/pblmTable';
export default {
  name: 'YwccDoubleCheckPblm',
  props: [],
  components: {
    pblmQueryTerms,
    pblmTable,
    ActivityComment,
    ActivityCommentNew,
  },
  data() {
    return {
      moment,
      loading: false,
      currentWorkflowStat: '25-1', // 整改状态 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-1', // important 25-1=责任处室（单位）内部复核
        schdYear: moment(new Date().getFullYear().toString()).format('YYYY'),
      },
      ywccDoubleCheckTableSetting: {
        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: '', // 流程流转方向名称
      curRow: {},
    };
  },
  created() {
    this.queryParams.workflowStat = this.currentWorkflowStat; // important 使用当前流程状态码来获取对应的问题列表
    this.getDoubleCheckPblm();
  },
  mounted() {
    this.$nextTick(function () {
      this.ywccDoubleCheckTableSetting.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 = this.currentWorkflowStat; // Important
      this.$refs.ywccPblmTable.clearSelection(); // 清空原有的选中行信息
      this.getDoubleCheckPblm();
    },
    // 获取待复核问题列表数据
    getDoubleCheckPblm() {
      this.loading = true;
      this.queryParams.pageSize =
        this.ywccDoubleCheckTableSetting.pagination.pageSize;
      this.queryParams.pageNum =
        this.ywccDoubleCheckTableSetting.pagination.pageNum;
      this.queryParams.dutyDeptRole = '1';
      requestPblmListByQueryParams(this.queryParams).then((res) => {
        this.loading = false;
        if (res.data !== null) {
          this.ywccDoubleCheckTableSetting.data = res.data.list;
          /* this.ywccDoubleCheckTableSetting.data.sort(function (x, y) {
            return x.zgDate - y.zgDate;
          }); */
          this.ywccDoubleCheckTableSetting.pagination.pblmCount =
            res.data.total;
        } else {
          this.ywccDoubleCheckTableSetting.data = [];
          this.ywccDoubleCheckTableSetting.pagination.pblmCount = 0;
        }
      });
    },
    // 直接页数跳转
    handlePageChange(val) {
      this.ywccDoubleCheckTableSetting.pagination.pageNum = val.pageNum;
      this.getDoubleCheckPblm();
    },
    // 调整每页显示记录数
    handleSizeChange(val) {
      this.ywccDoubleCheckTableSetting.pagination.pageSize = val.pageSize;
      this.getDoubleCheckPblm();
    },
    // 选中行时获取所有已选中行
    handleSelectionChange(val) {
      this.ywccDoubleCheckTableSetting.selectedPblms = val.selectedPblms;
    },

    openActivityCommentDialog(isAg, e) {
      console.log(e, 111);
      if (this.ywccDoubleCheckTableSetting.selectedPblms.length > 0) {
        this.isAg = isAg;
        this.actName = e.currentTarget.innerText;
        this.showActivityCommentDialog1 = true;
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
    closeActivityCommentDialog() {
      this.showActivityCommentDialog = false;
    },
    closeActivityCommentDialog1() {
      this.showActivityCommentDialog1 = false;
    },
    saveActivityComment(val) {
      this.closeActivityCommentDialog();
      this.approveSelectedPblm1(val);
    },
    saveActivityComment1(val) {
      this.closeActivityCommentDialog1();
      this.approveSelectedPblm(val.isAg, val.Comment);
    },
    // 驱动整改流程流转
    approveSelectedPblm(isAg, activityComment) {
      if (this.ywccDoubleCheckTableSetting.selectedPblms.length > 0) {
        this.loading = true;
        approvedPblmRectify(
          formatPblms(
            this.ywccDoubleCheckTableSetting.selectedPblms,
            isAg,
            activityComment,
          ),
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('操作已完成!');
            this.$refs.ywccPblmTable.clearSelection(); // 清空原有的选中行信息
            this.getDoubleCheckPblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
    submitYwcs(obj) {
      console.log(obj, 'submitYwcs111');
      this.curRow = obj.row;
      this.isAg = obj.isAg;
      this.actName = obj.e.currentTarget.innerText;
      this.showActivityCommentDialog = true;
    },
    // 驱动整改流程流转
    approveSelectedPblm1(activityComment) {
      if (this.ywccDoubleCheckTableSetting.isYwcs) {
        this.ywccDoubleCheckTableSetting.selectedPblms = [this.curRow];
      }
      if (this.ywccDoubleCheckTableSetting.selectedPblms.length > 0) {
        this.loading = true;
        for (
          let i = 0;
          i < this.ywccDoubleCheckTableSetting.selectedPblms.length;
          i++
        ) {
          this.ywccDoubleCheckTableSetting.selectedPblms[i].isag =
            activityComment.isAg; // isAg=0 反向流转 isAg=1 正向流转
          this.ywccDoubleCheckTableSetting.selectedPblms[i].flowType = 2; // 2=整改流程 1=问题处置流程
          this.ywccDoubleCheckTableSetting.selectedPblms[i].auditidea =
            activityComment.Comment;
          this.ywccDoubleCheckTableSetting.selectedPblms[i].isPdTmTemp =
            activityComment.isPdTmTemp
              ? moment(activityComment.isPdTmTemp).format('YYYY-MM-DD')
              : null;
          this.ywccDoubleCheckTableSetting.selectedPblms[i].delayMTime =
            activityComment.delayMTime
              ? moment(activityComment.delayMTime).format('YYYY-MM-DD')
              : null;
          this.ywccDoubleCheckTableSetting.selectedPblms[i].zgDelayEln =
            activityComment.zgDelayEln;
          this.ywccDoubleCheckTableSetting.selectedPblms[i].deptagePdEln =
            activityComment.deptagePdEln;
          this.ywccDoubleCheckTableSetting.selectedPblms[i].zgRedefineReq =
            activityComment.zgRedefineReq;
          this.ywccDoubleCheckTableSetting.selectedPblms[i].zgDate =
            activityComment.zgDate
              ? moment(activityComment.zgDate).format('YYYY-MM-DD')
              : null;
          this.ywccDoubleCheckTableSetting.selectedPblms[i].checkYTime =
            activityComment.checkYTime ? activityComment.checkYTime : null;
        }
        approvedPblmRectify(
          this.ywccDoubleCheckTableSetting.selectedPblms,
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('操作已完成!');
            this.$refs.ywccPblmTable.closeDetail();
            this.$refs.ywccPblmTable.clearSelection(); // 清空原有的选中行信息
            this.ywccDoubleCheckTableSetting.selectedPblms = [];
            this.getDoubleCheckPblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
  },
};
</script>

<style lang='less' scoped>

</style>
