<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">
      <a-form-item label="延期至" prop="isPdTmTemp" v-if="isAg==1 && (row.isDlyFlow=='1' || row.isDlyHeavy=='1')">
        <a-date-picker v-model:value="activityComment.isPdTmTemp" placeholder="选择日期" style="width: 100%;"/>
      </a-form-item>
      <a-form-item label="延期说明" prop="zgDelayEln" v-if="isAg==1 && (row.isDlyFlow=='1' || row.isDlyHeavy=='1')">
        <a-textarea v-model:value="activityComment.zgDelayEln" :rows="8" type="textarea" placeholder="延期说明" />
      </a-form-item>
      <a-form-item label="不同意销号原因" prop="deptagePdEln" v-if="isAg==0 && row.isDlyFlow!='1' && row.isDlyHeavy!='1'">
        <a-textarea v-model:value="activityComment.deptagePdEln" :rows="8" type="textarea" placeholder="不同意销号原因" />
      </a-form-item>
      <a-form-item label="重新明确整改要求" prop="zgRedefineReq" v-if="isAg==0 && row.isDlyFlow!='1' && row.isDlyHeavy!='1'">
        <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="row.isDlyFlow!='1' && row.isDlyHeavy!='1' && isAg==0 && isShowDate && (row.workflowStat == '25' || row.workflowStat == '26')">
        <a-date-picker v-model:value="activityComment.zgDate" placeholder="选择日期" style="width: 100%;" />
      </a-form-item>
      <a-form-item label="重新明确整改时限" prop="zgDate" v-else-if="(row.isDlyFlow == '1' && row.isDlyHeavy == '0') && isAg == 0 && row.workflowStat == '25' && isZgData">
        <a-date-picker v-model:value="activityComment.zgDate" 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>
    </a-form>
    <span slot="okText">提交</span>
  </a-modal>
</template>

