<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" :showAd="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="sjdwPblmTable"
          :pblmTableSetting="sjdwDoubleCheckTableSetting"
          @pageChange="handlePageChange"
          @sizeChange="handleSizeChange"
          @selectionChange="handleSelectionChange"
          @submitJc="submitJc" />
        <a-modal
          :open="showRectifyUnitDialog"
          @ok="approveSelectedPblmToYwcs(1, form.remark)"
          @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"
          :tag="tag"
          @saveActivityComment="saveActivityComment"
          @closeActivityCommentDialog="closeActivityCommentDialog" /> -->
        <ActivityCommentNew
          v-if="showActivityCommentDialog"
          :isAg="this.isAg"
          :row="curRow"
          :activityName="this.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,
  distributePblmToSpecificOrg,
  formatPblms,
  redistributePblmToSpecificOrg,
  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 pblmRectifyUnit from './Components/pblmRectifyUnit';
import pblmTable from './Components/pblmTable';
export default {
  name: 'SjdwPendingApprovePblm',
  props: [],
  data() {
    return {
      moment,
      curRow: {},
      tag: 'operator',
      loading: false,
      showRectifyUnitDialog: false,
      currentWorkflowStat: '26', // 整改状态 22-1=整改单位内部复核
      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: '26', // important 仅获取待市级单位审批的问题
        schdYear: moment(new Date().getFullYear().toString()).format('YYYY'),
      },
      sjdwDoubleCheckTableSetting: {
        adType: false,
        isJc: true,
        data: [], // 主数据源
        pagination: {
          pageSize: 10,
          pageNum: 1,
          pblmCount: 0,
        }, // 翻页相关配置项
        height: 500,
        currentPblmId: '', // 当前选中的问题的ID
        selectedPblms: {}, // 多选模式下选中的所有问题集合
        multiSelect: true, // 是否出现行复选框
        enablePblmRectifyInfo: false, // 操作栏是否有整改信息输入按钮
      },
      showActivityCommentDialog: false,
      isAg: 1, // 流程流转方向 0-正常向后 1=正常向前 2=列入调度清单
      actName: '', // 流程流转方向名称
    };
  },
  components: {
    pblmQueryTerms,
    pblmTable,
    pblmRectifyUnit,
    ActivityComment,
    ActivityCommentNew,
  },
  created() {
    this.queryParams.workflowStat = this.currentWorkflowStat; // important 使用当前流程状态码来获取对应的问题列表
    this.getDoubleCheckPblm();
  },
  mounted() {
    this.$nextTick(function () {
      this.sjdwDoubleCheckTableSetting.height =
        document.getElementsByClassName('ant-tabs')[0].offsetHeight - 260;
    });
  },
  methods: {
    // 按指定条件获取待下发问题列表数据
    queryByParams(val) {
      this.queryParams = val.queryParams;
      this.queryParams.workflowStat = this.currentWorkflowStat; // important 获取待市级单位审批的问题
      this.$refs.sjdwPblmTable.clearSelection(); // 清空原有的选中行信息
      this.getDoubleCheckPblm();
    },
    // 获取待下发问题列表数据
    getDoubleCheckPblm() {
      this.loading = true;
      this.queryParams.pageSize =
        this.sjdwDoubleCheckTableSetting.pagination.pageSize;
      this.queryParams.pageNum =
        this.sjdwDoubleCheckTableSetting.pagination.pageNum;
      requestPblmListByQueryParams(this.queryParams).then((res) => {
        this.loading = false;
        if (res.data !== null) {
          this.sjdwDoubleCheckTableSetting.data = res.data.list;
          /* this.sjdwDoubleCheckTableSetting.data.sort(function (x, y) {
             return x.zgDate - y.zgDate;
           }); */
          this.sjdwDoubleCheckTableSetting.pagination.pblmCount =
            res.data.total;
        } else {
          this.sjdwDoubleCheckTableSetting.data = [];
          this.sjdwDoubleCheckTableSetting.pagination.pblmCount = 0;
        }
      });
    },
    // 直接页数跳转
    handlePageChange(val) {
      this.sjdwDoubleCheckTableSetting.pagination.pageNum = val.pageNum;
      this.getDoubleCheckPblm();
    },
    // 调整每页显示记录数
    handleSizeChange(val) {
      this.sjdwDoubleCheckTableSetting.pagination.pageSize = val.pageSize;
      this.getDoubleCheckPblm();
    },
    // 选中行时获取所有已选中行
    handleSelectionChange(val) {
      this.sjdwDoubleCheckTableSetting.selectedPblms = val.selectedPblms;
    },

    openReassignRectifyUnitDialog() {
      if (this.sjdwDoubleCheckTableSetting.selectedPblms.length > 0) {
        this.showRectifyUnitDialog = true;
      } else {
        this.$message.warning('请选择需要重新指派整改单位的问题!');
      }
    },
    // 关闭下派单位选择窗口
    closeRectifyUnitDialog() {
      this.showRectifyUnitDialog = false;
    },
    saveRectifyUnitInfo(val) {
      this.showRectifyUnitDialog = false;
      if (this.sjdwDoubleCheckTableSetting.selectedPblms.length > 0) {
        for (
          let i = 0;
          i < this.sjdwDoubleCheckTableSetting.selectedPblms.length;
          i++
        ) {
          this.sjdwDoubleCheckTableSetting.selectedPblms[i].zgXianOrgId =
            val.zgXianOrgId;
          this.sjdwDoubleCheckTableSetting.selectedPblms[i].zgXianOrgMemo =
            val.zgXianOrgMemo;
          this.sjdwPendingRectifyTableSetting.selectedPblms[i].auditidea =
            '重新指派整改单位 - ' + val.activityComment;
          this.sjdwPendingRectifyTableSetting.selectedPblms[i].flowType = 2; // 2=整改流程 1=问题处置流程
          this.sjdwPendingRectifyTableSetting.selectedPblms[i].isag = 1; // 流程正向流转
        }
        redistributePblmToSpecificOrg(
          this.sjdwDoubleCheckTableSetting.selectedPblms,
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('重新指派成功!');
            this.$refs.sjdwPblmTable.clearSelection(); // 清空原有的选中行信息
            this.getDoubleCheckPblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
    openActivityCommentDialog(isAg, e) {
      if (this.sjdwDoubleCheckTableSetting.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.approveSelectedPblm1(val);
    },
    submitJc(obj) {
      this.curRow = obj.row;
      this.isAg = obj.isAg;
      this.actName = obj.e.currentTarget.innerText;
      this.showActivityCommentDialog = true;
    },
    approveSelectedPblm1(activityComment) {
      if (this.sjdwDoubleCheckTableSetting.isJc) {
        this.sjdwDoubleCheckTableSetting.selectedPblms = [this.curRow];
      }
      if (this.sjdwDoubleCheckTableSetting.selectedPblms.length > 0) {
        this.loading = true;
        for (
          let i = 0;
          i < this.sjdwDoubleCheckTableSetting.selectedPblms.length;
          i++
        ) {
          this.sjdwDoubleCheckTableSetting.selectedPblms[i].isag =
            activityComment.isAg; // isAg=0 反向流转 isAg=1 正向流转
          this.sjdwDoubleCheckTableSetting.selectedPblms[i].flowType = 2; // 2=整改流程 1=问题处置流程
          this.sjdwDoubleCheckTableSetting.selectedPblms[i].auditidea =
            activityComment.Comment;
          this.sjdwDoubleCheckTableSetting.selectedPblms[i].isPdTmTemp =
            activityComment.isPdTmTemp;
          this.sjdwDoubleCheckTableSetting.selectedPblms[i].zgDelayEln =
            activityComment.zgDelayEln;
          this.sjdwDoubleCheckTableSetting.selectedPblms[i].deptagePdEln =
            activityComment.deptagePdEln;
          this.sjdwDoubleCheckTableSetting.selectedPblms[i].zgRedefineReq =
            activityComment.zgRedefineReq;
          this.sjdwDoubleCheckTableSetting.selectedPblms[i].zgDate =
            activityComment.zgDate;

          this.sjdwDoubleCheckTableSetting.selectedPblms[i].checkYTime =
            activityComment.checkYTime;
          this.sjdwDoubleCheckTableSetting.selectedPblms[i].delayMTime =
            activityComment.delayMTime;
        }
        approvedPblmRectify(
          this.sjdwDoubleCheckTableSetting.selectedPblms,
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('操作已完成!');
            this.getDoubleCheckPblm();
            this.$refs.sjdwPblmTable.closeDetail();
            this.$refs.sjdwPblmTable.clearSelection();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
    // 驱动整改流程流转
    approveSelectedPblm(isAg, activityComment) {
      if (this.sjdwDoubleCheckTableSetting.selectedPblms.length > 0) {
        this.loading = true;
        approvedPblmRectify(
          formatPblms(
            this.sjdwDoubleCheckTableSetting.selectedPblms,
            isAg,
            activityComment,
          ),
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('操作已完成!');
            this.$refs.sjdwPblmTable.clearSelection(); // 清空原有的选中行信息
            this.getDoubleCheckPblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
  },
};
</script>

<style lang='less' scoped>

</style>
