<template>
  <div class="evaluation-form">
    <el-form ref="ruleForm" :model="ruleForm" size="small" label-width="140px">
      <el-form-item label="评价数据" prop="content" :rules="[{
                    required: true, message: '评价数据不能为空', trigger: 'blur'
                  },{min: 1, max: 200, message: '最多200 个字符', trigger: 'blur'}]">
        <el-input maxlength="200"	 clearable type="textarea" rows="5" v-model="ruleForm.content" size="small" placeholder="请输入评价数据"/>
      </el-form-item>
      <el-form-item label="备注" prop="note" :rules="[
      {min: 0, max: 200, message: '最多200 个字符', trigger: 'blur'}]">
        <el-input  maxlength="200" clearable type="textarea" rows="5" v-model="ruleForm.note" size="small" placeholder="请输入备注"/>
      </el-form-item>
      <el-form-item clearable label="排序" prop="sort">
        <el-input-number size="small" v-model="ruleForm.sort"></el-input-number>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button icon="el-icon-document-add" :loading="addBtnLoading" type="primary" size="medium"
                 @click="submitForm('ruleForm')">提交
      </el-button>
      <el-button icon="el-icon-back" v-if="ruleForm.id" style="margin-left: 15px" size="medium"
                 @click="onBack('ruleForm')">返回
      </el-button>
    </div>
  </div>
</template>

<script>
  import evaluationApi from '@/api/basicData/evaluation'

  export default {
    name: "evaluationForm",
    data() {
      return {
        addBtnLoading: false
      }
    },
    props: {
      ruleForm: {
        type: Object,
        default() {
          return {};
        }
      }
    },
    created() {
    },
    mounted() {
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let method = 'addDriver'
            if (this.ruleForm.id) {
              method = 'editDriver'
            }
            let ruleForm = JSON.parse(JSON.stringify(this.ruleForm))
            this.addBtnLoading = true
            evaluationApi[method](ruleForm).then(res => {
              this.addBtnLoading = false
              this.$refs[formName].resetFields()
              this.$message({
                type: 'success',
                message: this.ruleForm.id ? '修改成功' : '添加成功'
              })
              if (this.ruleForm.id) {
                this.$emit('submit', 2)
                this.$emit('onBack')
              } else {
                this.$emit('submit', 1)
              }
            }).catch(e => {
              this.addBtnLoading = false
            })
          }
        })
      },
      onBack() {
        this.$refs['ruleForm'].resetFields()
        this.$emit('onBack')
      }
    }
  }
</script>

<style lang="less">
  .evaluation-form {

  }
</style>
