<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="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="openReassignRectifyUnitDialog"
              :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="sjdwPendingRectifyTableSetting"
          @pageChange="handlePageChange"
          @sizeChange="handleSizeChange"
          @selectionChange="handleSelectionChange" />
        <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" />
      </a-card>
    </a-layout-content>
  </a-layout>
</template>

<script>
import moment from 'moment';
import { formatDateTime2 } from '../../utils/gw_date';
import {
  approvedPblmRectify,
  distributePblmToSpecificOrg,
  redistributePblmToSpecificOrg,
  requestPendingRectifyPblm,
} from './Common/rectify_api';
import { formatDeadLineDate } from './Common/rectify_date';
import ActivityComment from './Components/ActivityComment.vue';
import pblmDetail from './Components/pblmDetail.vue';
import pblmQueryTerms from './Components/pblmQueryTerms';
import pblmRectifyUnit from './Components/pblmRectifyUnit';
import pblmTable from './Components/pblmTable';
export default {
  name: 'SjdwPendingRectifyPblm',
  props: [],
  components: {
    pblmQueryTerms,
    pblmTable,
    pblmRectifyUnit,
    ActivityComment,
    pblmDetail,
  },
  data() {
    return {
      moment,
      PendingRectifyPblm: [], // 待下发问题列表
      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: '20', // 整改状态
        schdYear: moment(new Date().getFullYear().toString()).format('YYYY'),
      },
      distPblm: [], // 带整改文号的下发整改问题列表
      showRectifyUnitDialog: false,
      sjdwPendingRectifyTableSetting: {
        adType: false,
        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: '', // 流程流转方向名称
      JSONArr: '',
    };
  },
  created() {
    this.getPendingRectifyPblm();
  },
  mounted() {
    this.$nextTick(function () {
      this.sjdwPendingRectifyTableSetting.height =
        document.getElementsByClassName('ant-tabs')[0].offsetHeight - 260;
    });
  },
  computed: {
    formatDateTime() {
      return formatDateTime2;
    },
    formatDeadLineDate() {
      return formatDeadLineDate;
    },
  },
  methods: {
    getOk() {
      this.$refs.rectifyUnitInfoDialog.saveRectifyUnitInfo();
    },
    // 按指定条件获取待下发问题列表数据
    queryByParams(val) {
      this.queryParams = val.queryParams;
      this.queryParams.workflowStat = 20;
      this.$refs.sjdwPblmTable.clearSelection(); // 清空问题列表子组件中原有的选中的行
      this.getPendingRectifyPblm();
    },
    // 获取待下发问题列表数据
    getPendingRectifyPblm() {
      this.loading = true;
      this.queryParams.pageSize =
        this.sjdwPendingRectifyTableSetting.pagination.pageSize;
      this.queryParams.pageNum =
        this.sjdwPendingRectifyTableSetting.pagination.pageNum;
      requestPendingRectifyPblm(this.queryParams).then((res) => {
        this.loading = false;
        if (res.data !== null) {
          this.sjdwPendingRectifyTableSetting.data = res.data.list;
          this.sjdwPendingRectifyTableSetting.data.sort(
            (x, y) => x.zgDate - y.zgDate,
          );
          this.sjdwPendingRectifyTableSetting.pagination.pblmCount =
            res.data.total;
        } else {
          this.sjdwPendingRectifyTableSetting.data = [];
          this.sjdwPendingRectifyTableSetting.pagination.pblmCount = 0;
        }
      });
    },
    // 直接页数跳转
    handlePageChange(val) {
      this.sjdwPendingRectifyTableSetting.pagination.pageNum = val.pageNum;
      this.getPendingRectifyPblm();
    },
    // 调整每页显示记录数
    handleSizeChange(val) {
      this.sjdwPendingRectifyTableSetting.pagination.pageSize = val.pageSize;
      this.getPendingRectifyPblm();
    },
    // 选中行时获取所有已选中行
    handleSelectionChange(val) {
      this.sjdwPendingRectifyTableSetting.selectedPblms = val.selectedPblms;
      this.JSONArr = JSON.stringify(val.selectedPblms);
    },
    openActivityCommentDialog(isAg, e) {
      if (this.sjdwPendingRectifyTableSetting.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.assignSelectedPblmToRectifyUnit(val.Comment);
    },
    // 分配整改单位
    assignSelectedPblmToRectifyUnit(activityComment) {
      if (this.sjdwPendingRectifyTableSetting.selectedPblms.length > 0) {
        this.distPblm = [];
        let actUnitType = 'auto';
        if (activityComment.indexOf('指定为本单位整改') >= 0) {
          actUnitType = 'owner';
        }
        for (
          let i = 0;
          i < this.sjdwPendingRectifyTableSetting.selectedPblms.length;
          i++
        ) {
          if (actUnitType === 'owner') {
            // this.sjdwPendingRectifyTableSetting.selectedPblms[i].zgXianOrgId = this.sjdwPendingRectifyTableSetting.selectedPblms[i].adCode; //整改单位为市级单位本身
            this.sjdwPendingRectifyTableSetting.selectedPblms[i].zgXianOrgId =
              localStorage.getItem('currentAdCode'); // 指定为单位整改  adCode设置为登录人code
          } else {
            this.sjdwPendingRectifyTableSetting.selectedPblms[i].zgXianOrgId =
              JSON.parse(this.JSONArr)[i].adCode;
          }
          // else {
          //   this.sjdwPendingRectifyTableSetting.selectedPblms[i].zgXianOrgId = '361002000000'; //临时使用
          // }
          this.sjdwPendingRectifyTableSetting.selectedPblms[i].isag = 1; // 流程正向流转
          this.sjdwPendingRectifyTableSetting.selectedPblms[i].flowType = 2; // 2=整改流程 1=问题处置流程
          this.sjdwPendingRectifyTableSetting.selectedPblms[i].auditidea =
            activityComment; // 操作意见
        }
        distributePblmToSpecificOrg(
          this.sjdwPendingRectifyTableSetting.selectedPblms,
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('所选问题已提交到整改单位开始整改!');
            this.$refs.sjdwPblmTable.clearSelection(); // 清空问题列表子组件中原有的选中的行
            this.getPendingRectifyPblm();
            this.sjdwPendingRectifyTableSetting.selectedPblms = [];
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
    // 打开手动分配整改单位
    openReassignRectifyUnitDialog() {
      if (this.sjdwPendingRectifyTableSetting.selectedPblms.length > 0) {
        this.showRectifyUnitDialog = true;
      } else {
        this.$message.warning('请选择需要重新指派整改单位的问题!');
      }
    },
    // 关闭下派单位选择窗口
    closeRectifyUnitDialog() {
      this.showRectifyUnitDialog = false;
    },
    saveRectifyUnitInfo(val) {
      this.showRectifyUnitDialog = false;
      if (this.sjdwPendingRectifyTableSetting.selectedPblms.length > 0) {
        for (
          let i = 0;
          i < this.sjdwPendingRectifyTableSetting.selectedPblms.length;
          i++
        ) {
          this.sjdwPendingRectifyTableSetting.selectedPblms[i].zgXianOrgId =
            val.zgXianOrgId;
          this.sjdwPendingRectifyTableSetting.selectedPblms[i].zgXianOrgMemo =
            val.activityComment;
          this.sjdwPendingRectifyTableSetting.selectedPblms[i].isag = 1; // 流程正向流转
          this.sjdwPendingRectifyTableSetting.selectedPblms[i].flowType = 2; // 2=整改流程 1=问题处置流程
          this.sjdwPendingRectifyTableSetting.selectedPblms[i].auditidea =
            '手动分配整改单位 - ' + val.activityComment; // 操作意见
        }
        redistributePblmToSpecificOrg(
          this.sjdwPendingRectifyTableSetting.selectedPblms,
        ).then((res) => {
          if (res.success) {
            this.loading = false;
            this.$message.success('重新指派成功!');
            this.$refs.sjdwPblmTable.clearSelection(); // 清空原有的选中行信息
            this.getPendingRectifyPblm();
          } else {
            this.$message.error(res.message);
          }
        });
      } else {
        this.$message.warning('请选择需要操作的问题!');
      }
    },
  },
};
</script>

<style lang='less' scoped>

</style>
