<template>
  <div class="app-container">
    <el-form ref="postForm" :rules="formRules" :model="formData" label-width="150px">
      <!-- <el-form-item label="司机名称：" prop="driver_name">
        <el-col :span="6">
          <el-input v-model="formData.driver_name" placeholder="请输入司机名称"/>
        </el-col>
      </el-form-item>
      <el-form-item label="性别：" prop="driver_gender">
        <el-col :span="6">
          <el-select v-model="formData.driver_gender" placeholder="请选择" clearable>
            <el-option label="男" :value="1" ></el-option>
            <el-option label="女" :value="2" ></el-option>
          </el-select>
        </el-col>
      </el-form-item> -->
      <el-form-item label="车牌号：" prop="account">
        <el-col :span="6">
          <el-input v-model="formData.account" placeholder="请输入车牌号"/>
          <span class="red font-16">(登录账号)</span>
        </el-col>
      </el-form-item>
      <el-form-item label="联系电话：" prop="driver_phone">
        <el-col :span="6">
          <el-input v-model="formData.driver_phone" placeholder="请输入联系电话"/>
        </el-col>
      </el-form-item>
      <el-form-item label="上级调度员：" prop="yardman_id">
        <el-col :span="6">
          <el-select v-model="formData.yardman_id" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in yardmanList" 
              :key="index"
              :label="item.yardman_name" :value="item.yardman_id" >
            </el-option>
          </el-select>
          <!-- <el-input v-model="formData.yardman_id" placeholder="请输入上级调度员"/> -->
        </el-col>
      </el-form-item>
      <el-form-item label="车辆负责人" prop="head_id">
        <el-col :span="6">
          <el-select v-model="formData.head_id" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in headList" 
              :key="index"
              :label="item.head_name" :value="item.head_id" >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="登录密码：" prop="password">
        <el-col class="flex" :span="6">
          <el-input :disabled="!passwordValue" v-model="formData.password" placeholder="请输入登录密码"/>
          <el-switch class="ml-10 mt-10" v-if="$props.id" v-model="passwordValue" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
        </el-col>
      </el-form-item>
      <el-form-item label="账户状态：" prop="freeze_flag">
        <el-col :span="6">
          <el-select v-model="formData.freeze_flag" placeholder="请选择" clearable>
            <el-option label="正常" :value="false" ></el-option>
            <el-option label="冻结" :value="true" ></el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { checkRqInput, checkPwdLength, checkTel, checkLicense } from '@/utils/validate'
import { detailDriver, updateDriver, createDriver, getYardman, getHeadList  } from '@/api/user/index'

export default {
  name: 'ArticleDetail',
  // 配合路由配置的props属性实现在组件中通过props获取参数
  props: {
    id: {
      type: String,
      default() {
        return this.$route.params.id;
      }
    }
  },
  data() {
    return {
      formData: {
        freeze_flag: false
      },
      // 校验规则
      formRules: {
        driver_name: [ checkRqInput ],
        driver_gender: [ checkRqInput ],
        account: [ checkRqInput, checkLicense ],
        driver_phone: [ checkRqInput, checkTel ],
        yardman_id: [ checkRqInput ],
        head_id: [ checkRqInput ],
        password: [ checkRqInput, checkPwdLength ],
        freeze_flag: [ checkRqInput ]
      },
      passwordValue: this.$props.id?false:true, // 密码按钮状态
      yardmanList: [], // 调度员列表
      headList: [], // 车辆负责人列表
    }
  },
  created() {
    this.fetchYardman()
    this.fetchHead()
    this.fetchDetail()
  },
  methods: {
    // 获取上级调度员列表
    fetchYardman() {
      this.$http(getYardman(), (res) => {
        if (res.code == 0) {
          this.yardmanList = res.data
        }
      })
    },

    // 获取上级调度员列表
    fetchHead() {
      this.$http(getHeadList(), (res) => {
        if (res.code == 0) {
          this.headList = res.data
        }
      })
    },

    // 获取企业详情
    fetchDetail() {
      const id = this.$props.id
      if (id) {
        this.$http(detailDriver(id), (res) => {
          if (res.code == 0) {
            this.formData = res.data
          }
        })
      }
      this.formData.driver_id = id
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          // 修改
          if (this.formData.driver_id) {
            this.$http(updateDriver(this.formData), res => {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.go(-1)
            })
          } else {
            // 新增
            this.$http(createDriver(this.formData), res => {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.go(-1)
            })
          }
        } else {
          return false
        }
      })
    },
    // 取消
    onCancel() {
      this.$message({
        message: '已取消!',
        type: 'warning'
      })
      this.$router.go(-1)
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .license {
    position: relative;
    left: -150px;
    top: -20px;
  }
</style>
