<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="modularForm" size="small" label-position="right" label-width="90px">
      <el-row :gutter="20" type="flex" justify="center">
        <el-col :span="16" c>
          <el-form-item label="模块名称" prop="modularName">
            <el-input v-model.trim="modularForm.modularName" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" type="flex" justify="center">
        <el-col :span="16">
          <el-form-item label="描述" prop="description">
            <el-input v-model.trim="modularForm.description" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20" type="flex" justify="center">
        <el-col :span="16">
          <el-form-item label="总分" prop="score">
            <el-input v-model.trim="modularForm.score" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addCustomer, updateCustomer } from '@/api/customer'

export default {
  name: 'EditModular',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      modularForm: {
        id: '',
        modularName: '',
        description: '',
        score: ''
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增',
        info: '详情'
      }, // 表头显示标题
      rules: {
        modularName: [{ required: true, message: '模块名称不能为空', trigger: ['blur', 'change'] }],
        description: [{ required: true, message: '描述不能为空', trigger: ['blur', 'change'] }],
        score: [{ required: true, message: '总分不能为空', trigger: ['blur', 'change'] }],
      }, // 前端校验规则
      roleName: '',
      disabled: false,
      deptShow: true,
      btnLoading: false // 保存按钮的加载中状态
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      this.btnLoading = false
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.disabled = false
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update' || dialogStatus === 'info') { // 如果是修改，将row中数据填写到输入框中
        this.modularForm = {
          id:row.id,
          modularName: row.modularName,
          description: row.description,
          score: row.score
        }
        if (dialogStatus === 'info') {
          this.disabled = true
        } else {
          this.disabled = false
        }
      }
    },
    // 重置表单
    resetForm() {
      this.modularForm = {
        id: '',
        modularName: '',
        description: '',
        score: ''
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.modularForm)
        if (valid) {
          this.btnLoading = true
          addCustomer(this.modularForm).then(response => {
            if (response.code === 200) {
              this.$message.success('新增成功')
              this.$emit('refresh')
              this.dialogFormVisible = false
            }
          }).catch(_ => { // 异常情况，loading置为false
            this.btnLoading = false
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.btnLoading = true
          updateCustomer(this.modularForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('refresh')
              this.dialogFormVisible = false
            }
          }).catch(_ => { // 异常情况，loading置为false
            this.btnLoading = false
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

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

  .el-dialog{
    width:700px
  }
  .el-select{
    width: 100%;
  }
</style>
