<template>
  <div class="addCardTicket">
    <div class="form-box" style="margin-top: 0">
      <div>
        <span style="color: rgb(64, 158, 255);cursor: pointer;margin-left: 20px" @click="cancel">卡券</span>
        <span style="margin: 0 5px;color: #C0C4CC;">/</span>
        <span>卡券编辑</span>
      </div>
    </div>

    <div id="pane-first">
      <div class="form-box">
        <el-form :model="msg" style="padding:0 50px;" :rules="rules" ref="msg" label-width="130px">
          <el-form-item label="卡券名称" prop="Name" class="is-required">
            <el-input v-model="msg.Name" class="el-input--small"/>
          </el-form-item>

          <el-form-item label="卡券图标" prop="CouponIco" class="is-required" >
            <button class="el-button el-button--default el-button--small" @click="openChangeDig()">
              选择图标
            </button>
            <div class="img_yuan inputM_l">
              <img v-if="msg.CouponIco=='' || msg.CouponIco==null" src="../../assets/img/setup/default_img.png" alt="">
              <img v-else :src="msg.CouponIco" alt="">
            </div>
          </el-form-item>

          <el-form-item label="卡券有效期" class="is-required">
            <el-radio-group v-model="msg.IndateType">
              <el-radio :label="1">领取后N天内有效</el-radio>
              <el-radio :label="2">时间段</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="有效天数" prop="IndateDay" class="is-required" v-if="msg.IndateType==1">
            <el-input v-model="msg.IndateDay" class="el-input--small" step="1"  min="0" onkeyup="this.value= this.value.match(/\d+(\d{0,2})?/) ? this.value.match(/\d+(\d{0,2})?/)[0] : ''"/>
          </el-form-item>

          <el-form-item label="有效期范围" prop="value" class="is-required" v-if="msg.IndateType==2">
            <el-date-picker
              @input="daterangeChange"
              style="padding: 3px 10px;width: 450px;height: 32px"
              v-model="msg.value"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              align="right">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="核销总次数" prop="HeXiao" class="is-required">
            <el-input v-model="msg.HeXiao" class="el-input--small" placeholder="请输入整数" step="1"  min="0" onkeyup="this.value= this.value.match(/\d+(\d{0,2})?/) ? this.value.match(/\d+(\d{0,2})?/)[0] : ''"/>
            <span style="color: rgb(201, 201, 201);">注：50≥核销总次数≥1</span>
          </el-form-item>

          <el-form-item label="可发放数量" prop="TotalNum" class="is-required">
            <el-tooltip class="item" effect="dark" content="卡券可发放数量，-1表示不限制发放数量" placement="top"
                        style="position: absolute;left: -16px;top:13px">
              <i class="el-tooltip el-icon-info"></i>
            </el-tooltip>
            <el-input v-model="msg.TotalNum" class="el-input--small" :disabled="checked" step="1"  min="0" onkeyup="this.value= this.value.match(/\d+(\d{0,2})?/) ? this.value.match(/\d+(\d{0,2})?/)[0] : ''"/>
            <el-checkbox v-model="checked" @change="checked==true? msg.TotalNum=-1:msg.TotalNum=0">无限制</el-checkbox>
          </el-form-item>


          <el-form-item label="卡券描述" prop="Describe" class="is-required">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入卡券描述"
              v-model="msg.Describe">
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <el-button type="primary" style="margin: 12px 0 " @click="preserve('msg')" v-loading="loading">保存</el-button>
      <el-button style="margin: 12px 0 " @click="cancel">取消</el-button>
    </div>
    <!-- 选择文件 -->
    <el-dialog title="选择文件" :visible.sync="changeState" width="1240px">
      <ChooseImg @SelectId="SelectId" ref="mychild"></ChooseImg>
    </el-dialog>
  </div>
</template>

