<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="advertName">
            <el-input  v-model="dataForm.advertName"></el-input>
        </el-form-item>

        <el-form-item label="广告图" prop="advertImg">
          <img-upload v-model="dataForm.advertImg" />
        </el-form-item>

        <el-form-item label="背景图" prop="advertBack">
          <img-upload v-model="dataForm.advertBack" />
        </el-form-item>

        <el-form-item label="排序" prop="advertSort">
            <el-input-number
              size="small"
              v-model="dataForm.advertSort"
              controls-position="right"
              :max="1000000000"
              :min="0"
            ></el-input-number>
        </el-form-item>
   
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="dataForm.status"
            :active-value="1"
            :inactive-value="2"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </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>
import imgUpload from '@/components/img-upload'
export default {
  data () {
    return {
      loading: false,
      dataForm: {
        "advertName": '',
        "advertImg": '',
        "advertBack": '',
        "jumpUrl": '',
        "advertSort": 1,
        "status": 1
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      addProdVisible: false,
      visible: false,
      resourcesUrl: window.SITE_CONFIG.resourcesUrl,
      dataRule: {
        advertName: [
          { required: true, message: '请输入广告位名称', trigger: 'blur' },
        ],
        advertImg: [
          { required: true, message: '请上传广告图', trigger: 'change' },
        ],
         advertBack: [
          { required: true, message: '请上传背景图', trigger: 'change' },
        ],
         advertSort: [
          { required: true, message: '请输入排序', trigger: 'blur' },
        ],
       
      }
    }
  },
  components: {imgUpload},
  methods: {
    init (data) {
      this.visible = true
      if (data) {
        // this.dataForm = Object.assign({}, data)
         this.dataForm ={
          "id": data.id,
          "advertName": data.advertName,
          "advertImg": data.advertImg,
          "advertBack": data.advertBack,
          "jumpUrl": data.jumpUrl,
          "advertSort": data.advertSort,
          "status": 1
        }
      } else {
        this.$nextTick(() => {
          this.dataForm = {
            "advertName": '',
            "advertImg": '',
            "advertBack": '',
            "jumpUrl": '',
            "advertSort": '',
            "status": 1
          }
          this.$refs['dataForm'].resetFields()
        })
      }
      console.log(this.dataForm);

    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.loading = true
          let param = this.dataForm
          this.$http({
            url: this.$http.adornUrl(`/advert//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)
                this.$nextTick(() => {
                  this.loading = false
                })
              }
            })
          })
        }
      })
    }
  }
}
</script>
