<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="司机" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入司机名称"
          style="width: 140px"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          style="width: 140px"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工作状态" prop="driveStatus">
        <el-select
          v-model="queryParams.driveStatus"
          placeholder="工作状态"
          clearable
          style="width: 140px"
        >
          <el-option
            v-for="dict in workDicts"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="认证状态" prop="authStatus">
        <el-select
          v-model="queryParams.authStatus"
          placeholder="认证状态"
          clearable
          style="width: 140px"
        >
          <el-option
            v-for="dict in authDicts"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center">
        <template scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="nickName" width="100">
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex" width="100">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_USER_SEX" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phonenumber" width="130"/>
      <el-table-column label="年龄" align="center" prop="age" width="100"/>
      <el-table-column label="图片" align="center" prop="avatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="认证状态" align="center" prop="authStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.authStatus =='0'" style="color: #1c84c6">待认证</span>
          <span v-if="scope.row.authStatus =='1'" style="color: #1c84c6">待审核</span>
          <span v-if="scope.row.authStatus =='2'" style="color: green">认证成功</span>
          <span v-if="scope.row.authStatus =='3'" style="color: red">驳回</span>
        </template>
      </el-table-column>
      <el-table-column label="接单状态" align="center" prop="driverStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.driverStatus =='1'">空闲</span>
          <span v-if="scope.row.driverStatus =='2'">暂停</span>
          <span v-if="scope.row.driverStatus =='3'">忙碌</span>
          <span v-if="scope.row.driverStatus =='4'">离线</span>
        </template>
      </el-table-column>
      <el-table-column label="当前所在位置" align="center" prop="driverPositionInfo" width="180"/>
      <el-table-column label="司机离线时间" align="center" prop="driverOfflineTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.driverOfflineTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.authStatus =='1'"
            icon="el-icon-s-check"
            @click="handleAuth(scope.row)"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="认证审核" :visible.sync="authFlag" width="400px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="审核结果" prop="authStatus">
          <el-radio-group v-model="form.authStatus">
            <el-radio label="2">通过</el-radio>
            <el-radio label="3">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="拒绝原因" prop="rejectInfo">
          <el-input type="textarea" v-model="form.rejectInfo" placeholder="请输入拒绝原因"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="authSubmit">确 定</el-button>
        <el-button @click="authCancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-tabs :value="'first'" @tab-click="handleClick">
        <el-tab-pane label="司机信息" name="first">
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-form-item v-show="false" label="用户主键" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入用户主键"/>
            </el-form-item>
            <el-row>
              <el-col :span="12">
                <el-form-item label="真实姓名" prop="realName">
                  <el-input v-model="form.realName" placeholder="真实姓名"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="手机号" prop="phonenumber">
                  <el-input v-model="form.phonenumber" placeholder="手机号"/>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="司机性别">
                  <el-select v-model="form.sex" placeholder="请选择性别">
                    <el-option
                      v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_USER_SEX)"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="年龄" prop="age">
                  <el-input-number v-model="form.age" placeholder="年龄"/>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="身份证正面" prop="idCardRight">
                  <image-upload :limit="1" v-model="form.idCardRight"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="身份证反面" prop="idCardBack">
                  <image-upload :limit="1" v-model="form.idCardBack"/>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="12">
                <el-form-item label="驾驶证照片" prop="licenseImage">
                  <image-upload :limit="1" v-model="form.licenseImage"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="司机照片" prop="avatar">
                  <image-upload :limit="1" v-model="form.avatar"/>
                </el-form-item>
              </el-col>
            </el-row>

          </el-form>

        </el-tab-pane>
        <el-tab-pane label="车辆信息" name="second">
          <el-form ref="carfrom" :model="carfrom" :rules="carRules" label-width="120px">
            <div v-for="(item,index) in carfrom.carInfoList" :key="index" style="width: 100%; ">
              <el-collapse v-model="activeNames" accordion>
                <el-collapse-item :title=" '车辆'+ (index+1)" :name="index" accordion>
                  <el-row>
                    <el-col :span="12">
                      <div style="position: relative">
                        <div style="position: absolute; left: 35px;top: 10px; color: red">*</div>
                        <el-form-item label=" 车辆类型" prop="rescueCarType">
                          <el-select v-model="item.rescueCarType" placeholder="请选择车辆类型">
                            <el-option
                              v-for="dict in getDictDatasNew(DICT_TYPE.JYC_TYPE)"
                              :key="dict.value"
                              :label="dict.label"
                              :value="dict.value"
                            ></el-option>
                          </el-select>
                        </el-form-item>
                      </div>


                    </el-col>
                    <el-col :span="12">
                      <div style="position: relative">
                        <!--                    <div style="position: absolute; left: 35px;top: 10px; color: red">*</div>-->
                        <el-form-item label=" 车牌号" :prop="'rescueCarNum'+index">
                          <el-input v-model="item.rescueCarNum" @input="valid(item.rescueCarNum,index,'rescueCarNum')"
                                    placeholder="请输入车牌号"
                          />
                        </el-form-item>
                      </div>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="12">
                      <div style="position: relative">
                        <div style="position: absolute; left: 35px;top: 10px; color: red">*</div>
                        <el-form-item label=" 品牌型号" prop="rescueCarBrand">
                          <el-input v-model="item.rescueCarBrand" placeholder="请输入品牌型号"/>
                        </el-form-item>
                      </div>
                    </el-col>
                    <el-col :span="12">

                    </el-col>
                  </el-row>


                  <el-row>
                    <el-col :span="12">
                      <div style="position: relative">
                        <div style="position: absolute; left: 35px;top: 10px; color: red">*</div>
                        <el-form-item label=" 购买时间" prop="carBuyTime">
                          <el-date-picker clearable
                                          v-model="item.carBuyTime"
                                          type="date"
                                          value-format="yyyy-MM-dd"
                                          placeholder="请选择购买时间"
                          >
                          </el-date-picker>
                        </el-form-item>
                      </div>
                    </el-col>
                    <el-col :span="12">
                      <div style="position: relative">
                        <div style="position: absolute; left: 35px;top: 10px; color: red">*</div>
                        <el-form-item label=" 车架号" prop="frameNumber">
                          <el-input v-model="item.frameNumber" placeholder="请输入车架号"/>
                        </el-form-item>
                      </div>
                    </el-col>
                  </el-row>


                  <el-row>
                    <el-col :span="12">
                      <div style="position: relative">
                        <div style="position: absolute; left: 35px;top: 10px; color: red">*</div>
                        <el-form-item label="车辆性质" prop="carUseNature">
                          <el-radio-group v-model="item.carUseNature">
                            <el-radio
                              v-for="dict in getDictDatasNew(DICT_TYPE.CAR_USE_NATURE)"
                              :key="dict.value"
                              :label="dict.value"
                            >{{ dict.label }}
                            </el-radio>
                          </el-radio-group>
                        </el-form-item>
                      </div>
                    </el-col>
                    <el-col :span="12">
                      <div style="position: relative">
                        <div style="position: absolute; left: 35px;top: 10px; color: red">*</div>
                        <el-form-item label=" 车牌颜色" prop="carLicenseColor">
                          <el-radio-group v-model="item.carLicenseColor">
                            <el-radio
                              v-for="dict in getDictDatasNew(DICT_TYPE.CAR_LICENSE_COLOR)"
                              :key="dict.value"
                              :label="dict.value"
                            >{{ dict.label }}
                            </el-radio>
                          </el-radio-group>
                        </el-form-item>
                      </div>
                    </el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="12">
                      <div style="position: relative">
                        <div style="position: absolute; left: 35px;top: 10px; color: red">*</div>
                        <el-form-item label="行驶证" prop="driveLicenseImage">
                          <image-upload v-model="item.driveLicenseImage"/>
                        </el-form-item>
                      </div>
                    </el-col>
                    <el-col :span="12">
                      <div style="position: relative">
                        <div style="position: absolute; left: 35px;top: 10px; color: red">*</div>
                        <el-form-item label="图片" prop="carImage">
                          <image-upload v-model="item.carImage"/>
                        </el-form-item>
                      </div>
                    </el-col>
                  </el-row>

                  <div style="width: 100%; display: flex; align-items: center;justify-content: flex-end ">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="deletecomm(index)"
                    >删除
                    </el-button>
                  </div>
                </el-collapse-item>
              </el-collapse>
            </div>
            <div style="width: 100%; margin: 20px auto; display: flex;align-items: center; justify-content: flex-end ">
              <!--            <el-button type="primary" icon="el-icon-plus" @click="addCommodity()" >新增车辆信息</el-button>-->
            </div>
          </el-form>
        </el-tab-pane>
      </el-tabs>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { driverList, delDriver, addDriver, updateDriver } from '@/api/rescue/driver'

