<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="picture">
        <image-upload2 v-model="form.picture"/>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="form.sort" placeholder="请输入排序" />
      </el-form-item>
      <el-form-item label="状态" prop="enableStatus">
        <el-select v-model="form.enableStatus" placeholder="状态" size="small">
          <el-option
              v-for="dict in enableStatusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </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 { enableStatusList } from '@/constant/statusMap'
import { addPairSuccess, getPairSuccess, updatePairSuccess } from '@/api/xq/pairSuccess'

export default {
  components: {
  },
  data () {
    return {
      enableStatusList,
      open: false,
      title: '',
      form:{
        id: undefined,
        picture: undefined,
        sort: undefined,
        remark: undefined,
        enableStatus: undefined,
      },
      // 表单校验
      rules: {
        picture: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        enableStatus: [
          { required: true, message: "数据不能为空", trigger: "change" }
        ],
      },
      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){
          getPairSuccess(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updatePairSuccess(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.$modal.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else{
            addPairSuccess(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }

        }
      })
    },
  }
}
</script>
