<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-form-item label="活动名称" prop="activityName">
        <el-input v-model="ruleForm.activityName" placeholder="请输入活动名称"></el-input>
      </el-form-item>
      <el-form-item label="活动时间" prop="activityTime">
        <el-date-picker
        v-model="ruleForm.activityTime"
        :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-form-item>
      <el-form-item label="活动描述" prop="activityDescription">
        <el-input v-model="ruleForm.activityDescription" placeholder="请输入活动描述"></el-input>
      </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-form-item>
      <el-form-item label="商品活动价格" prop="commodityActivityPrice">
        <el-input v-model="ruleForm.commodityActivityPrice" placeholder="请输入商品活动价格" type="number"></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,getshopping } from '@/api/marketingManagement/fullGiveMarketing'
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'
      },
      sourceOptions: [], // 工单来源下拉数据
      pickerOptions ,// 日期数据
      shopList:[],//绑定商品数据
      ruleForm: {
        unique_id: '', //		否	string	callid
        ordercode: '',
        activityName: '', //活动名称
        activityTime: '', //活动时间
        activityDescription: '', //活动描述
        boundGoods: '', //绑定商品
        commodityActivityPrice: '', //商品活动价格
      },
      rules: {
        activityName: [{
          required: true,
          trigger: 'blur',
          message: '请输入活动名称',
        }],
        activityTime: [{
          required: true,
          trigger: 'blur',
          message: '请输入活动时间',
        }],
      },
      loading: false
    }
  },
  created() {
    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:4,
              F_ID:this.rowid ? this.rowid : '',
              F_Name:this.ruleForm.activityName,//活动名称
              F_Describe:this.ruleForm.activityDescription,//活动描述
              F_StartTime:this.ruleForm.activityTime && this.ruleForm.activityTime[0],//政策开始时间
              F_EndTime:this.ruleForm.activityTime && this.ruleForm.activityTime[1],//政策结束时间
              F_Commodity:this.ruleForm.boundGoods,//商品ID
              F_Money:this.ruleForm.commodityActivityPrice,//活动价格
            }
            // 添加
            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.activityTime = ''
    },
    // 详情
    getDetail(rid) {
      getFullMarketingDetail(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.activityName = res.F_Name;
          this.ruleForm.activityDescription = res.F_Describe;
          this.ruleForm.boundGoods = res.F_Commodity;
          this.ruleForm.commodityActivityPrice = res.F_Money;
          this.ruleForm.activityTime = [res.F_StartTime,res.F_EndTime];
        }
      })
    },
    //获取绑定商品
    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>