export default {
  name: 'Info',
  data() {
    var verify = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('不能为空'))
        this.pd = false
      } else {
        callback()
      }

    }
    return {
      pd: true,
      switchover: 0,
      activeNames: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      infoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      workDicts: [
        {
          label: '空闲',
          value: '1'
        },
        {
          label: '暂停',
          value: '2'
        },
        {
          label: '忙碌',
          value: '3'
        },
        {
          label: '离线',
          value: '4'
        }
      ],
      authDicts: [
        {
          label: '待认证',
          value: '0'
        },
        {
          label: '待审核',
          value: '1'
        },
        {
          label: '认证成功',
          value: '2'
        },
        {
          label: '认证失败',
          value: '3'
        }
      ],
      authFlag: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        userId: null,
        licenseImage: null,
        idCardRight: null,
        idCardBack: null,
        driveLicenseImage: null,
        carBrand: null,
        carLicenseNum: null,
        carType: null,
        carNum: null,
        carBuyTime: null,
        carLicenseColor: null,
        authStatus: null,
        rejectInfo: null,
        driverStatus: null,
        driverLongitude: null,
        driverLatitude: null,
        driverPositionInfo: null,
        driverOfflineTime: null,
        driveStatus: null,
        phonenumber: null
      },
      carfrom: {
        carInfoList: [
          {
            rescueCarType: '1',
            rescueCarNum: '',
            rescueCarBrand: '',
            carOwn: '',
            carImage: '',
            driveLicenseImage: '',
            carBuyTime: '',
            carLicenseColor: '',
            carUseNature: '',
            frameNumber: '',
            possessorId: '',
            deptId: ''
          }
        ]
      },

      // 表单参数
      form: {
        sex: '1'
      },
      carRules: {},
      // 表单校验
      rules: {
        realName: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 1, max: 11, message: '长度在 1 到 11 个字符', trigger: 'blur' }
        ],
        phonenumber: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 1, max: 11, message: '长度在 1 到 11 个字符', trigger: 'blur' }
        ],
        rescueCarType: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        rescueCarNum: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        carOwn: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        carImage: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        driveLicenseImage: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        carLicenseColor: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        carUseNature: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        frameNumber: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        possessorId: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getDictDatasNew(str){
      return this.getDictDatas(str)
    },
    valid(value, idx, str) {
      this.$set(this.carfrom, str + idx, value)
    },
    handleClick(tab, event) {
      // console.log(tab, event)
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true
      driverList(this.queryParams).then(response => {
        this.infoList = response.data.records
        this.total = response.data.total
        this.loading = false
      })
    },
    addCommodity() {
      let data = {
        rescueCarType: null,
        rescueCarNum: null,
        rescueCarBrand: null,
        carOwn: null,
        carImage: null,
        driveLicenseImage: null,
        carBuyTime: null,
        carLicenseColor: null,
        carUseNature: null,
        frameNumber: null,
        possessorId: null,
        deptId: null
      }

      this.carfrom.carInfoList.push(data)
      let idx = this.carfrom.carInfoList.length - 1
      let key = 'rescueCarNum' + idx
      this.$set(this.carRules, key, [
        { required: true, message: '不能为空', trigger: 'blur' }
      ])
    },
    deletecomm(index) {
      this.carfrom.carInfoList.splice(index, 1)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    authCancel() {
      this.authFlag = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.resetForm('form')
      this.form = {
        id: null,
        userId: null,
        licenseImage: null,
        idCardRight: null,
        idCardBack: null,
        driveLicenseImage: null,
        carBrand: null,
        carLicenseNum: null,
        carType: null,
        carNum: null,
        carBuyTime: null,
        carLicenseColor: null,
        authStatus: null,
        rejectInfo: null,
        driverStatus: null,
        driverLongitude: null,
        driverLatitude: null,
        driverPositionInfo: null,
        driverOfflineTime: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        sex: '1'
      }
      this.carfrom.carInfoList = [
        {
          rescueCarType: '',
          rescueCarNum: '',
          rescueCarBrand: '',
          carOwn: '',
          carImage: '',
          driveLicenseImage: '',
          carBuyTime: '',
          carLicenseColor: '',
          carUseNature: '',
          frameNumber: '',
          possessorId: '',
          deptId: ''
        }
      ]

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加司机'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.reset()
      this.form = row
      this.form.realName = row.nickName
      this.carfrom.carInfoList = !!row.carInfoList ? row.carInfoList : this.carfrom.carInfoList
      this.open = true
      this.title = '修改司机信息'

    },
    handleAuth(row) {
      this.reset()
      this.form = row
      this.authFlag = true
    },
    //提交前的处理

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$refs['carfrom'].validate(carValid => {
            if (carValid) {
              if (this.form.id != null) {
                this.form.carInfoList = this.carfrom.carInfoList
                updateDriver(this.form).then(response => {
                  this.$modal.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                })
              } else {
                this.form.carInfoList = this.carfrom.carInfoList[0].carOwn ? this.carfrom.carInfoList : null
                addDriver(this.form).then(response => {
                  this.$modal.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                })
              }

            }
          })
        }
      })
    },
    authSubmit() {
      updateDriver(this.form).then(response => {
        this.$modal.msgSuccess('审核成功')
        this.authFlag = false
        this.getList()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
        return delDriver(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
