<template>
  <a-modal
    v-model="showDialog"
    @ok="saveActivityComment"
    @cancel="closeActivityCommentDialog"
    title="操作意见"
    width="60%"
    class="outerDialog custom_dialog">
    <a-form :model="activityComment" :rules="rules" :label-col="{span: 8}" :wrapper-col="{span: 15}" ref="activityCommentForm">

      <!-- 重新明确时限流程 -->
      <template v-if="isAg==1 && row.isDlyFlow=='1' && row.isDlyHeavy=='1'">
        <a-form-item label="重新明确至" prop="isPdTmTemp">
          <a-date-picker v-model:value="activityComment.isPdTmTemp" format="YYYY-MM-DD" value-format="YYYY-MM-DD" placeholder="选择日期" style="width: 100%;"/>
        </a-form-item>
        <!-- 省级业务处室审核人 25-1 -->
        <a-form-item v-if="row.workflowStat == '25-1'" label="意见" prop="Comment">
          <a-textarea v-model:value="activityComment.Comment" :rows="8" type="textarea" placeholder="意见" />
        </a-form-item>
      </template>

      <!-- 同意延期 -->
      <template v-if="isAg==1 && row.isDlyFlow=='1' && row.isDlyHeavy=='0'">
        <a-form-item label="延期至" prop="isPdTmTemp">
          <a-date-picker v-model:value="activityComment.isPdTmTemp" format="YYYY-MM-DD" value-format="YYYY-MM-DD" placeholder="选择日期" style="width: 100%;"/>
        </a-form-item>
        <a-form-item label="延期说明" prop="zgDelayEln">
          <a-textarea v-model:value="activityComment.zgDelayEln" :rows="8" type="textarea" placeholder="延期说明" />
        </a-form-item>
        <a-form-item label="意见" prop="Comment">
          <a-textarea v-model:value="activityComment.Comment" :rows="8" type="textarea" placeholder="意见" />
        </a-form-item>
      </template>

      <!-- 不同意延期 -->
      <template v-if="isAg==0 && row.isDlyFlow=='1' && row.isDlyHeavy=='0'">
        <a-form-item label="意见" prop="Comment">
          <a-textarea v-model:value="activityComment.Comment" :rows="8" type="textarea" placeholder="意见" />
        </a-form-item>
      </template>

      <!-- 同意销号 -->
      <template v-if="isAg==1 && row.isDlyFlow=='0' && row.isDlyHeavy=='0'">
        <a-form-item label="意见" prop="Comment">
          <a-textarea v-model:value="activityComment.Comment" :rows="8" type="textarea" placeholder="意见" />
        </a-form-item>
      </template>

      <!-- 不同意销号 -->
      <template v-if="isAg==0 && row.isDlyFlow=='0' && row.isDlyHeavy=='0'">
        <a-form-item label="不同意销号原因" prop="deptagePdEln">
          <a-textarea v-model:value="activityComment.deptagePdEln" :rows="8" type="textarea" placeholder="不同意销号原因" />
        </a-form-item>
        <a-form-item label="重新明确整改要求" prop="zgRedefineReq">
          <a-textarea v-model:value="activityComment.zgRedefineReq" :rows="8" type="textarea" placeholder="重新明确整改要求" />
        </a-form-item>
        <a-form-item label="整改时限" prop="zgDate" :disabled="isShowDate" v-if="isShowDate && (row.workflowStat == '25' || row.workflowStat == '26')">
          <a-date-picker v-model:value="activityComment.zgDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD" placeholder="选择日期" style="width: 100%;" />
        </a-form-item>
        <a-form-item label="意见" prop="Comment">
          <a-textarea v-model:value="activityComment.Comment" :rows="8" type="textarea" placeholder="意见" />
        </a-form-item>
      </template>

    </a-form>
    <span slot="okText">提交</span>
  </a-modal>
</template>

