<template>
  <div class="panel panel-info auto">
    <div class="panel-heading auto" style="height: 30px">
      <h4 style="display:inline-block;margin-top: auto">增值信息</h4>
    </div>
    <div class="auto info-content" v-if="hasinfo == 1 ">
      <table class="table table-striped table-bordered" >
        <thead>
        <tr>  <th colspan="3" style="text-align: center;background-color: #8cbce2;">增值信息概况</th></tr>
        <tr>
          <td colspan="1" class="headers" >
            <span style="width:100%;height:90%;display: block;border-right: 1px solid #222;color:#222">产品</span>
          </td>
          <td colspan="1" class="headers">
            <span style="width:100%;height:90%;display: block;border-right: 1px solid #222;color:#222">
              购买时间
            </span>
          </td>
          <td colspan="1"  class="headers2" >
            <span style="width:100%;height:90%;display: block;color:#222">
              是否购买
            </span>
          </td>
        </tr>
        </thead>
        <template v-for="valadditem in valuedate">

          <tr  style="background:#deecf9;border-bottom: 8px solid white;">
            <td colspan="1"  style="line-height:30px;text-align: left;padding-left:35px;border: #E9E9E9 solid 1px;border-left:2px solid #E4EEFA">
              <span style="width:60%;height:90%;margin:0 auto;border-left:4px solid #5ac0d9;" >{{ valadditem.value_type }}</span>
            </td>
            <td colspan="1" class="staduadr" >{{  valadditem.startDate==''?'':valadditem.startDate.substring(0,10) }}</td>
            <td colspan="1" class="staduadr" >
              <span v-show="valadditem.isuse == '是'" class="checkboxk1"  >
                <span class="checkboxk2"></span>
              </span>
              <span v-show="valadditem.isuse == '否'" class="checkboxk1"  >
                <span class="checkboxk3"></span>
              </span>
            </td>
          </tr>
        </template>

      </table>
    </div>
    <div class="auto info-content" v-if="hasinfo === -1 && allhasinfo==0">
      <div class="row">
        <p>正在获取增值信息...</p>
      </div>
    </div>
    <div class="auto info-content" v-if="hasinfo === 0 && allhasinfo==0">
      <div class="row">
        <strong>暂无增值信息</strong>
      </div>
    </div>
    <modal v-if="showParame" :show.sync="showParame" v-ref:modal backdrop="false">
      <header slot="modal-header" class="modal-header">
        <h4 class="modal-title" style="text-align: center;">修改{{data.f_user_name}}[{{changeitem.name}}]增值业务</h4>
      </header>
      <article slot="modal-body" class="modal-body" style="height:65px;">
        <form class="form-horizontal" >
          <div class="form-group  col-sm-6">
            <label for="f_title" class="control-label">开始时间</label>
            <datepicker :format="'yyyy-MM-dd'" :value.sync="changeitem.startdate"
                        class="datepicker"
                        placeholder="开始时间"
                        style="width:60%"
                        v-model="changeitem.startdate"
            ></datepicker>
          </div>
          <div class="form-group  col-sm-6">
            <label for="f_reason" class="control-label">终止时间</label>
            <datepicker :format="'yyyy-MM-dd'" :value.sync="changeitem.enddate"
                        class="datepicker"
                        placeholder="终止时间"
                        style="width:60%"
                        v-model="changeitem.enddate"
            ></datepicker>
          </div>
        </form>
      </article>

      <footer slot="modal-footer" class="modal-footer">
        <button type="button" class="btn btn-success" @click='updateConfirm'>确认</button>
        <button type="button" class="btn btn-default" @click='updateclose'>取消</button>
      </footer>
    </modal>
  </div>

