<template>
  <div class="auto">
    <validator name='v' style="overflow-y: auto">
      <form class="form-horizontal">
        <div class="row">
          <div class="col-sm-6  form-group">
            <label class="font_normal_body">上期保费截止日期</label>
            <input type="text" v-model="model.f_ins_stop_date" disabled=disabled style="width: 60%"
                   class="input_search">
          </div>
          <div class="col-sm-6  form-group">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;费用类型</label>
            <input type="text" v-model="model.f_fee_type" placeholder="保险收费" disabled=disabled style="width: 60%"
                   class="input_search">
          </div>
          <div class="col-sm-6  form-group">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;&emsp;投保人</label>
            <input type="text" v-model="model.f_user_name"  style="width: 60%"
                   class="input_search">
          </div>
          <div class="col-sm-6  form-group">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;&emsp;身份证</label>
            <input type="text" v-model="model.f_idnumber"  style="width: 60%"
                   class="input_search">
          </div>
          <div class="col-sm-6  form-group">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;电话</label>
            <input type="text" v-model="model.f_user_phone"  style="width: 60%"
                   class="input_search">
          </div>
          <div class="col-sm-6  form-group">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;购买日期</label>
            <datepicker id="f_buy_date" placeholder="本次保费开始日期" style="width:60%"
                        v-model="model.f_buy_date"
                        :value.sync="model.f_buy_date"
                        :format="'yyyy-MM-dd HH:mm:ss'"
                        :show-reset-button="true">
            </datepicker>
          </div>
          <div class="col-sm-6  form-group" :class="[$v.f_ins_start_date.required ? 'has-error' : '']" v-if="model.f_state === '待执行'">
            <label class="font_normal_body">本次保费开始日期</label>
            <input type="text" v-show="false" v-model="model.f_ins_start_date">
            <datepicker id="f_ins_start_date" placeholder="本次保费开始日期" style="width:60%"
                        v-model="model.f_ins_start_date"
                        :value.sync="model.f_ins_start_date"
                        :format="'yyyy-MM-dd HH:mm:ss'"
                        :show-reset-button="true">
            </datepicker>
          </div>
          <div class="col-sm-6  form-group" :class="[$v.f_ins_start_date.required ? 'has-error' : '']" v-else="model.f_state !== '待执行'">
            <label class="font_normal_body">本次保费开始日期</label>
            <input type="text" v-show="false" v-model="model.f_ins_start_date"
                   v-validate:f_ins_start_date='{required: true }'>
            <datepicker id="f_ins_start_date" placeholder="本次保费开始日期" style="width:60%"
                        v-model="model.f_ins_start_date"
                        :value.sync="model.f_ins_start_date"
                        :format="'yyyy-MM-dd HH:mm:ss'"
                        :show-reset-button="true">
            </datepicker>
          </div>
          <div class="col-sm-6  form-group"  v-if="model.f_state ==='待执行'"  :class="[$v.f_salecount.required? 'has-error' : '']">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;购买年限</label>
              <input type="number" v-model="model.f_salecount" placeholder="购买年限" style="width: 60%"
                   class="input_search" v-validate:f_salecount='{required: true }' v-el:style v-next-el="inpipe">
          </div>
          <div class="col-sm-6  form-group" v-if="model.f_state !=='待执行'">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;购买年限</label>
              <input type="number" v-model="model.f_salecount" placeholder="购买年限" style="width: 60%"
                   class="input_search" v v-el:style v-next-el="inpipe">
          </div>
          <div class="col-sm-6  form-group" :class="[$v.f_expiration_date2.required ? 'has-error' : '']" v-if="model.f_state === '待执行'">
          <input type="text" v-show="false" v-model="tmp" :value.sync='model.f_expiration_date'  v-el:f_expiration_date2>
          <label class="font_normal_body">&emsp;&emsp;保费结束日期</label>
          <datepicker id="f_expiration_date" placeholder="保费结束日期" style="width:60%"
                      @con-bulr="isShowSaleCount"
                      v-ref:f_expiration_date
                      v-model="model.f_expiration_date"
                      :value.sync="model.f_expiration_date"
                      :format="'yyyy-MM-dd HH:mm:ss'"
                      :show-reset-button="true">
          </datepicker>
        </div>
          <div class="col-sm-6  form-group" :class="[$v.f_expiration_date2.required ? 'has-error' : '']" v-else="model.f_state !== '待执行'">
            <input type="text" v-show="false" v-model="tmp" :value.sync='model.f_expiration_date' v-validate:f_expiration_date2='{required: true }' v-el:f_expiration_date2>
            <label class="font_normal_body">&emsp;&emsp;保费结束日期</label>
            <datepicker id="f_expiration_date" placeholder="保费结束日期" style="width:60%"
                        @con-bulr="isShowSaleCount"
                        v-ref:f_expiration_date
                        v-model="model.f_expiration_date"
                        :value.sync="model.f_expiration_date"
                        :format="'yyyy-MM-dd HH:mm:ss'"
                        :show-reset-button="true">
            </datepicker>
          </div>
          <div class="col-sm-6  form-group" :class="[$v.f_insurance_type.required ? 'has-error' : '']">
            <label class="font_normal_body" title="参数:险种">&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;险种</label>
            <input type="text" v-show="false" v-model="model.f_insurance_type"
                   v-validate:f_insurance_type='{required: true }'>
            <v-select
              placeholder='险种'
              :value.sync="model.f_insurance_type"
              style="width:60%"
              :value-single="true"
              v-model="model.f_insurance_type"
              :options='insurancetype'
              @change="getmoney()"
              close-on-select>
            </v-select>
          </div>
          <div class="col-sm-6  form-group" :class="[$v.f_state.required ? 'has-error' : '']">
            <label class="font_normal_body" title="参数:保险状态">&emsp;&emsp;&emsp;&emsp;保险状态</label>
            <input type="text" v-show="false" v-model="model.f_state"
                   v-validate:f_state='{required: true }'>
            <v-select
              placeholder='保险状态'
              :value.sync="model.f_state"
              style="width:60%"
              :value-single="true"
              v-model="model.f_state"
              :options='insurancestate'
              close-on-select>
            </v-select>
          </div>
          <div class="col-sm-6  form-group" :class="[$v.f_money.required ? 'has-error' : '']">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;保费金额</label>
            <input type="number" v-model="model.f_money" class="input_search" style="width:60%"
                   v-validate:f_money='{required: true, dctest: [0, ">=" ] }' placeholder="保费金额">
          </div>

          <div class="col-sm-6" v-show="false">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;公司 </label>
            <right-tree @re-res="getRes"></right-tree>
          </div>
          <div class="col-sm-6">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;部门</label>
            <res-select restype='department'
                        :is-mul="mul"
                        @res-select="getdep"
                        :parentresid="depresid"
                        :initresid='depid'>
            </res-select>

          </div>
          <div class="col-sm-6">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;人员</label>
            <res-select restype='user'
                        @res-select="getuser"
                        :is-mul="mul"
                        :parentresid="userresid"
                        :initresid='operatorid'>
            </res-select>
          </div>
          <div class="col-sm-6  form-group">
            <label class="font_normal_body">&emsp;&emsp;&emsp;确认书编号</label>
            <input type="text" v-model="model.f_ok_number"  style="width: 60%"
                   class="input_search">
          </div>
          <div class="col-sm-12  form-group">
            <label class="font_normal_body">&emsp;&emsp;&emsp;&emsp;备注信息</label>
            <input type="textarea" v-model="model.f_comments" style="width: 80%;height: 50px" class="input_search">
          </div>
        </div>
      </form>
      <div style="float: right;padding-top:20px">
        <button class="button_search" @click="save" :disabled="!$v.valid">保存</button>
        <button class="button_clear" @click="cancel">取消</button>
      </div>
    </validator>
  </div>