<script>
import moment from 'moment';
import request from '@/utils/gw_ajax_request';
export default {
  name: 'ActivityCommentDialog',
  props: {
    isAg: {
      type: Number,
      default: 1,
    },
    activityName: {
      type: String,
      default: '默认操作',
    },
    tag: {
      type: String,
      default: '',
    },
    row: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      moment,
      isShowDate: false,
      activityComment: {
        isAg: this.isAg,
        isPdTmTemp: null,
        zgDelayEln: '',
        Comment: '',
        deptagePdEln: '',
        zgRedefineReq: '',
        zgDate: null,
      },
      rules: {
        isPdTmTemp: [
          { required: true, message: '时间不能为空', trigger: 'blur' },
        ],
        deptagePdEln: [
          {
            required: true,
            message: '不同意销号原因不能为空',
            trigger: 'blur',
          },
        ],
        zgDate: [
          { required: true, message: '整改时限不能为空', trigger: 'blur' },
        ],
      },
      showDialog: true,
      isZgData: false,
      msgData: [],
    };
  },
  created() {
    this.getBizMsg();
  },
  mounted() {
    this.isShowDate = false;
    if (this.row.isDlyFlow == '1' || this.row.isDlyHeavy == '1') {
      // 如果当前业务处室经办人 且 是退回延期申请 且 整改时限小于当前时限
      if (this.row.workflowStat == '25' && this.row.isDlyFlow == '1') {
        const zgDate1 = new Date(this.row.zgDate).getTime();
        const zgDate2 = new Date().getTime();
        if (zgDate1 < zgDate2) {
          this.isZgData = true;
        } else {
          this.isZgData = false;
        }
      }
      this.activityComment.isPdTmTemp = this.row.isPdTmTemp
        ? this.row.isPdTmTemp
        : this.getZgDateMonthLast();
      this.activityComment.zgDelayEln = this.row.zgDelayEln;
    } else {
      var date = new Date();
      var year = date.getFullYear();
      var month =
        date.getMonth() + 1 > 9
          ? date.getMonth() + 1
          : '0' + (date.getMonth() + 1);
      var day = date.getDate() > 9 ? date.getDate() : '0' + date.getDate();
      var curDateStr = year + '-' + month + '-' + day;
      var date1 = new Date(this.row.zgDate.substring(0, 7));
      var date2 = new Date(year + '-' + month);
      if (date1.getTime() < date2.getTime()) {
        this.activityComment.zgDate = this.getCurrentMonthLast()
          ? this.getCurrentMonthLast()
          : null;
        this.isShowDate = true;
      }
    }
    console.log(
      this.row.isDlyFlow,
      this.row.isDlyHeavy,
      this.isAg,
      this.row.workflowStat,
      this.isZgData,
      'saveActivityComment',
      this.activityComment.zgDate,
    );
  },
  methods: {
    // 获取不同意延期 意见 匹配
    getBizMsg() {
      request.get('/bis/insp/zg/pblm/getBizMsg').then((res) => {
        this.msgData = res.data;
      });
    },
    // 获取当前月底
    getCurrentMonthLast() {
      const date = new Date();
      let currentMonth = date.getMonth();
      const nextMonth = ++currentMonth;
      const nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
      const oneDay = 1000 * 60 * 60 * 24;
      const lastTime = new Date(nextMonthFirstDay - oneDay);
      let month = parseInt(lastTime.getMonth() + 1);
      let day = lastTime.getDate();
      if (month < 10) {
        month = '0' + month;
      }
      if (day < 10) {
        day = '0' + day;
      }
      return date.getFullYear() + '-' + month + '-' + day;
    },
    // 获取整改时限日期
    getZgDateMonthLast() {
      var date = new Date();
      var currentMonth = date.getMonth();
      var nextMonth = ++currentMonth;
      var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
      var oneDay = 1000 * 60 * 60 * 24;
      var lastTime = new Date(nextMonthFirstDay - oneDay);
      var month = parseInt(lastTime.getMonth() + 1);
      var day = lastTime.getDate();
      if (month < 10) {
        month = '0' + month;
      }
      if (day < 10) {
        day = '0' + day;
      }

      var format = '';
      if (day - new Date().getDate() > 11) {
        format = date.getFullYear() + '-' + month + '-' + day;
      } else {
        var date1 = new Date(
          new Date(date.getFullYear() + '-' + month + '-' + day).getTime() +
            24 * 60 * 60 * 1000,
        );
        date1.setMonth(date1.getMonth() + 1);
        date1.setDate(0);
        format =
          date1.getFullYear() +
          '-' +
          (date1.getMonth() + 1) +
          '-' +
          date1.getDate();
      }

      return format;
    },
    closeActivityCommentDialog() {
      this.$emit('closeActivityCommentDialog');
    },
    saveActivityComment() {
      let comment = this.activityComment.Comment;

      // 重新明确整改时限流程 业务处室
      if (
        this.isAg == '1' &&
        this.row.isDlyFlow == '1' &&
        this.row.isDlyHeavy == '1'
      ) {
        if (!this.activityComment.isPdTmTemp) {
          this.$message.warning('时间不能为空');
          return false;
        }
        this.activityComment.checkYTime = 'Y';
        this.activityComment.delayMTime = this.activityComment.isPdTmTemp;
        this.activityComment.zgDate = null;
        if (this.row.workflowStat == '25-1') {
          if (this.activityComment.Comment == '') {
            comment = '同意';
          }
          this.activityComment.Comment = this.activityName + ' - ' + comment;
        } else {
          this.activityComment.Comment = this.activityName;
        }
      }

      // 同意延期流程
      if (
        this.isAg == 1 &&
        this.row.isDlyFlow == '1' &&
        this.row.isDlyHeavy == '0'
      ) {
        if (!this.activityComment.isPdTmTemp) {
          this.$message.warning('时间不能为空');
          return false;
        }
        this.activityComment.checkYTime = 'Y';
        this.activityComment.delayMTime = this.activityComment.isPdTmTemp;
        this.activityComment.zgDate = null;
        if (this.activityComment.Comment == '') {
          this.activityComment.Comment = this.activityName;
        } else {
          this.activityComment.Comment = this.activityName + ' - ' + comment;
        }
      }

      // 不同意延期
      if (
        this.isAg == 0 &&
        this.row.isDlyFlow == '1' &&
        this.row.isDlyHeavy == '0'
      ) {
        if (this.activityComment.Comment == '') {
          this.$message.warning('意见不能为空');
          return false;
        }
        const index = this.msgData.indexOf(this.activityComment.Comment);
        if (index > 0) {
          return this.$message.warning('请详细描述不同意延期原因!');
        }
        this.activityComment.isPdTmTemp = null;
        this.activityComment.zgDelayEln = '';
        this.activityComment.deptagePdEln = '';
        this.activityComment.zgRedefineReq = '';
        this.activityComment.Comment = this.activityName + ' - ' + comment;
      }

      // 同意销号
      if (
        this.isAg == 1 &&
        this.row.isDlyFlow == '0' &&
        this.row.isDlyHeavy == '0'
      ) {
        this.activityComment.isPdTmTemp = null;
        this.activityComment.zgDelayEln = '';
        this.activityComment.deptagePdEln = '';
        this.activityComment.zgRedefineReq = '';
        this.activityComment.zgDate = null;
        if (this.activityComment.Comment == '') {
          this.activityComment.Comment = this.activityName + ' - ' + '同意';
        } else {
          this.activityComment.Comment = this.activityName + ' - ' + comment;
        }
      }

      // 不同意销号
      if (
        this.isAg == 0 &&
        this.row.isDlyFlow == '0' &&
        this.row.isDlyHeavy == '0'
      ) {
        this.activityComment.isPdTmTemp = null;
        this.activityComment.zgDelayEln = '';
        if (!this.activityComment.deptagePdEln) {
          this.$message.warning('不同意销号原因不能为空');
          return false;
        }
        if (!this.activityComment.zgDate && this.isShowDate) {
          this.$message.warning('整改时限不能为空');
          return false;
        }
        if (
          this.row.workflowStat == '25-1' ||
          this.row.workflowStat == '26-1'
        ) {
          this.activityComment.Comment =
            this.activityName + ' - ' + this.activityComment.deptagePdEln;
          this.activityComment.zgDate = null;
        } else {
          if (this.isShowDate) {
            this.activityComment.Comment =
              this.activityName +
              ' - ' +
              this.activityComment.deptagePdEln +
              ' - 重新明确至' +
              this.activityComment.zgDate;
          } else {
            this.activityComment.Comment =
              this.activityName + ' - ' + this.activityComment.deptagePdEln;
          }
        }

        if (
          this.row.workflowStat == '25' ||
          this.row.workflowStat == '25-1' ||
          this.row.workflowStat == '26' ||
          this.row.workflowStat == '26-1'
        ) {
          this.activityComment.deptagePdEln = comment;
        }
      }

      console.log(this.activityComment, 'activityComment');
      this.$emit('saveActivityComment', this.activityComment);
    },
  },
};
</script>