</template>
<script>
  import {HttpResetClass} from 'vue-client'

  let getvalueaddinfo = async function(self){
    try{
      let http = new HttpResetClass()
      let param = {
        f_userfiles_id: self.data.f_userfiles_id,
        f_userinfo_id: self.data.f_userinfo_id
      }
      if (self.data.f_olduserinfo_code) {
        param.f_olduserinfo_code = self.data.f_olduserinfo_code
      }
      let valueaddinfo = await http.load('POST', 'rs/logic/valueaddinfo', param, {resolveMsg: null, rejectMsg: null})
      if(valueaddinfo.data){
        self.hasinfo = 1;
        self.valuedate= valueaddinfo.data.valaddinfo;
        if(self.valuedate.length>0){
          self.valuedate.forEach(item=>{
            item.yearvalue = self.getdiffTime(item.startDate,item.endDate,'year');
          })
        }else{
          self.hasinfo = 0
        }

      }else{
        self.hasinfo = 0
      }
    }catch(e){
      self.hasinfo = 0;
      // self.$showAlert("获取增值信息失败",'warning',2000);
    }
  }


  export default{
    title:'增值信息',
    data(){
      return {
        model:{},
        hasinfo:-1,
        allhasinfo:0,
        showParame:false,
        oldchangeitem:{},
        changeitem:{},
        valuedate:[],
      }
    },
    props: ['data'],
    ready(){
      if(this.data && this.data.f_userinfo_id!=null){
        getvalueaddinfo(this)
      }else{
        this.hasinfo = 0;
      }
    },
    methods:{
      reflush(){
        if(this.data.f_userinfo_id!=null){
          getvalueaddinfo(this)
        }else{
          this.hasinfo = 0;
        }
      },
      changevalueadd(valueadditem){
        if(valueadditem.ishas =='1' && this.data.f_userinfo_id!=null &&  this.data.f_userinfo_id!='' ){
          this.showParame = true;
          let olditem = JSON.stringify(valueadditem);
          this.changeitem = JSON.parse(olditem);
          this.oldchangeitem = JSON.parse(olditem);
        }
      },
      updateConfirm(){
        if(this.changeitem.startdate> this.changeitem.enddate){
          this.$showAlert("结束时间不能小于开始时间",'warning',2000);
          return;
        }else{
          const param = {
            oldvalueitem:this.oldchangeitem,
            valueitem:this.changeitem,
            userinfo:this.data,
            f_operator: this.$login.f.name,
            f_operatorid: this.$login.f.id,
            f_orgid: this.$login.f.orgid,
            f_orgname: this.$login.f.orgs,
            f_depid: this.$login.f.depids,
            f_depname: this.$login.f.deps,
          }
          this.$resetpost('rs/logic/valueaddsave',{data:param}, {resolveMsg: null, rejectMsg: null}).then((res)=>{
            if(res.data){
              if(res.data.code==200){
                this.$showAlert('更新成功!','success',2000);
                this.updateclose();
                getvalueaddinfo(this);
              }else{
                this.$showAlert('更新失败!','warning',2000);
                this.updateclose();
              }
            }
          })
        }

      },
      updateclose(){
        this.changeitem = {};
        this.oldchangeitem = {};
        this.showParame = false;
      },
      //获取增值服务年限
      getdiffTime(d1,d2,models) {
        if(d1 == null || d1 == "" || d2 == null || d2 == "" || d1.indexOf("-")<0 || d2.indexOf("-")<0 ){
          return "无";
        }
        d1 = d1.split(" ")[0];
        d2 = d2.split(" ")[0];
        let day = 24 * 60 * 60 * 1000;
        if(models == 'year'){
          day = day*365;
        }
        try {
          let dateArr = d1.split("-");
          let checkDate = new Date();
          checkDate.setFullYear(dateArr[0], dateArr[1] - 1, dateArr[2]);
          let checkTime = checkDate.getTime();

          let dateArr2 = d2.split("-");
          let checkDate2 = new Date();
          checkDate2.setFullYear(dateArr2[0], dateArr2[1] - 1, dateArr2[2]);
          let checkTime2 = checkDate2.getTime();

          let cha = ((  checkTime2-checkTime) / day).toFixed(2);
          return cha;
        } catch (e) {
          return 0;
        }
      }
    },
    watch:{
      'data' () {
        if(this.data == null){
          this.hasinfo = 0;
          this.allhasinfo = 0;
          this.model = {};
        }else{
          getvalueaddinfo(this)
        }
      }
    }
  }


</script>
<style scoped>
  .img {
    border: 1px solid #9773ed;
    width: auto;
    height: auto;
    float: left;
    text-align: center;
    margin: 6px;
  }

  img {
    margin: 6px;
  }


  .container{
    margin: 10px auto;
    width: 80%;
    height: auto;
  }
  div::-webkit-scrollbar {
    width: 0 !important;
    height: 0 !important;
  }

  div{
    -ms-overflow-style: none;
    overflow: -moz-scrollbars-none;
  }
  .divmask {
    pointer-events: none;
  }
  .app-header {
    border-radius: 50%;
    background: rgba(73, 158, 223, 0.4);
    color: #FFF;
    padding: 20px;
    text-align: center;
    font-size: 1.2em;
    z-index: 10;
    opacity:0.5;
    bottom: 50px;
    margin-top: -20px;
    position: absolute;
    top: 10%;
    right: 2%;
    height: 8%;
  }
  .headers{
    text-align: center;
    background-color: white;
    color: #333;
    font-weight: 100;
    width: 33%;

    height: 15px;
    font-size: 14px;
  }
  .headers2{
    text-align: center;
    background-color: white;
    color: #333;
    font-weight: 100;
    width: 33%;
    height: 15px;
    font-size: 14px;
  }
  .staduadr{
    line-height:30px;text-align: center;border: #E9E9E9 solid 1px;
  }
  .checkboxk1{
    width: 50px;height: 22px;position: relative;border-radius: 20px;display: block;margin: 0 auto;
  }
  .checkboxk2{
    width: 24px;height: 24px;position: absolute;top: 0px;left: 15px;border-radius: 17px;display: block;
    background-image:url("../../../../../static/valueinfo1.png");
    background-size: 100%;
  }
  .checkboxk3{
    width: 24px;height: 24px;position: absolute;top: 0px;left: 15px;border-radius: 17px;display: block;
    background-image:url("../../../../../static/valueinfo0.png");background-size: 100%;
  }
</style>
