
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible" width="70%">
    <el-form :model="dataFormD" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="200px">
      <el-form-item label="驾驶员所属公司" prop="fleetCompanyId">
        <el-select v-model="dataFormD.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="dataFormD.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="dataFormD.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="dataFormD.birthday"
              type="date"
              placeholder="出生日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="准驾车型" prop="allowDriverCar">
            <el-input v-model="dataFormD.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="dataFormD.firstCertificateDate"
              type="date"
              placeholder="初次领证日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="身份证号" prop="idcarNum">
            <el-input v-model="dataFormD.idcarNum" placeholder="身份证号"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="tel">
            <el-input v-model="dataFormD.tel" placeholder="联系电话"></el-input>
          </el-form-item>
          <el-form-item label="家庭住址" prop="address">
            <el-input v-model="dataFormD.address" placeholder="家庭住址"></el-input>
          </el-form-item>
      <br>
          <el-form-item label="个人照片" prop="resumePic">
            <el-upload
              style="width: 300px"
              :action="uploadImgUrl"
              :before-remove="beforeRemove"
              :on-remove="(res,file)=>{handleRemove(2,res,file)}"
              :on-success="(res,file)=>{handleSuccess(1,res,file)}"
              multiple
              :limit="1"
              :headers="headers"
              :on-exceed="handleExceed">
              <el-button size="small" type="primary">点击上传</el-button>
              <el-button size="small" type="primary" v-if="dataFormD.resumePic" @click="preview(1)">预览</el-button>
            </el-upload>
          </el-form-item>
          <el-form-item label="驾驶证照片" prop="drivingPic">
            <el-upload
              style="width: 300px"
              :action="uploadImgUrl"
              :before-remove="beforeRemove"
              :on-remove="(res,file)=>{handleRemove(2,res,file)}"
              :on-success="(res,file)=>{handleSuccess(2,res,file)}"
              multiple
              :limit="1"
              :headers="headers"
              :on-exceed="handleExceed">
              <el-button size="small" type="primary">点击上传</el-button>
              <el-button size="small" type="primary" v-if="dataFormD.drivingPic" @click="preview(2)">预览</el-button>
            </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>
import { company, lookOver, preservation } from '@/api/fleet/driverMsg'
import { getToken } from '@/utils/auth'

  export default {
    props: {
      dataForm: {
        type: Object,
        require: false
      }
    },
    data () {
      return {
        visible: false,
        dataFormD:{},
        uploadImgUrl: process.env.VUE_APP_BASE_API + '/common/upload', // 上传的图片服务器地址
        headers: {
          Authorization: 'Bearer ' + getToken()
        },
        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: {
      preview(actid){
        if (actid === 1){
          window.open(this.dataFormD.resumePic)
        }else {
          window.open(this.dataFormD.drivingPic)
        }
      },
      handlePreview(file) {
        this.$message.success('如需预览请点击预览');
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 1 个文件件`);
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${ file.name }？`);
      },
      init () {
        this.visible = true
        this.needCondition()
        console.log(this.dataForm)
        if (this.dataForm.id) {
          lookOver(this.dataForm.id).then(data => {
            console.log(data)
            this.dataFormD = data.data
            this.fTmsorderbillsattachs = data.data.fTmsorderbillsattachs
            this.optionsProjectTypeItem = data.data.fleetCompanyMsg
            if (data.data.fleetCompanyMsg){
              this.handleSelect()
            }
          })
        }else {
          this.dataFormD = {}
        }
      },
      // 获取下拉选项
      needCondition () {
        company().then(data => {
          console.log(data)
            if (data && data.code === 200) {
              this.optionsProjectTypeItem = data.rows
            } else {
              this.$message.error(data.msg)
            }
        })
      },
      // 图片上传回调
      handleSuccess (response,res,file) {
        console.log(response)
        if (response === 2){
          console.log("22222")
          this.dataFormD.drivingPic = res.url
          console.log(this.dataFormD.drivingPic)
        }else {
          console.log("11111")
          this.dataFormD.resumePic = res.url
        }
      },
      // 文件列表移除文件时的钩子
      handleRemove (actid,file, fileList) {
        if (actid === 1){
          this.dataForm.resumePic = ''
        }else {
          this.dataForm.drivingPic = ''
        }
      },
      // 表单提交
      dataFormSubmit () {
        console.log(this.dataFormD)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            preservation(this.dataFormD).then(data=>{
              if (data && data.code === 200) {
                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>
