<template>
  <div>
    <el-button type="primary" size="small" @click="dialogFormVisible = true">新 建</el-button>
    <el-dialog title="新建活动" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" size="small">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入活动类型" clearable></el-input>
        </el-form-item>
        <el-form-item label="活动类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" style="width:100%" clearable>
            <el-option
              v-for="item in campaigntypeList"
              :key="item.index"
              :label="item.value"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="form.director" placeholder="请输入负责人"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" placeholder="请输入地址"></el-input>
        </el-form-item>
        <el-form-item label="开始日期" prop="begdate">
          <el-date-picker
            v-model="form.begdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            style="width:100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="enddate">
          <el-date-picker
            v-model="form.enddate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
            style="width:100%">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" style="width:120px" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="small" style="width:120px" type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props:['campaigntypeList'],
  data () {
    return {
      dialogFormVisible:false,
      form:{
        "sat_campaignid":0,
        "name":"",
        "director":"",
        "address":"",
        "type":"",
        "begdate":"",
        "enddate": ""
      },
      rules:{
        name:[
          { required: true, message: '请输入活动名称', trigger: 'blur' },
        ],
        type:[
          { required: true, message: '请选择活动类型', trigger: 'change' }
        ],
        begdate: [
          { required: true, message: '请选择开始日期', trigger: 'change' }
        ],
        enddate: [
          { validator:this.validateEndDate, trigger: 'change' }
        ],
      }
    }
  },
  methods:{
    validateEndDate(rule, value, callback){
      let start = (new Date(this.form.begdate)).getTime()
      let end = (new Date(this.form.enddate)).getTime()
      if (value === '' || !value) {
        this.form.enddate = ''
        callback();
      } else if (end < start) {
         callback(new Error('结束日期不能小于开始日期'))
      } else {
        callback();
      }
    },
    add () {
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "classname":"webmanage.saletool.orderclue.ordercluecampaign",
          "method": "insertormodify_campaign",
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.dialogFormVisible = false
          this.$emit('onSuccess')
        })
      });
    },
    
  }
}

</script>
<style>
</style>