<template>
  <div class="app-container">
    <el-form ref="postForm" :rules="formRules" :model="formData" label-width="150px">
      <el-form-item label="中转站名称：" prop="transit_name" :rules="formRules.checkRqInput">
        <el-col :span="6">
          <el-input v-model="formData.transit_name" placeholder="请输入中转站名称"/>
        </el-col>
      </el-form-item>
      <el-form-item label="车牌号" prop="driver_id" :rules="formRules.checkRqSelect">
        <el-col :span="6">
          <el-select v-model="formData.driver_id" placeholder="请选择" clearable>
            <el-option v-for="(item, index) in driverList" 
              :key="index"
              :label="item.driver_account" :value="item.driver_id" >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="负责人名称：">
        <el-col :span="6">
          <el-input v-model="formData.transit_man" placeholder="请输入负责人名称"/>
        </el-col>
      </el-form-item>
      <el-form-item label="负责人电话：" prop="transit_phone" :rules="formRules.checkTel">
        <el-col :span="6">
          <el-input v-model="formData.transit_phone" placeholder="请输入负责人电话"/>
        </el-col>
      </el-form-item>
      <el-form-item label="中转站地址：">
        <el-col :span="6">
          <el-input v-model="formData.transit_addr" placeholder="请输入中转站地址"/>
        </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, checkRqSelect, checkPwdLength, checkTel } from '@/utils/validate'
import { detailTransfer, updateTransfer, createTransfer, listDriver  } from '@/api/base/index'

export default {
  name: 'ArticleDetail',
  // 配合路由配置的props属性实现在组件中通过props获取参数
  props: {
    id: {
      type: String,
      default() {
        return this.$route.params.id;
      }
    }
  },
  data() {
    return {
      formData: {
        transit_id: '',
        transit_name: '',
        driver_id: '',
        transit_man: '',
        transit_phone: '',
        transit_addr: ''
      },
      // 校验规则
      formRules: {
        checkRqInput,
        checkRqSelect,
        checkTel
        // transit_name: [ checkRqInput ],
        // driver_id: [ checkRqInput ],
        // transit_phone: [ checkTel ],
        // site_address: [ checkRqInput ],
        // client_id: [ checkRqInput ]
      },
      passwordValue: this.$props.id?false:true, // 密码按钮状态
      driverList: [], // 客户单位列表
    }
  },
  created() {
    this.fetchDriver()
    this.fetchDetail()
  },
  methods: {
    // 获取司机列表
    fetchDriver() {
      this.$http(listDriver({}), (res) => {
        if (res.code == 0) {
          this.driverList = res.data
        }
      })
    },
    // 获取详情
    fetchDetail() {
      const id = this.$props.id
      if (id) {
        this.$http(detailTransfer(id), (res) => {
          if (res.code == 0) {
            this.formData = res.data
          }
        })
      }
      this.formData.transit_id = id
    },
    // 提交表单
    submitForm() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          // 修改
          if (this.formData.transit_id) {
            this.$http(updateTransfer(this.formData), res => {
              this.$message({
                message: '成功',
                type: 'success'
              })
              this.$router.go(-1)
            })
          } else {
            // 新增
            this.$http(createTransfer(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>
</style>
