<template>
  <!-- 附加页面 -->
  <div>
    <el-form
      :model="model"
      ref="modelForm"
      label-position="left"
      label-width="70px"
      size="small"
      :rules="rules"
      style="height:600px"
    >
      <!-- 页面左半部分 -->
      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="本金" prop="prin">
            <c-input-number v-model="model.prin" placeholder="请输入" :precision="4"></c-input-number>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="利率" prop="intRate">
            <div>
              <c-input-number v-model="model.intRate" :precision="2" :max="99.99" placeholder="请输入"></c-input-number>%
            </div>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="起息日" prop="bgnIntDay">
            <c-date-picker
              v-model="model.bgnIntDay"
              type="date"
              placeholder="选择日期"
              :picker-options="bgnIntDay"
              @blur="isholiday1()"
              :append-to-body="false"
            ></c-date-picker>
          </el-form-item>
        </el-col>
      </el-col>

      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="到期日" prop="dueDate">
            <c-date-picker
              v-model="model.dueDate"
              type="date"
              placeholder="选择日期"
              :picker-options="dueDate"
              @blur="isholiday2"
              :append-to-body="false"
            ></c-date-picker>
          </el-form-item>
        </el-col>
      </el-col>

      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="周几" prop="weekday">
            <c-input v-model="model.weekday1" maxlength="32" placeholder disabled></c-input>
            <!-- :style="{'width':'80px', 'margin':'0px','padding' :'0px'}" -->
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="周几" prop="weekday">
            <c-input v-model="model.weekday2" maxlength="32" placeholder disabled></c-input>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="10">
        <el-col :span="20">
          <el-form-item label="节假日" prop="holiday">
            <c-input v-model="model.holiday1" maxlength="32" placeholder disabled></c-input>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="12">
        <el-col :span="20">
          <el-form-item label="节假日" prop="holiday">
            <c-input v-model="model.holiday2" maxlength="32" placeholder disabled></c-input>
          </el-form-item>
        </el-col>
      </el-col>

      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="业务天数" prop="busiDayNum">
            <c-input
              v-model="model.busiDayNum"
              maxlength="32"
              placeholder
              disabled
              @blur="calculateBusinessDays()"
            ></c-input>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="年度天数" prop="annlDayNum">
            <c-input v-model="model.annlDayNum" maxlength="100" placeholder="请输入"></c-input>
          </el-form-item>
        </el-col>
      </el-col>

      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="付息" prop="payInt">
            <c-input-number v-model="model.payInt" :precision="4" placeholder disabled></c-input-number>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="应付金额" prop="payblAmt">
            <c-input-number v-model="model.payblAmt" :precision="4" placeholder disabled></c-input-number>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="FTP" prop="ftp">
            <div>
              <c-input-number v-model="model.ftp" :precision="4" :max="99.9999" placeholder="选输"></c-input-number>%
            </div>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="税率" prop="tax">
            <div>
              <c-input-number
                v-model="model.tax"
                placeholder="选输"
                :precision="4"
                :step="0.1"
                :max="99.9999"
              ></c-input-number>%
            </div>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="收益点" prop="profit">
            <div>
              <c-input-number v-model="model.profit" :precision="4" placeholder disabled></c-input-number>%
            </div>
          </el-form-item>
        </el-col>
      </el-col>
    </el-form>
  </div>
</template>

<script>
import moment from "moment";
import Api from "~/service/Api";
import Calculator from "./Calculator";
export default {
  data() {
    return {
      month: moment(new Date()).format("YYYYMMDD"),
    bgnIntDay: {
        // 结束日期大于开始日期
        disabledDate: time => {
          return time.getTime() > moment(this.model.dueDate);
        }
      },
      dueDate: {
        // 结束日期大于开始日期
        disabledDate: time => {
          return time.getTime() < moment(this.model.bgnIntDay);
        }
      },
      model: new Calculator().data,
      rules: {
        prin: [
          {
            required: true,
            trigger: "blur"
          }
        ],
        intRate: [
          {
            required: true,
            message: "输入有误",
            trigger: "blur"
          }
        ],
        bgnIntDay: [
          {
            required: true
          }
        ],
        dueDate: [
          {
            required: true
          }
        ],
        annlDayNum: [
          {
            required: true
          }
        ]
      }
    };
  },

  methods: {
    //判断起息日是否是节假日
    async isholiday1() {
      // this.$refs.modelForm.validate(async valid => {
      // if (!valid) return;
      if (this.model.bgnIntDay !== null) {
        this.model.queryDate=this.model.bgnIntDay;
        let rtnmsg =await Api.post("v1/pm/isholiday", this.model);
        if (rtnmsg.data.isHolidayFlag=="0") {
          this.model.holiday1 = "否";
        } else {
          this.model.holiday1 = "是";
        }
        this.model.weekday1 = this.getWeekNo(this.model.bgnIntDay);
        if (this.model.bgnIntDay != null && this.model.dueDate != null) {
          let days = this.getBusinessDay(
            this.model.bgnIntDay,
            this.model.dueDate
          );
          this.model.busiDayNum = days;
        }
      }
      // });
    },
    //判断到期日是否是节假日
    async isholiday2() {
      // this.$refs.modelForm.validate(async valid => {
        // if (valid) {
           if (this.model.dueDate !== null) {
        this.model.queryDate=this.model.dueDate;
        let rtnmsg = await  Api.post("v1/pm/isholiday", this.model);
        // this.model.holiday2 = rtnmsg.data; //
        if (rtnmsg.data.isHolidayFlag=="0") {
          this.model.holiday2 = "否";
        } else {
          this.model.holiday2 = "是";
        }
        console.log(rtnmsg.msg);
        this.model.weekday2 = this.getWeekNo(this.model.dueDate);
        if (this.model.bgnIntDay != null && this.model.dueDate != null) {
          let days = this.getBusinessDay(
            this.model.bgnIntDay,
            this.model.dueDate
          );
          this.model.busiDayNum = days;
        }
      }
        // }
     
      // });
    },
    //计算当前日期是周几
    getWeekNo(date) {
      let week = moment(date).day();
      if (week === 0) {
        return "7";
      } else {
        return week;
      }
    },

    //计算业务天数:获取两个字符串日期的天数差
    getBusinessDay(startDate, endDate) {
      let days = moment(endDate).diff(moment(startDate), "days");

      return days;
    }
  },

  //计算:付息=本金x(利率/100/年度天数)x(到息日-起息日)
  computed: {
    payInt: function() {
      if (this.model.annlDayNum !== 0) {
        let time =
          this.model.prin *
          (this.model.intRate / 100 / this.model.annlDayNum) *
          this.model.busiDayNum;
        return time;
        console.log(time);
      }
    },
    //计算:应付金额=本金+付息
    payblAmt: function() {
      return this.model.prin + this.model.payInt;
    },
    //监听利率的变化,计算:收益点=利率/(1+税点)-FTP
    profit: function() {
      return (
        (this.model.intRate / 100 / (1 + this.model.tax / 100) -
          this.model.ftp / 100) *
        100
      );
    }
  },

  watch: {
    profit: function() {
      this.model.profit = this.profit;
    },
    payInt: function() {
      this.model.payInt = this.payInt;
    },
    payblAmt: function() {
      this.model.payblAmt = this.payblAmt;
    },
    busiDayNum: function() {
      this.model.busiDayNum = this.busiDayNum;
    }
  }
};
</script>