<template>
  <div class="driver-form">
    <el-form ref="ruleForm" :model="ruleForm" size="small" label-width="180px">
      <el-form-item label="河湖类型" prop="type" :rules="{
                    required: true, message: '河湖类型不能为空', trigger: 'change'
                  }">
        <el-select :disabled="ruleForm.disabled" filterable clearable v-model="ruleForm.type" size="small"
                   placeholder="请选择河湖类型">
          <el-option v-for="(value,key,index) in typeList" :key="key" :label="value" :value="parseInt(key)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="河湖名称" prop="name" :rules="[{
                    required: true, message: '河湖名称不能为空', trigger: 'blur'
                  },
                  {min: 2, max: 20, message: '长度在2 到 20 个字符', trigger: 'blur'}]">
        <el-input maxlength="20" :disabled="ruleForm.disabled" clearable v-model="ruleForm.name" size="small" placeholder="请输入河湖名称"/>
      </el-form-item>
      <el-form-item label="河湖等级" prop="level" :rules="{
                    required: true, message: '河湖等级不能为空', trigger: 'change'
                  }">
        <el-select :disabled="ruleForm.disabled" filterable clearable v-model="ruleForm.level" size="small"
                   placeholder="请选择河湖等级">
          <el-option v-for="(value,key,index) in levelList" :key="key" :label="value" :value="parseInt(key)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="河长" prop="driverManager" :rules="[{
                    required: true, message: '河长不能为空', trigger: 'change'
                  }]">
        <el-select :disabled="ruleForm.disabled" filterable clearable v-model="ruleForm.driverManager" size="small"
                   placeholder="请选择河湖等级">
          <el-option v-for="(item,index) in userList" :key="index" :label="item.realname" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="起点" prop="startPoint" :rules="[
        {min: 0, max: 50, message: '最多50 个字符', trigger: 'blur'}]">
        <el-input maxlength="50" :disabled="ruleForm.disabled" clearable v-model="ruleForm.startPoint" size="small"
                  placeholder="请输入起点"/>
      </el-form-item>
      <el-form-item label="终点" prop="endPoint" :rules="[
        {min: 0, max: 50, message: '最多50 个字符', trigger: 'blur'}]">
        <el-input maxlength="50" :disabled="ruleForm.disabled" clearable v-model="ruleForm.endPoint" size="small" placeholder="请输入终点"/>
      </el-form-item>
      <el-form-item label="河道长度" prop="length" :rules="{
                    validator: validateNum, trigger: 'blur'
                  }">
        <el-input maxlength="9" :disabled="ruleForm.disabled" clearable v-model="ruleForm.length" size="small" placeholder="请输入长度"/>
      </el-form-item>
      <el-form-item label="区划" prop="driverArea" :rules="{
                    required: true, message: '区划不能为空', trigger: 'change'
                  }">
        <driverAreaSelect :clearValidate="clearValidate" :disabled="ruleForm.disabled" v-model="ruleForm.driverArea"></driverAreaSelect>
      </el-form-item>
      <el-form-item label="水质标准" prop="category">
        <el-select :disabled="ruleForm.disabled" filterable clearable v-model="ruleForm.category" size="small"
                   placeholder="水质标准">
          <el-option v-for="(item,index) in categoryList" :key="index" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item v-if="ruleForm.disabled" label="创建人" prop="format_create_user">
        <el-input :disabled="ruleForm.disabled" clearable v-model="ruleForm.format_create_user" size="small"
                  placeholder=""/>
      </el-form-item>
      <el-form-item v-if="ruleForm.disabled" label="创建时间" prop="format_create_time">
        <el-input :disabled="ruleForm.disabled" clearable v-model="ruleForm.format_create_time" size="small"
                  placeholder=""/>
      </el-form-item>
      <el-form-item label="备注" prop="note" :rules="[
        {min: 0, max: 200, message: '最多200个字符', trigger: 'blur'}]">
        <el-input :disabled="ruleForm.disabled" show-word-limit maxlength="200" clearable type="textarea" rows="5" v-model="ruleForm.note" size="small" placeholder="请输入备注"/>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button v-if="!ruleForm.disabled" 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 driverApi from '@/api/basicData/driver'
  import driverAreaSelect from '@/components/driverAreaSelect'

  export default {
    name: "driverForm",
    data() {
      var patrn = /^\d+(\.\d+)?$/;
      var lengthP = /^(\d+)(.\d{0,4})?$/
      var validateNum = (rule, value, callback) => {
        if (!value) {
          callback();
        } else {
          if (rule.field == "length") {
            if (!lengthP.test(value)) {
              callback(new Error('小数点最多4位'));
            }else if (parseInt(value)>9999) {
              callback(new Error('最大10000'));
            } else {
              callback();
            }
          }else{
            if (!patrn.test(value)) {
              callback(new Error('请输入数字'));
            } else {
              callback();
            }
          }
        }
      };
      return {
        validateNum: validateNum,
        addBtnLoading: false,
        categoryList:[
          {id:'categoryOne',name:'Ⅰ类'},
          {id:'categoryTwo',name:'Ⅱ类'},
          {id:'categoryThree',name:'Ⅲ类'},
          {id:'categoryFour',name:'Ⅳ类'},
          {id:'categoryFive',name:'Ⅴ类'},
        ]
      }
    },
    props: {
      ruleForm: {
        type: Object,
        default() {
          return {};
        }
      },
      userList:{
        type: Array,
        default() {
          return [];
        }
      },
      typeList: {
        type: Object,
        default() {
          return {};
        }
      },
      levelList: {
        type: Object,
        default() {
          return {};
        }
      }
    },
    components: {driverAreaSelect},
    created() {
    },
    mounted() {
    },
    methods: {
      clearValidate(){
        this.$refs.ruleForm.clearValidate('driverArea')
      },
      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
            driverApi[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>
