<template>
  <el-dialog 
    :title="title" 
    :visible.sync="dialogVisible" 
    width="1000px" 
    append-to-body 
    @close="handleClose"
    @open="handleOpen"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="loading">
      <el-form-item label="分类" prop="classifyId">
        <attach-classify-select v-model="form.classifyId" check-strictly/>
      </el-form-item>
      <el-form-item label="资源名称" prop="name" v-if="form.id != null">
        <el-input v-model="form.name" placeholder="请输入资源名称" />
      </el-form-item>
      <el-form-item label="资源" prop="url" v-if="form.id == null">
        <image-upload v-model="form.url" :limit="100" :file-type="fileType" :file-size="200" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :loading="submitLoading">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addAttach, updateAttach, getAttach } from "@/api/bst/attach"
import AttachClassifySelect from '@/components/Business/AttachClassify/AttachClassifySelect.vue'
import { FileType } from '@/utils/constants'

export default {
  name: "AttachClassifyEditDialog",
  components: {
    AttachClassifySelect
  },
  props: {
    show: {
      type: Boolean,
      required: true
    },
    id: {
      type: String,
      default: null
    },
    initData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      loading: false,
      submitLoading: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classifyId: [
          { required: true, message: "分类不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "资源名称不能为空", trigger: "blur" }
        ],
        url: [
          { required: true, message: "资源不能为空", trigger: "blur" }
        ]
      },
      fileType: FileType.all()
    }
  },
  computed: {
    title() {
      return this.id ? '修改资源' : '新增资源'
    },
    dialogVisible: {
      get() {
        return this.show
      },
      set(value) {
        this.$emit('update:show', value)
      }
    }
  },
  methods: {
    /** 获取详细信息 */
    getInfo(id) {
      this.loading = true
      getAttach(id).then(response => {
        this.form = response.data
      }).finally(() => {
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        classifyId: null,
        urls: null,
        ...this.initData
      }
      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.submitLoading = true
          const submitFunc = this.form.id ? updateAttach : addAttach
          submitFunc(this.form).then(() => {
            this.$modal.msgSuccess(this.form.id ? "修改成功" : "新增成功")
            this.$emit('success')
            this.handleClose()
          }).finally(() => {
            this.submitLoading = false
          })
        }
      })
    },
    // 取消按钮
    handleClose() {
      this.$emit('update:show', false)
    },
    // 打开事件
    handleOpen() {
      if (this.id) {
        this.getInfo(this.id)
      } else {
        this.reset()
      }
    }
  }
}
</script> 