<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="sdccPblmTable"
          :pblmTableSetting="sdccPendingApproveTableSetting"
          @pageChange="handlePageChange"
          @sizeChange="handleSizeChange"
          @selectionChange="handleSelectionChange"
          @submitB="submitB"/>
        <activity-comment v-if="showActivityCommentDialog" :isAg="this.isAg" :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,
  requestPblmListByQueryParams,
  requestPendingApprovePblm,
} from './Common/rectify_api';
import { formatDeadLineDate } from './Common/rectify_date';
import ActivityComment from './Components/ActivityComment.vue';
import pblmQueryTerms from './Components/pblmQueryTerms';
import pblmTable from './Components/pblmTable';
export default {
  props: [],
  data() {
    return {
      moment,
      loading: false,
      currentWorkflowStat: '27', // 整改状态 27=监督处经办人审核
      queryParams: {
        hasVedio: '',
        adCode: '',
        pblmStat: '',
        inspPblmCate: '',
        ifCasePblm: '',
        nm: '',
        rsName: '',
        adFullName: '',
        startTime: '',
        endTime: '',
        objType: '', // 为空才能不区分督查类别
        inspPblmName: '',
        dataStat: '2',
        persId: localStorage.getItem('persId'),
        villType: '0',
        pageSize: 10,
        pageNum: 1,
        pblmTotal: 0,
        pTypeCode: '', // 督查类别
        pYear: '', // 年度
        zgXianOrgId: null, // 接收单位
        zgWh: '', // 整改文号
        workflowStat: '27', // 整改状态 important 27=监督处经办人审核
        schdYear: moment(new Date().getFullYear().toString()).format('YYYY'),
      },
      sdccPendingApproveTableSetting: {
        data: [], // 主数据源
        pagination: {
          pageSize: 10,
          pageNum: 1,
          pblmCount: 0,
        }, // 翻页相关配置项
        height: 500,
        currentPblmId: '', // 当前选中的问题的ID
        selectedPblms: {}, // 多选模式下选中的所有问题集合
        multiSelect: true, // 是否出现行复选框
        enablePblmRectifyInfo: false, // 操作栏是否有整改信息输入按钮
        isShowBtn: 2,
      },
      showActivityCommentDialog: false,
      isAg: 1, // 流程流转方向 0-正常向后 1=正常向前 2=列入调度清单
      actName: '', // 流程流转方向名称
    };
  },
  components: {
    pblmQueryTerms,
    pblmTable,
    ActivityComment,
  },
  created() {
    this.queryParams.workflowStat = this.currentWorkflowStat; // important 使用当前流程状态码来获取对应的问题列表
    this.getPendingApprovePblm();
  },
  mounted() {
    this.$nextTick(function () {
      this.sdccPendingApproveTableSetting.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;
      this.$refs.sdccPblmTable.clearSelection(); // 清空原有的选中行信息
      this.getPendingApprovePblm();
    },
    // 获取经办人待审核问题列表数据
    getPendingApprovePblm() {
      this.loading = true;
      this.queryParams.pageSize =
        this.sdccPendingApproveTableSetting.pagination.pageSize;
      this.queryParams.pageNum =
        this.sdccPendingApproveTableSetting.pagination.pageNum;
      requestPblmListByQueryParams(this.queryParams).then((res) => {
        this.loading = false;
        if (res.data !== null) {
          this.sdccPendingApproveTableSetting.data = res.data.list;
          /* this.sdccPendingApproveTableSetting.data.sort(function (x, y) {
             return x.zgDate - y.zgDate;
           }); */
          this.sdccPendingApproveTableSetting.pagination.pblmCount =
            res.data.total;
        } else {
          this.sdccPendingApproveTableSetting.data = [];
          this.sdccPendingApproveTableSetting.pagination.pblmCount = 0;
        }
      });
    },
    // 直接页数跳转
    handlePageChange(val) {
      this.sdccPendingApproveTableSetting.pagination.pageNum = val.pageNum;
      this.getPendingApprovePblm();
    },
    // 调整每页显示记录数
    handleSizeChange(val) {
      this.sdccPendingApproveTableSetting.pagination.pageSize = val.pageSize;
      this.getPendingApprovePblm();
    },
    // 选中行时获取所有已选中行
    handleSelectionChange(val) {
      this.sdccPendingApproveTableSetting.selectedPblms = val.selectedPblms;
    },
    submitB(obj) {
      this.sdccPendingApproveTableSetting.selectedPblms = [obj.row];
      if (obj.isAg != null && obj.e != null) {
        this.openActivityCommentDialog(obj.isAg, obj.e);
      }
    },
    openActivityCommentDialog(isAg, e) {
      if (this.sdccPendingApproveTableSetting.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.sdccPendingApproveTableSetting.selectedPblms.length > 0) {
        this.loading = true;
        for (
          let i = 0;
          i < this.sdccPendingApproveTableSetting.selectedPblms.length;
          i++
        ) {
          this.sdccPendingApproveTableSetting.selectedPblms[i].isag = isAg; // isAg=0 反向流转 isAg=1 正向流转
          this.sdccPendingApproveTableSetting.selectedPblms[i].flowType = 2; // 2=整改流程 1=问题处置流程
          this.sdccPendingApproveTableSetting.selectedPblms[i].auditidea =
            activityComment; // 操作意见
        }
        approvedPblmRectify(
          this.sdccPendingApproveTableSetting.selectedPblms,
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('操作已完成!');
            this.$refs.sdccPblmTable.clearSelection(); // 清空原有的选中行信息
            this.getPendingApprovePblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
  },
};
</script>

<style lang='less' scoped>

</style>
