<template>
  <el-drawer
    :title="title"
    :modal-append-to-body="false"
    :modal="false"
    :wrapper-closable="false"
    size="25%"
    :visible.sync="dialogVisible"
  >
    <div class="content-container">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-position="top" label-width="100px">
        <el-form-item label="目录名称" prop="dirTitle">
          <el-input v-model="formData.dirTitle" />
        </el-form-item>
      </el-form>
      <div class="btn-group">
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
        <el-button class="cancel-btn" @click="dialogVisible = false">取 消</el-button>
      </div>
    </div>

  </el-drawer>
</template>

<script>
import { ACTION_ADD, ACTION_UPDATE } from '@/utils/actionType'
import { createDir, updateDir, getDir } from '@/api/system/docApi'

export default {
  name: 'DocDirectoryComponent',
  props: {
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialogVisible: false,
      formData: {
        dirId: '',
        dirTitle: '',
        parentId: ''
      },
      rules: {
        dirTitle: [
          { required: true, message: '请输入目录名称', trigger: 'blur' }
        ]
      },
      actionType: ''
    }
  },
  mounted() {
  },
  methods: {
    // Add Model
    showAddModel(parentId) {
      this.resetFormData()
      this.actionType = ACTION_ADD
      this.dialogVisible = true
      this.formData.parentId = parentId
    },

    // Edit Model
    showEditModel(dirId) {
      this.resetFormData()
      this.actionType = ACTION_UPDATE
      this.dialogVisible = true
      getDir(dirId).then((resp) => {
        const { code, data, msg } = resp
        if (code === 200) {
          this.formData = data
        } else {
          this.$message.error(msg)
        }
      }).catch((error) => {
        console.log(error)
      })
    },

    // 处理
    resetFormData() {
      this.formData = {
        dirId: '',
        dirTitle: '',
        parentId: ''
      }
    },

    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          switch (this.actionType) {
            case ACTION_ADD:
              createDir(this.formData).then((resp) => {
                const { code, msg } = resp
                if (code === 200) {
                  this.dialogVisible = false
                  this.$message.success(msg)
                  this.formSuccess()
                } else {
                  this.$message.error(msg)
                }
              }).catch((error) => {
                console.log(error)
              })
              break

            case ACTION_UPDATE:
              updateDir(this.formData).then((resp) => {
                const { code, msg } = resp
                if (code === 200) {
                  this.dialogVisible = false
                  this.$message.success(msg)
                  this.formSuccess()
                } else {
                  this.$message.error(msg)
                }
              }).catch((error) => {
                console.log(error)
              })
              break
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    formSuccess() {
      this.$emit('formSuccess')
    },
    resetFormField(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss">
</style>
<style lang="scss" scoped>
</style>
