<template>
  <div>
    <p class="edit_page_title">{{pageTitle}}</p>
    <Form ref="honorWelfareData" :model="honorWelfareData" :rules="honorWelfareRule" :label-width="90">
      <div style="width:670px">
        <FormItem label="福利名称" prop="welfareName">
          <Input type="text" v-model="honorWelfareData.welfareName" class="wid_275" placeholder="请填写福利名称"></Input>
        </FormItem>
        <FormItem label="指标" prop="required">
          <Input type="text" v-model="honorWelfareData.required" class="wid_275" placeholder="请填写指标"></Input>
        </FormItem>
        <FormItem label="有效期" prop="honor_num">
          <Date-picker type="date" :value='honorWelfareData.start_time' class="wid_132" :options="bg_data" placeholder="开始时间" @on-change='bgdatechange'></Date-picker> - 
          <Date-picker type="date" :value='honorWelfareData.end_time' class="wid_132" :options="ed_data" placeholder="结束时间" @on-change='eddatechange'></Date-picker>
          <p class="form_tap_text">不填默认为永久有效</p>
          <div class="clear"></div>
        </FormItem>
        <FormItem class="form_btn">
          <Button type="primary" @click="editHonorWelfare('honorWelfareData')">确认</Button>
          <Button class="return_btn mar_L_15" @click="return_last">取消</Button>
        </FormItem>
      </div>
    </Form>
  </div>
</template>
<script>
import { getNewtoday,changeUnixtimedate } from "@/assets/js/common";
export default {
  name: "edit_honor_welfare",
  props: {
    isWelfareType:String,
    welfareTypeId:Number,
    pindex:Number,
  },
  components: {},
  data() {
    return {
      bg_data:{},
      ed_data:{},
      honorWelfareData:{
        welfareName:'',
        required:'',
        start_time:'',
        end_time:'',
      },
      honorWelfareRule:{
        welfareName:{ required: true, message: "请填写福利名称", trigger: "blur"},
        required:{ required: true, message: "请填写指标", trigger: "blur"}
      },
    };
  },
  computed:{
    pageTitle(){
      return this.isWelfareType=='edit'?'编辑福利':'添加新福利'
    }
  },
  watch: {
    'honorWelfareData.start_time'(n){
      if(n){
        let datetime=getNewtoday(n)
        this.ed_data={
          disabledDate: date => {
            return date && date.valueOf() <  new Date(datetime) ;
          }
        }
      }else{
        this.ed_data={}
      }
    },
    'honorWelfareData.end_time'(n){
      if(n){
        this.bg_data={
          disabledDate: date => {
            return date && date.valueOf() >  new Date(n);
          }
        }
      }else{
        this.bg_data={}
      }
    },
  },
  methods: {
    return_last() {
      this.$emit('return_last',this.pindex)
    },
    changetimedate(date){
      let n=date.toString()
      let datetime = new Date(n)
      return Date.parse(datetime) / 1000
    },
    editHonorWelfare(formname){
      this.$refs[formname].validate(valid=>{
        if(valid){
          let api=this.isWelfareType=='edit'?'editHonorWelfare':'addHonorWelfare'
          let start=this.changetimedate(this.honorWelfareData.start_time)
          let end=this.changetimedate(this.honorWelfareData.end_time)
          let params={
            name:this.honorWelfareData.welfareName,
            required:this.honorWelfareData.required,
            start_time:start,
            end_time:end,
          }
          this.isWelfareType == 'edit'? params.id=this.welfareTypeId:''
          this.$api[api]({...params}).then(data =>{
            if(data.code==200){
              this.$Message.info(data.msg)
              this.$emit('return_last',this.pindex)
            }else{
              this.$Message.info(data.msg)
            }
          })
        }
      })
    },
    bgdatechange(date){
      this.honorWelfareData.start_time=date
    },
    eddatechange(date){
      this.honorWelfareData.end_time=date
    },
    getHonorWelfareDetail(id){
      this.$api.getHonorWelfareDetail({id}).then(data=>{
        if(data){
          this.honorWelfareData.welfareName=data.name
          this.honorWelfareData.required=data.required
          this.honorWelfareData.start_time=!!data.start_time?changeUnixtimedate(data.start_time):''
          this.honorWelfareData.end_time=!!data.end_time?changeUnixtimedate(data.end_time):''
        }
      })
    }
  },
  created() {
    this.$nextTick(()=>{
      if(!!this.welfareTypeId&&this.isWelfareType=='edit'){
        this.getHonorWelfareDetail(this.welfareTypeId)
      }
    })
  }
};
</script>
<style>
</style>


