<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="indexForm" size="small" label-position="right" label-width="140px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="指标名称" prop="indexName">
            <el-input v-model.trim="indexForm.indexName" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="指标级别" prop="level">
            <el-select v-model="indexForm.level" placeholder="请选择">
              <el-option
                v-for="item in levelList"
                :key="item.value"
                :label="item.name"
                :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="indexForm.level=='2'" :gutter="20">
        <el-col :span="12">
          <el-form-item label="指标类型" prop="indexType">
            <el-select v-model="indexForm.indexType" placeholder="请选择">
              <el-option
                v-for="item in indexTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="indexForm.indexType=='1'" :span="12">
          <el-form-item label="分值" prop="score">
            <el-input v-model.trim="indexForm.score" :disabled="disabled" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="质检考核标准解析" prop="indexDetail">
            <el-input v-model.trim="indexForm.indexDetail" :disabled="disabled" type="textarea" 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 {
  indexName: 'EditIndex',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      indexForm: {
        id: '', // 编号
        modularId: '', // 模块id
        indexName: '', // 指标名称
        level: '', // 指标等级
        firstIndexId: '', // 上级id
        indexDetail: '', // 考核标准
        score: '', // 分数
        indexType: '' // 指标类型
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增',
        info: '详情'
      }, // 表头显示标题
      rules: {
        indexName: [{ required: true, message: '姓名不能为空', trigger: ['blur', 'change'] }],
        level: [{ required: true, message: '等级不能为空', trigger: ['blur', 'change'] }],
        indexType: [{ required: true, message: '指标类型不能为空', trigger: ['blur', 'change'] }],
        score: [{ required: true, message: '分值不能为空', trigger: ['blur', 'change'] }],
        indexDetail: [{ required: true, message: '内容不能为空', trigger: ['blur', 'change'] }]
      },
      levelList: [{ name: '一级指标', value: '1' }, { name: '二级指标', value: '2' }],
      indexTypeList: [{ name: '分值型', value: '1' }, { name: '是非型', value: '2' }],
      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.indexForm = {
          id: row.id, // 编号
          modularId: row.modularId, // 模块id
          indexName: row.secondIndexName, // 指标名称
          level: '2', // 指标等级
          firstIndexId: row.firstIndex, // 上级id
          indexDetail: row.indexDetail, // 考核标准
          score: row.score, // 分数
          indexType: row.indexType // 指标类型
        }
        if (dialogStatus === 'info') {
          this.disabled = true
        } else {
          this.disabled = false
        }
      }
    },
    // 重置表单
    resetForm() {
      this.indexForm = {
        id: '', // 编号
        modularId: '', // 模块id
        indexName: '', // 指标名称
        level: '', // 指标等级
        firstIndexId: '', // 上级id
        indexDetail: '', // 考核标准
        score: '', // 分数
        indexType: '' // 指标类型
      }
    },
    // 保存数据
    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.indexForm)
        if (valid) {
          this.btnLoading = true
          addCustomer(this.indexForm).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.indexForm).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>