<script>
  import ChooseImg from "@/components/global/ChooseImg.vue";

  export default {
        name: "addCardTicket",
        components: {
          ChooseImg,
        },
      data(){
          return{
            msg:{
              ID:0,
              Name:'',
              CouponIco:'',
              IndateType:1,
              IndateDay:'',
              HeXiao:'',
              TotalNum:-1,
              Describe:'',
              StartDate:'',
              EndDate:'',

              value:'',
            },
            checked:true,
            loading:false,
            rules:{
              Name:[
                {required: true, message: "请输入卡券名称", trigger: "blur"}
              ],
              CouponIco:[
                {required: true, message: "请选择卡券图标", trigger: "blur"}
              ],
              IndateDay:[
                {required: true, message: "请输入有效天数", trigger: "blur"}
              ],
              HeXiao:[
                {required: true, message: "请输入可核销总次数", trigger: "blur"}
              ],
              TotalNum:[
                {required: true, message: "请输入可发放数量", trigger: "blur"}
              ],
              Describe:[
                {required: true, message: "请输入卡券描述", trigger: "blur"}
              ],
              value:[
                { type: 'array', required: true, message: '请输入有效天数', trigger: 'change' }

              ]

            },
            changeState:false
          }
      },
      created(){
        if(this.$route.query.ID){
          this.getCouponDetail(this.$route.query.ID)
        }
      },
      methods:{
        getCouponDetail(ID){
          this.apipost("/api/Coupon/GetCouponDetail",{'ID':ID} , res => {
            if (res.data.resultCode == 1) {
                this.msg = res.data.data
                this.msg.value = []
              if(res.data.data.IndateType==2){
                this.msg.value.push(res.data.data.StartDate.replace("T", " "))
                this.msg.value.push(res.data.data.EndDate.replace("T", " "))
              }
              if(res.data.data.TotalNum!=-1){
                  this.checked=false
              }

            } else {
              this.Info(res.data.message);
            }
          })
        },
        cancel(){
          this.$router.go(-1)
        },
        preserve(formName){
          this.$refs[formName].validate((valid) => {
            if (valid) {
              if(this.msg.IndateType==2){
                this.msg.StartDate=this.msg.value[0];
                this.msg.EndDate=this.msg.value[1];
                this.msg.IndateDay=0
              }

              // delete this.msg.value
              this.loading= true
              this.apipost("/api/Coupon/AddOrUpdateCoupon",this.msg , res => {
                this.loading= false
                if (res.data.resultCode == 1) {
                  this.Success(res.data.message);
                  this.$router.go(-1);//返回上一层

                } else {
                  this.Info(res.data.message);
                }
              })
            } else {
              return false;
            }
          });
        },
        daterangeChange(e){
          let _this =this
          _this.$nextTick(() => {
            _this.$set(_this.msg, "value", [e[0], e[1]]);
            _this.$forceUpdate();
          });
        },
        openChangeDig(){
          this.changeState=true;
          setTimeout(()=>{
            this.$refs.mychild.InitData();
          },10)
        },
        SelectId(msg){
          this.changeState=false;
          this.msg.CouponIco= this.getIconLink(msg.url);
        },

      }
    }
</script>

<style>
  .addCardTicket .form-box {
    background: #fff;
    padding: 20px 50% 20px 0;
    margin-top: 10px;
  }
  .addCardTicket .el-input--small .el-input__inner{
    height: 32px;
    line-height: 32px;
  }
  .addCardTicket .img_yuan{
    width: 80px;
    height: 80px;
    margin-top: 5px;
    position: relative;
  }
  .addCardTicket .img_yuan img{
    width: 100%;
    height: 100%;
  }
  .addCardTicket .el-date-editor .el-range__icon {
    line-height: 24px;
  }
  .addCardTicket .el-date-editor .el-range-separator{
    line-height: 24px;

  }
  .addCardTicket .el-date-editor .el-range__close-icon{
    line-height: 24px;

  }
  .addCardTicket .el-form-item__label{
    padding: 0 17px 0 0;
  }
</style>
