<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-form-item label="政策名称" prop="policyName">
        <el-input v-model="ruleForm.policyName" placeholder="请输入政策名称"></el-input>
      </el-form-item>
      <el-form-item label="促销规则" prop="promotionSpecifications">
        <el-select v-model="ruleForm.promotionSpecifications" clearable placeholder="请选择促销方式">
          <el-option label="按总件数" value="按总件数"></el-option>
          <el-option label="按总金额" value="按总金额"></el-option>
        </el-select>
        <!-- <el-input v-model="ruleForm.promotionSpecifications" placeholder="请输入促销规则"></el-input> -->
      </el-form-item>
      <el-form-item label="绑定赠品" prop="bundledGifts">
        <el-select v-model="ruleForm.bundledGifts" multiple placeholder="请选择绑定赠品" label-width="90px">
          <el-option
            v-for="item in giftsList"
            :key="item.F_ProductId"
            :label="item.F_ProductName"
            :value="item.F_ProductId">
          </el-option>
        </el-select>
        <!-- <el-input v-model="ruleForm.bundledGifts" placeholder="请输入绑定赠品"></el-input> -->
      </el-form-item>
      <el-form-item label="政策描述" prop="policyDescription">
        <el-input v-model="ruleForm.policyDescription" placeholder="请输入政策描述"></el-input>
      </el-form-item>
      <el-form-item label="政策有效期" prop="lateValidity">
        <el-date-picker
        v-model="ruleForm.lateValidity"
        :picker-options="pickerOptions"
        class="filter-item"
        type="daterange"
        format="yyyy年MM月dd日"
        value-format="yyyy-MM-dd"
        align="left"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
        <!-- <el-date-picker v-model="ruleForm.lateValidity" type="date" placeholder="请选择延迟有效期" class="form_date"></el-date-picker> -->
      </el-form-item>
      <el-form-item label="适用会员" prop="applicableMember">
        <el-select v-model="ruleForm.applicableMember" clearable placeholder="请选择会员标签">
          <el-option
            v-for="item in memeberList"
            :key="item.F_ID"
            :label="item.F_Name"
            :value="item.F_ID">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="绑定商品标签" prop="boundGoods">
        <el-select v-model="ruleForm.boundGoods" clearable placeholder="请选择绑定商品标签">
          <el-option
            v-for="item in shopList"
            :key="item.F_TagName"
            :label="item.F_TagName"
            :value="item.F_TagName">
          </el-option>
        </el-select>
        <!-- <el-input v-model="ruleForm.boundGoods" placeholder="请输入绑定商品"></el-input> -->
      </el-form-item>
      <el-form-item label="活动金额" prop="money">
        <el-input v-model="ruleForm.money" placeholder="请输入活动金额" onkeyup="value=value.replace(/[^\d.]/g,'')"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getProviceCity, getDictionary } from '@/api/commonAPI'
