<template>
  <!-- 附加页面 -->
  <div>
    <!-- 页面左半部分 -->
    <el-form
      :model="model"
      ref="modelForm"
      label-position="left"
      label-width="90px"
      size="small"
      :rules="rules"
      style="height:600px"
    >
      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="标准月数1" prop="month1">
            <c-input v-model="model.month1" maxlength="4" required placeholder="请输入"></c-input>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="标准月数2" prop="month2">
            <c-input v-model="model.month2" maxlength="4" required placeholder="请输入"></c-input>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="标准天数1" prop="days1">
            <c-input v-model="model.days1" maxlength="32" disabled></c-input>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="标准天数2" prop="days2">
            <c-input v-model="model.days2" maxlength="32" disabled></c-input>
          </el-form-item>
        </el-col>
      </el-col>

      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="价格1" prop="price1">
            <c-input-number
              v-model="model.price1"
              :max="999999999999999999.99"
              :precision="4"
              placeholder="请输入"
            ></c-input-number>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="价格2" prop="price2">
            <c-input-number
              v-model="model.price2"
              :max="999999999999999999.99"
              :precision="4"
              placeholder="请输入"
            ></c-input-number>
          </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"
              style="width:180px !important;"
            ></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"
              style="width:180px !important;"
            ></c-date-picker>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="周几" prop="weekday1">
            <c-input v-model="model.weekday1" disabled placeholder="请输入"></c-input>
          </el-form-item>
        </el-col>
      </el-col>

      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="周几" prop="weekday2">
            <c-input v-model="model.weekday2" disabled placeholder="请输入"></c-input>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="节假日" prop="holiday1">
            <c-input v-model="model.holiday1" disabled placeholder="请输入"></c-input>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="节假日" prop="holiday2">
            <c-input v-model="model.holiday2" disabled placeholder="请输入"></c-input>
          </el-form-item>
        </el-col>
      </el-col>

      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="实际天数" prop="realdays">
            <c-input v-model="model.realdays" required maxlength="4" placeholder="请输入"></c-input>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="12">
        <el-col :span="22">
          <el-form-item label="插值价" prop="guidePrice">
            <c-input-number
              v-model="model.guidePrice"
              :max="999999999999999999.9999"
              :precision="4"
              disabled
              placeholder="请输入"
            ></c-input-number>%
          </el-form-item>
        </el-col>
      </el-col>

      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="报价" prop="offer">
            <c-input-number
              v-model="model.offer"
              :max="999999999999999999.9999"
              :precision="4"
              placeholder="请输入"
            ></c-input-number>%
          </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"
                maxlength="100"
                :precision="4"
                :step="0.1"
                :max="99.9999"
                placeholder="请输入"
              ></c-input-number>%
            </div>
          </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="税后FTP" prop="taxBehindOffer">
            <c-input-number
              v-model="model.taxBehindOffer"
              :precision="4"
              disabled
              placeholder="请输入"
              :max="999999999999999999.9999"
            ></c-input-number>%
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="10">
        <el-col :span="22">
          <el-form-item label="收益点" prop="profit">
            <c-input-number v-model="model.profit" :precision="4" disabled placeholder="请输入"></c-input-number>%
          </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 {
      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: {
        month1: [
          {
            required: true
          }
        ],
        month2: [
          {
            required: true
          }
        ],
        price1: [
          {
            required: true
          }
        ],
        price2: [
          {
            required: true
          }
        ],
        realdays: [
          {
            required: true
          }
        ]
      }
    };
  },
  // props: ["model", "codes"],
  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);
        // this.model.holiday1 = rtnmsg.data;
        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.realdays = days;
        }
      }
      // });
    },
    //判断到期日是否是节假日
    async isholiday2() {
      // this.$refs.modelForm.validate(async valid => {
      //   if (!valid) return;
      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 = "是";
        }
        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.realdays = days;
        }
      }
      // });
    },
    //计算当前日期是周几
    getWeekNo(date) {
      let week = moment(date).day();
      if (week === 0) {
        debugger;
        return "7";
      } else {
        return week;
      }
    },
    //计算业务天数：获取两个字符串日期的天数差
    getBusinessDay(startDate, endDate) {
      let days = moment(endDate).diff(moment(startDate), "days");

      return days;
    }
  },

  computed: {
    //　标准天数=标准月数x30
    days1: function() {
      return this.model.month1 * 30;
    },
    days2() {
      return this.model.month2 * 30;
    },

    guidePrice() {
      //多出低档=实际天数-标准天数1
      let lowLevel = this.model.realdays - this.model.days1;
      //间隔天数=标准天数2-标准天数1
      let days = this.model.days2 - this.model.days1;
      let rate;
      //　多出占间距比例=多出低档/间隔天数
      if (days !== 0) {
        rate = lowLevel / days;
      } else {
        rate = 0;
      }

      //间隔价格：
      let invPrice;
      if (this.model.price2 > this.model.price1) {
        debugger;
        invPrice = this.model.price2 - this.model.price1;
      } else {
        invPrice = this.model.price1 - this.model.price2;
      }

      //　插值指导价=指导价1+间隔价格x多出占间距比例
      return this.model.price1 + invPrice * rate;
    },
    taxBehindOffer() {
      //税后报价=FTP*（1+税率）
      return (this.model.ftp )* (1 + this.model.tax/100);
    },
    profit() {
      //　收益点=税后报价-报价
      return  this.model.offer-this.taxBehindOffer;
    }
  },
  watch: {
    days1: function() {
      this.model.days1 = this.days1;
    },
    days2() {
      this.model.days2 = this.days2;
    },
    guidePrice() {
      this.model.guidePrice = this.guidePrice;
    },
    taxBehindOffer() {
      this.model.taxBehindOffer = this.taxBehindOffer;
    },
    profit() {
      this.model.profit = this.profit;
    }
  }
};
</script>