<template>
  <el-dialog title="专家排序" :visible.sync="dialogFormVisible" width="600px">
    <div class="box">
      <div class="t-left">
        <div class="headPhoto">
          <img :src="form.img" alt="">
        </div>
      </div>
      <el-form :model="form" class="form-el" ref="form" :rules="rules2">
        <el-form-item :label-width="formLabelWidth">
          {{form.name}}
        </el-form-item>
        <el-form-item :label-width="formLabelWidth">
          {{form.title}}
        </el-form-item>
        <el-form-item :label-width="formLabelWidth">
          {{form.orgName}}
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" prop="level">
          <div class='labelBox'>专家平台序号</div>
          <el-input type="number" v-model.number="form.level" placeholder="最多三位数字" class="conBox" min="0" max="999" ></el-input>
        </el-form-item>
        <el-form-item class="modiaB">
          <el-button type="primary" @click="submitForm('form')">确定</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script>
  import { sortUrl } from '@/api/specialExpert'
  export default {
    data() {
      //  校验排序字段
      const checkAge = (rule, value, callback) => {
        const par = /^[0-9]*[1-9][0-9]*$/
        if (!value) {
          if (value === 0) {
            return callback('填写数字大于0')
          }
          return callback('请填写数字')
        } else {
          if (par.test(value)) {
            if (value > 999) {
              return callback('请最多填写三位正整数数字')
            }
            return callback()
          } else {
            callback('请最多填写三位正整数数字')
          }
        }
      }
      return {
        dialogFormVisible: false,
        form: {
          level: ''
        },
        rules2: {
          level: [
            { validator: checkAge, required: true }
          ]
        },
        formLabelWidth: '20px'
      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$http.post(sortUrl, { id: this.form.id, level: this.form.level }, (response) => {
              if (response.success) {
                this.$emit('rearrange')
                this.dialogFormVisible = false
              }
            })
          } else {
            return false
          }
        })
      }
    }
  }
</script>

<style scoped>
  .modiaB {
    text-align: center;
    margin: 30px 0;
  }

  .box {
    overflow: hidden;
  }

  .t-left {
    float: left
  }
  .headPhoto img {
    width: 100px;
    height: 100px;
    border: 1px solid #E5E5E5;
    border-radius: 50%;
  }

  .form-el {
    float: left;
    margin-bottom: 20px;
  }

  .form-el .el-form-item {
    margin-bottom: 5px;
  }

  .labelBox {
    float: left;
    margin-right: 10px;
  }

  .conBox {
    float: left;
    width: 200px;
  }

  .dialog-footer {
    text-align: center;
  }
</style>