import { getFullMarketingDetail, addFullMarketing, editFullMarketing,getGifts,getshopping } from '@/api/marketingManagement/fullGiveMarketing'
import { getLabelLists} from '@/api/memberManagement/memberList'
import { validateTel } from '@/utils/validate'
import { filterContent,pickerOptions } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    callid: {
      type: Number,
      default: 0
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      props: { // 自定义省市下拉数据的key值
        value: 'code',
        label: 'name',
        children: 'entityJson'
      },
      pickerOptions ,// 日期数据
      ruleForm: {
        unique_id: '', //		否	string	callid
        ordercode: '',
        policyName: '', //政策名称
        promotionSpecifications: '', //促销规则
        bundledGifts: [], //绑定赠品
        policyDescription: '', //政策描述
        lateValidity: '', //延迟有效期
        applicableMember: '', //适用会员
        applicableMemberId: '', //适用会员id
        boundGoods: '', //绑定商品
        money: '', //活动金额
      },
      memeberList:[],//会员标签
      giftsList:[],//绑定赠品数据
      shopList:[],//绑定商品数据
      rules: {
        policyName: [{
          required: true,
          trigger: 'blur',
          message: '请输入政策名称',
        }],
        lateValidity: [{
          required: true,
          trigger: 'blur',
          message: '请输入政策有效期',
        }],
      },
      loading: false
    }
  },
  created() {
    this.getLable();//获取会员标签
    this.getGiftsList();//获取绑定赠品
    this.getShopList();//获取绑定商品
    if (this.callid) {
      this.ruleForm.unique_id = this.callid
    }
    Promise.all([])
      .then(() => {
        if (this.rowid) {
          this.ruleForm.ordercode = this.rowid
          this.getDetail(this.rowid)
        }
      })
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.ruleForm.content = filterContent.delHtmlTag(this.ruleForm.content)// 保存内容时去掉 html 标签
          this.loading = true
          return new Promise(resolve =>{
            const params = {
              F_Type:1,
              F_ID:this.rowid ? this.rowid : '',
              F_Name:this.ruleForm.policyName,//政策名称
              F_PromotionRules:this.ruleForm.promotionSpecifications,//促销规则
              F_Gift:this.ruleForm.bundledGifts.toString(),//绑定赠品
              F_Describe:this.ruleForm.policyDescription,//政策描述
              F_StartTime:this.ruleForm.lateValidity && this.ruleForm.lateValidity[0],//政策开始时间
              F_EndTime:this.ruleForm.lateValidity && this.ruleForm.lateValidity[1],//政策结束时间
              F_Member:this.ruleForm.applicableMember,//会员ID
              F_Commodity:this.ruleForm.boundGoods,//商品ID
              F_Money:this.ruleForm.money,//活动金额
            }
            // 添加
            if (!this.rowid) {
              addFullMarketing(params).then(response => {
                this.loading = false
                if (response.state.toLowerCase() === 'success') {
                  this.$parent.$layer.close(this.layerid)
                  if (this.callid) {
                    this.$parent.getOrderRecord() // 重新加载来电弹屏工单记录
                  } else {
                    this.$parent.btn_search() // 重新加载父级数据
                  }
                  this.$message.success('恭喜你，添加成功!')
                }
              }).catch(() => {
                this.loading = false
              })
              return
            }
            // 编辑
            editFullMarketing(params).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getList() // 重新加载父级数据
                this.$message.success('恭喜你，编辑成功!')
              }
            }).catch(() => {
              this.loading = false
            })
          })
          
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
      this.ruleForm.lateValidity=''
    },
    // 详情
    getDetail(rid) {
      getFullMarketingDetail(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.policyName = res.F_Name;
          this.ruleForm.promotionSpecifications = res.F_PromotionRules;
          this.ruleForm.bundledGifts = res.F_Gift?res.F_Gift.split(','):[];
          this.ruleForm.policyDescription = res.F_Describe;
          this.ruleForm.boundGoods = res.F_Commodity;
          this.ruleForm.applicableMember = res.F_Member?(res.F_Member-0):'';
          // this.ruleForm.applicableMemberId = res.F_Member?res.F_Member:'';
          this.ruleForm.money = res.F_Money;
          this.ruleForm.lateValidity = [res.F_StartTime,res.F_EndTime];
        }
      })
    },
    //获取会员标签
    getLable(){
      getLabelLists().then(response =>{
        if(response.state.toLowerCase() == 'success'){
          this.memeberList=response.rows.Rows
        }
      }).catch(result=>{
        console.log(result)
      })
    },
    //获取绑定赠品
    getGiftsList(){
      getGifts({IsSale:0}).then(response =>{
        if(response.state.toLowerCase() == 'success'){
          this.giftsList=response.data.Rows
        }
      }).catch(result=>{
        console.log(result)
      })
    },
    //获取绑定商品
    getShopList(){
      getshopping({CLass:3}).then(response =>{
        if(response.state.toLowerCase() == 'success'){
          this.shopList=response.data
        }
      }).catch(result=>{
        console.log(result)
      })
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.order_form {
  .form_select {
  	width: 100%;
  }
  .form_date {
    width: 100%;
  }
}
</style>