
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible" width="70%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="200px">
      <el-form-item label="驾驶员所属公司" prop="fleetCompanyId">
        <el-select v-model="dataForm.fleetCompanyId" placeholder="请选择">
          <el-option
            v-for="item in optionsProjectTypeItem"
            :key="item.id"
            :label="item.companyName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
          <el-form-item label="驾驶员姓名" prop="name">
            <el-input v-model="dataForm.name" placeholder="驾驶员姓名"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
<!--            <el-input v-model="dataForm.sex" placeholder="性别（0男/1女）"></el-input>-->
            <el-radio-group style="margin-left: 20px;" v-model="dataForm.sex">
              <el-radio :label="0">男</el-radio>
              <el-radio :label="1">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="出生日期" prop="birthday">
<!--            <el-input v-model="dataForm.birthday" placeholder="出生日期"></el-input>-->
            <el-date-picker
              style="width: 100%"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="dataForm.birthday"
              type="date"
              placeholder="出生日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="准驾车型" prop="allowDriverCar">
            <el-input v-model="dataForm.allowDriverCar" placeholder="准驾车型"></el-input>
          </el-form-item>
          <el-form-item label="初次领证日期" prop="firstCertificateDate">
<!--            <el-input v-model="dataForm.firstCertificateDate" placeholder="初次领证日期"></el-input>-->
            <el-date-picker
              style="width: 100%"
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="dataForm.firstCertificateDate"
              type="date"
              placeholder="初次领证日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="身份证号" prop="idcarNum">
            <el-input v-model="dataForm.idcarNum" placeholder="身份证号"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="tel">
            <el-input v-model="dataForm.tel" placeholder="联系电话"></el-input>
          </el-form-item>
          <el-form-item label="家庭住址" prop="address">
            <el-input v-model="dataForm.address" placeholder="家庭住址"></el-input>
          </el-form-item>
          <el-form-item label="个人照片" prop="resumePic">
<!--            <el-input v-model="dataForm.resumePic" placeholder="个人照片"></el-input>-->
            <el-upload
              class="upload-demo"
              :limit="1"
              :multiple="false"
              :action="uploadURL"
              :file-list="fileList"
              :headers = "uploadHeaders"
              :on-success="handleSuccess1"
              :on-remove="handleRemove"
              list-type="picture">
              <el-button size="small" type="primary">点击上传</el-button>
              <!--              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
            </el-upload>
          </el-form-item>
          <el-form-item label="驾驶证照片" prop="drivingPic">
<!--            <el-input v-model="dataForm.drivingPic" placeholder="驾驶证照片"></el-input>-->
            <el-upload
              class="upload-demo"
              :limit="1"
              :multiple="false"
              :action="uploadURL"
              :file-list="fileList"
              :headers = "uploadHeaders"
              :on-success="handleSuccess"
              :on-remove="handleRemove"
              list-type="picture">
              <el-button size="small" type="primary">点击上传</el-button>
              <!--              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
            </el-upload>
          </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    props: {
      dataForm: {
        type: Object,
        require: false
      }
    },
    data () {
      return {
        visible: false,
        dataRule: {
          fleetCompanyId: [
            {required: true, message: '请选择驾驶员所属公司id', trigger: 'blur'}
          ],
          name: [
            {required: true, message: '请输入驾驶员姓名', trigger: 'blur'}
          ],
          idcarNum: [
            {required: true, message: '请输入身份证号', trigger: 'blur'}
          ],
          tel: [
            {required: true, message: '请输入联系电话', trigger: 'blur'}
          ]
        },
        optionsProjectTypeItem: [],
        fileList: [],
        uploadHeaders: {
          // localStorage.setItem('token', data.token)
          token: localStorage.getItem('token')
        },
        uploadURL: 'http://test.echepei.com/api/admin/fleet/carManage/uplodaImgForDrivingLicense'
      }
    },
    methods: {
      init () {
        this.visible = true
        this.needCondition()
      },
      // 获取下拉选项
      needCondition () {
        this.$http({
          url: this.$http.adornUrl(`/fleet/companyMsg/list`),
          method: 'post'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.optionsProjectTypeItem = data.list
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 图片上传回调
      handleSuccess (response) {
        // 这里可以写文件上传成功后的处理,但是一定要记得给imgPath赋值
        this.dataForm.drivingPic = response.msg
      },
      handleSuccess1 (response) {
        // 这里可以写文件上传成功后的处理,但是一定要记得给imgPath赋值
        this.dataForm.resumePic = response.msg
      },
      // 文件列表移除文件时的钩子
      handleRemove (response) {
        this.dataForm.imgPath = ''
      },
      // 表单提交
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/fleet/driverMsg/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style scoped>

.el-form-item {
  width: 49%;
  margin-bottom: 20px;
  display: inline-block;
}
.el-select {
  display: block;
}
.upload-demo {
  margin-left: 50px;
}
</style>