</template>
<script>
  import Vue from 'vue'

  export default {
    title: '新增保险',
    data () {
      return {
        insurancetype: this.$appdata.getParam('险种'),
        insurancestate: this.$appdata.getParam('保险状态'),
        f_saleCount: {
          dctest: true
        },
        mul: false,
        tmp: '',
        depresid: [this.$login.f.depids],
        userresid: [this.$login.f.id],
        f_orgid: this.$login.f.orgid,
        f_depid: this.$login.f.depids,
        f_operatorid: this.$login.f.id,
        operatorid: [this.$login.f.id],
        depid: [this.$login.f.depids],
        orgname: '',
        depname: '',
        operatorname: '',
      }
    },
    props: {
      model: Object,
      row: Object
    },
    ready () {
      console.log('----------------row', this.row)
      if (this.row) {
        if (JSON.stringify(this.row) != '{}') {
          this.model = {
            f_userinfo_id: this.row.f_userinfo_id,
            f_ins_stop_date: this.row.f_ins_stop_date,
            f_user_name: this.row.f_user_name,
            f_idnumber: this.row.f_idnumber,
            f_user_phone: this.row.f_user_phone,
            f_saleCount: this.row.f_saleCount,
            f_userinfo_code: this.row.f_userinfo_code,
            f_last_insexpiration_date: this.row.f_ins_stop_date,
          }
        }
      }
      this.model.f_buy_date =  this.$login.toStandardTimeString()
    },
    watch: {
      'model.f_ins_start_date' (val) {
        if (val && this.model.f_saleCount) {
          let tx = new Date(val)
          let tmp = {
            year: tx.getFullYear(),
            month: tx.getMonth() + 1,
            day: tx.getDate()
          }
          let tmpdate = new Date((tmp.year + (this.model.f_saleCount - 0)) + '-' + (tmp.month < 10 ? '0' + tmp.month : tmp.month) + '-' + (tmp.day < 10 ? '0' + tmp.day : tmp.day))
          let date
          if (tmp.month < (tmpdate.getMonth() + 1)) {
            date = new Date(tmpdate.getTime() - 24 * 60 * 60 * 1000)
          } else {
            date = tmpdate
          }
          let jt = {
            year: date.getFullYear(),
            month: date.getMonth() + 1,
            day: date.getDate()
          }
          this.$refs.f_expiration_date.value = jt.year + '-' + (jt.month < 10 ? '0' + jt.month : jt.month) + '-' + (jt.day < 10 ? '0' + jt.day : jt.day)
        }
      },
      'model.f_money' (val) {
        if (val && (val < 0 || Math.floor(val - 0) !== (val - 0))) {
          this.model.f_money = 0
        }
      },
      'model.f_saleCount' (val) {
        if (val && (val < 0 || Math.floor(val - 0) !== (val - 0))) {
          this.model.f_saleCount = 0
          this.f_saleCount.dctest = false
        } else if (val && this.model.f_ins_start_date) {
          let tx = new Date(this.model.f_ins_start_date)
          let tmp = {
            year: tx.getFullYear(),
            month: tx.getMonth() + 1,
            day: tx.getDate()
          }
          let tmpdate = new Date((tmp.year + (val - 0)) + '-' + (tmp.month < 10 ? '0' + tmp.month : tmp.month) + '-' + (tmp.day < 10 ? '0' + tmp.day : tmp.day))
          let date
          if (tmp.month < (tmpdate.getMonth() + 1)) {
            date = new Date(tmpdate.getTime() - 24 * 60 * 60 * 1000)
          } else {
            date = tmpdate
          }
          let jt = {
            year: date.getFullYear(),
            month: date.getMonth() + 1,
            day: date.getDate()
          }
          this.$refs.f_expiration_date.value = jt.year + '-' + (jt.month < 10 ? '0' + jt.month : jt.month) + '-' + (jt.day < 10 ? '0' + jt.day : jt.day)
          this.f_saleCount.dctest = true
        } else {
          this.f_saleCount.dctest = true
        }
      }
    },
    methods: {
      getRes (obj) {
        this.orgname = obj.res[0]
        this.depresid = obj.resids
        this.f_orgid = obj.resids
        this.userresid = obj.resids
      },
      getdep (obj, val) {
        this.depname = val
        // this.userresid = obj
        this.f_depid = obj
      },
      getuser ( obj, val) {
        this.operatorname = val
        this.f_operatorid = obj
      },
      valids (val1, val2) {
        console.log(val1, val2)

        if (!(val1.valid && val2)) {
          return true
        } else {
          return false
        }
      },
      isShowSaleCount (oldVal) {
        console.log(oldVal)
        if (this.model.f_expiration_date) {
          this.model.f_saleCount = ''
        }
      },
      getmoney () {
        let val = this.$appdata.getSingleValue(this.model.f_insurance_type)
        this.money = val || 0
      },
      cancel () {
        this.$info('取消操作')
        this.$dispatch('refresh', this.row)
        this.$dispatch('close')
      },
      async save () {
        let saveState = true
        if (this.model.f_state !== '待执行') {
          if (Date.parse(this.model.f_ins_start_date) > Date.parse(this.model.f_expiration_date)) {
            this.$showAlert('结束日期不能早于开始日期', 'warning', 2000)
            saveState = false
          } else if (this.model.f_ins_start_date !== null && this.model.f_ins_start_date !== '' && Date.parse(this.model.f_ins_stop_date) > Date.parse(this.model.f_ins_start_date)) {
            this.$showAlert('开始日期不能早于上期保费截止日期', 'warning', 2000)
            saveState = false
          }
        }
        if (saveState) {
          this.model.f_total_ins_charge = this.model.f_total_ins_charge == null ? 0 : this.model.f_total_ins_charge
          this.model.f_operat_type = '保险'
          this.model.f_operator = this.operatorname[0]
          this.model.f_operatorid = this.f_operatorid[0]
          this.model.f_orgid = this.$login.f.orgid
          this.model.f_orgname = this.$login.f.orgs
          this.model.f_depid = this.f_depid[0]
          this.model.f_depname = this.depname[0]
          this.model.f_fee_type = '保险收费'
          this.model.f_last_insexpiration_date = this.model.f_ins_stop_date
          await
            this.$resetpost('rs/logic/saveInsInfo', {data: this.model}).then((res) => {
              this.$showAlert('新增保险成功', 'success', 2000)
            })
          this.$dispatch('close')
          this.$dispatch('success')
        }
      }
    }
  }
</script>