<script>
import moment from 'moment';
export default {
  name: 'ActivityCommentDialog',
  props: {
    isAg: {
      type: Number,
      default: 1,
    },
    activityName: {
      type: String,
      default: '默认操作',
    },
    tag: {
      type: String,
      default: '',
    },
    row: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      isShowDate: false,
      activityComment: {
        isAg: this.isAg,
        isPdTmTemp: null,
        zgDelayEln: '',
        deptagePdEln: '',
        zgRedefineReq: '',
        zgDate: null,
        Comment: '',
      },
      showDialog: true,
      rules: {
        isPdTmTemp: [
          { required: true, message: '延期时间不能为空', trigger: 'blur' },
        ],
        deptagePdEln: [
          {
            required: true,
            message: '不同意销号原因不能为空',
            trigger: 'blur',
          },
        ],
        zgDate: [
          { required: true, message: '整改时限不能为空', trigger: 'blur' },
        ],
      },
      moment,
      isZgData: false,
    };
  },
  mounted() {
    console.log(this.row, 'row');
    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;
        }
      }
      console.log(this.isZgData, 'mounted');
      this.activityComment.isPdTmTemp = this.row.isPdTmTemp
        ? moment(this.row.isPdTmTemp)
        : null;
      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()
          ? moment(this.getCurrentMonthLast())
          : null;
        this.isShowDate = true;
      }
      // var compareDate = year+"-"+month+"-"+28;
      // var currentDate = year+"-"+month+"-"+day;
      // // if(currentDate<=compareDate){
      //     this.activityComment.zgDate = this.row.zgDate;
      // }else{
      //     if(month>11){
      //         year = year + 1;
      //         month = 1;
      //     }else{
      //         month = month + 1
      //     }
      //     var nextMonthDate = year+"-"+(month>9?month:'0'+month)+"-01"
      //     this.activityComment.zgDate = nextMonthDate
      // }
      // if(month>11){
      //     year = year + 1;
      //     month = 1;
      // }else{
      //     month = month + 1
      // }
      // var nextMonthDate = year+"-"+(month>9?month:'0'+month)+"-01"
      // if(this.row.zgDate){
      //     var zgYear = this.row.zgDate.substring(0,4);
      //     var zgMonth = this.row.zgDate.substring(5,7);
      //     if(parseInt(zgMonth)>11){
      //         zgYear = parseInt(zgYear) + 1;
      //         zgMonth = 1;
      //     }else{
      //         zgMonth = parseInt(zgMonth) + 1
      //     }
      //     this.activityComment.zgDate =  zgYear+"-"+(zgMonth>9?zgMonth:'0'+zgMonth)+"-01"
      //     if(this.activityComment.zgDate < nextMonthDate){
      //         this.activityComment.zgDate = nextMonthDate
      //     }
      // }else{
      //     this.activityComment.zgDate = nextMonthDate;
      // }
    }
  },
  methods: {
    getCurrentMonthLast() {
      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;
      }
      return date.getFullYear() + '-' + month + '-' + day;
    },
    closeActivityCommentDialog() {
      this.$emit('closeActivityCommentDialog');
    },
    saveActivityComment() {
      console.log(
        this.row.isDlyFlow,
        this.row.isDlyHeavy,
        this.isAg,
        this.row.workflowStat,
        this.isZgData,
        'saveActivityComment',
        this.activityComment.zgDate,
      );
      const comment = this.activityComment.Comment;
      if (this.row.isDlyFlow == '1' || this.row.isDlyHeavy == '1') {
        this.activityComment.deptagePdEln = '';
        this.activityComment.zgRedefineReq = '';
        if (
          this.row.workflowStat == '25' &&
          this.isAg == '0' &&
          ((this.row.isDlyFlow == '1' && this.row.isDlyHeavy == '1') ||
            (this.row.isDlyFlow == '1' && this.row.isDlyHeavy == '0'))
        ) {
          if (this.activityComment.zgDate && !this.activityComment.zgDate) {
            this.$message.warning('重新明确整改时限不能为空');
            return false;
          }
          this.activityComment.zgDate = moment(
            this.activityComment.zgDate,
          ).format('YYYY-MM-DD');
          this.activityComment.checkYTime = 'Y';
          this.activityComment.delayMTime = this.activityComment.zgDate;
        } else {
          this.activityComment.zgDate = null;
        }
        if (this.isAg == '0') {
          this.activityComment.isPdTmTemp = null;
          this.activityComment.zgDelayEln = '';
        } else {
          if (!this.activityComment.isPdTmTemp) {
            this.$message.warning('延期日期不能为空');
            return false;
          }
          this.activityComment.checkYTime = 'Y';
          this.activityComment.delayMTime = this.activityComment.isPdTmTemp;
        }
      } else {
        this.activityComment.isPdTmTemp = null;
        this.activityComment.zgDelayEln = '';
        if (this.isAg == '1') {
          this.activityComment.deptagePdEln = '';
          this.activityComment.zgRedefineReq = '';
          this.activityComment.zgDate = null;
        } else {
          if (!this.activityComment.deptagePdEln) {
            this.$message.warning('不同意销号原因不能为空');
            return false;
          }
          if (!this.activityComment.zgDate && this.isShowDate) {
            this.$message.warning('整改时限不能为空');
            return false;
          }
        }
      }

      if (this.activityComment.Comment === '' && this.row.isDlyFlow == '1') {
        if (this.activityComment.isAg === 1) {
          this.activityComment.Comment = '同意延期';
        } else {
          this.activityComment.Comment = '不同意延期';
        }
      } else {
        if (this.activityComment.isAg === 1) {
          this.activityComment.Comment = '同意';
        } else {
          this.activityComment.Comment = '不同意';
        }
      }

      if (this.activityComment.isAg == 1) {
        this.activityComment.Comment =
          this.activityName + ' - ' + this.activityComment.Comment;
      } else {
        if (this.row.isDlyFlow == '1' || this.row.isDlyHeavy == '1') {
          if (
            this.row.workflowStat == '25' &&
            this.row.isDlyFlow == '1' &&
            this.row.isDlyHeavy == '1'
          ) {
            this.activityComment.Comment =
              this.activityName +
              ' - ' +
              this.activityComment.Comment +
              ' - 重新明确至' +
              moment(this.activityComment.zgDate).format('YYYY-MM-DD');
          } else {
            this.activityComment.Comment =
              this.activityName + ' - ' + this.activityComment.Comment;
          }
        } else if (
          this.row.isDlyFlow == '0' &&
          this.row.isDlyHeavy == '0' &&
          (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 +
              ' - 重新明确至' +
              moment(this.activityComment.zgDate).format('YYYY-MM-DD');
          } else {
            this.activityComment.Comment =
              this.activityName + ' - ' + this.activityComment.deptagePdEln;
          }
        }
      }

      // this.activityComment.Comment = this.activityName + ' - ' + this.activityComment.Comment
      // 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');
      return;
      this.$emit('saveActivityComment', this.activityComment);
      // this.$refs.activityCommentForm.resetFields()
    },
  },
};
</script>

<style lang='less' scoped>

</style>
