<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入标题" />
      </el-form-item>
      <el-form-item label="轮播图" prop="image">
        <image-upload2 v-model="form.image"/>
      </el-form-item>
      <el-form-item label="链接地址" prop="linkUrl">
        <el-input v-model="form.linkUrl" placeholder="请输入链接地址" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item label="图片地址" prop="imageDetails">
        <el-input v-model="form.imageDetails" placeholder="请输入图片地址" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addBanner, getBanner, updateBanner } from '@/api/cai/banner'

export default {
  components: {
  },
  data () {
    return {
      open: false,
      title: '',
      form:{
        id: undefined,
        type: 1,
        cid: 1,
        title: undefined,
        image: undefined,
        linkType: undefined,
        linkUrl: undefined,
        sort: undefined,
        imageDetails: undefined
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        linkType: [
          { required: true, message: "链接类", trigger: "change" }
        ],
        linkUrl: [
          { required: true, message: "链接地址不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ]
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init (id) {
      this.form.id = id || undefined;
      this.title = (id ? "修改" : "新增") + "轮播";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        if(this.form.id){
          getBanner(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateBanner(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.$modal.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else{
            addBanner(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }

        }
      })
    },
  }
}
</script>
