<!-- 用户编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改广告位':'添加广告位'"
    :visible="visible"
    width="600px"
    :destroy-on-close="true"
    :lock-scroll="false"
    custom-class="ele-dialog-form"
    @update:visible="updateVisible">
    <el-form
      :model="form"
      ref="form"
      :rules="rules"
      label-width="82px">
      <el-row :gutter="15">
        <el-col>
          <el-form-item
            label="名称:"
            prop="name">
            <el-input
              v-model="form.name"
              placeholder="请输入广告位名称"
              clearable/>
          </el-form-item>
          <el-form-item
            label="广告标识:"
            prop="name">
            <el-input
              v-model="form.idf"
              :disabled="isUpdate?true:false"
              placeholder="请输入广告位前台调用标识"
              clearable/>
          </el-form-item>
          <el-form-item label="分类:">
            <el-select
              v-model="form.type"
              placeholder="请选择广告位分类"
              class="ele-fluid">
              <el-option
                v-for="item in tagLists"
                :key="item.id"
                :label="item.name"
                :value="item.id">
                <span style="padding-left: 8px;">{{ item.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>


          <el-form-item
            label="排序号:"
            prop="sortNumber">
            <el-input-number
              v-model="form.sort"
              controls-position="right"
              :min="0"
              placeholder="请输入排序号"
              class="ele-fluid ele-text-left"/>
          </el-form-item>

          <el-form-item label="描述:">
            <el-input
              v-model="form.desc"
              placeholder="请输入描述"
              clearable/>
          </el-form-item>

        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button
        @click="updateVisible(false)">取消
      </el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

export default {
  name: 'AdEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,

  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        name: [
          {required: true, message: '请输入广告位名称', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入广告位排序', trigger: 'blur'}
        ],

      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 角色列表
      tagLists: [],
    }
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        console.log(this.form )
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    this.getTagLists();  // 查询角色列表
  },
  methods: {
    afterUploadImage(response) {
      if (response.code != 0 || response.url == '') {
        this.$message.error(response.msg)
      } else {
        // console.log(this.form)
        // this.form.url = response.url
        // console.log(this.form)

        this.$set(this.form,'url',response.url)
      }
    },

    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;

          this.$http.post('/Ad/' + (this.isUpdate ? 'edit' : 'add'), this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message({type: 'success', message: res.data.msg});
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /* 查询角色列表 */
    getTagLists(){
      this.$http.get('/Ad/getType').then(res => {
        if (res.data.code === 0) {
          this.tagLists = res.data.data;
        }
      })
    },
  }
}
</script>

<style scoped>
</style>
