<template>
  <div class="mod-hotSearch-add-or-update">
    <el-dialog :title="!dataForm.id ? '新增' : '修改'"
               :close-on-click-modal="false"
               :visible.sync="visible">
      <el-form :model="dataForm"
               :rules="dataRule"
               ref="dataForm"
               @keyup.enter.native="dataFormSubmit()"
               label-width="120px">

        <el-form-item label="标题"
                      prop="name">
          <el-input v-model="dataForm.name"
                    controls-position="right"
                    :min="0"
                    label="标题"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="dataForm.type" placeholder="请选择类型">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="落地页名称" prop="resultName">
          <el-input
            v-model="dataForm.resultName"
            placeholder="请输入落地页名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="跳转参数" prop="jumpParam">
          <el-input
            v-model="dataForm.jumpParam"
            placeholder="请输入跳转参数"
          ></el-input>
        </el-form-item>

        <el-form-item label="有效时间参数" prop="effectiveTime">
          <el-date-picker
            v-model="dataForm.effectiveTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="截止时间参数" prop="deadTime">
          <el-date-picker
            v-model="dataForm.deadTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
   
        <el-form-item label="状态"
                      size="mini"
                      prop="status">
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">下线</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" :loading="loading"
                   @click="dataFormSubmit()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading:false,
      options:[{
        label:'关键词',
        value:1
      },{
        label:'热门搜索词',
        value:2
      },{
        label:'推荐搜索词',
        value:3
      }],
      dataForm: {
        id: 0,
        name: '',
        type: '',
        resultName: '',
        jumpParam: '',
        effectiveTime: '',
        deadTime: '',
        status: 0
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      addProdVisible: false,
      visible: false,
      resourcesUrl: window.SITE_CONFIG.resourcesUrl,
      dataRule: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' },
          // { min: 1, max: 50, message: '长度在1到50个字符内', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' },
        ],
         effectiveTime: [
          { required: true, message: '请选择有效时间', trigger: 'change' },
        ],
         deadTime: [
          { required: true, message: '请选择截止时间', trigger: 'change' },
        ],
       
      }
    }
  },
  components: {},
  methods: {
    init (data) {
      this.visible = true
      if (data) {
        this.dataForm = Object.assign({}, data)
        this.$delete(this.dataForm,'deleted') 
        console.log(this.dataForm);
      } else {
        this.$nextTick(() => {
          this.dataForm = {
            name: '',
            type: '',
            resultName: '',
            jumpParam: '',
            effectiveTime: '',
            deadTime: '',
            status: 1
          }
          this.$refs['dataForm'].resetFields()
        })
      }
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.loading = true
          let param = this.dataForm
          this.$http({
            url: this.$http.adornUrl(`/keyword/addOrUpdate`),
            method:'post',
            data: this.$http.adornData(param)
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList', this.page)
                setTimeout(() => {
                  this.loading = false
                }, 1500)
               
              }
            })
          })
        }
      })
    }
  }
}
</script>
