<template>
  <div class="mod-driverMsg app-container">
    <!-- 卡片视图区域 -->

    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.name" placeholder="驾驶员姓名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.tel" placeholder="联系电话" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button
          v-hasPermi="['fleet:driverMsg:add']"
          type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <!--        <el-button v-if="isAuth('fleet:driverMsg:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="companyName"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        width="150"
        label="所属公司">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        width="150"
        label="驾驶员姓名">
      </el-table-column>
      <el-table-column
        prop="sex"
        header-align="center"
        align="center"
        width="150"
        :formatter="formatSex"
        label="性别">
      </el-table-column>
      <el-table-column
        prop="birthday"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        width="160"
        :formatter="timestampToTime"
        label="出生日期">
      </el-table-column>
      <el-table-column
        prop="allowDriverCar"
        header-align="center"
        align="center"
        label="准驾车型">
      </el-table-column>
      <el-table-column
        prop="firstCertificateDate"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        :formatter="dateFormat"
        width="160"
        label="初次领证日期">
      </el-table-column>
      <el-table-column
        prop="idcarNum"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        width="170"
        label="身份证号">
      </el-table-column>
      <el-table-column
        prop="tel"
        header-align="center"
        align="center"
        width="150"
        label="联系电话">
      </el-table-column>
      <el-table-column
        prop="address"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="家庭住址">
      </el-table-column>
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="个人照片">
        <template slot-scope="scope">
          　　　　<img v-if="scope.row.resumePic? 'true':''" :src="scope.row.resumePic" width="80" height="60" class="head_pic"/>
          　　</template>
      </el-table-column>
      <el-table-column
        prop="drivingPic"
        header-align="center"
        align="center"
        width="150"
        label="驾驶证照片">
        <template slot-scope="scope">
          　　　　<img v-if="scope.row.drivingPic? 'true':''" :src="scope.row.drivingPic" width="80" height="60" class="head_pic"/>
          　　</template>
      </el-table-column>
      <el-table-column
        prop="creatTime"
        header-align="center"
        align="center"
        width="160"
        :show-overflow-tooltip="true"
        label="注册时间">
      </el-table-column>

      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="修改" :enterable="false" placement="top">
            <el-button type="primary"
                       v-hasPermi="['fleet:driverMsg:edit']"
                       icon="el-icon-edit"
                       content="修改"
                       size="mini"
                       circle @click="addOrUpdateHandle(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" :enterable="false" placement="top">
            <el-button type="danger"
                       v-hasPermi="['fleet:driverMsg:delete']"
                       icon="el-icon-delete"
                       size="mini"
                       circle @click="deleteHandle(scope.row.id)"></el-button>
          </el-tooltip>
        </template>

      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="dataForm.pageNum"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="dataForm.pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" :dataForm="editModel"></add-or-update>

  </div>
</template>

<script>
import {listFtmsorderbills} from '@/api/fleet/driverMsg'
  import AddOrUpdate from './driverMsg-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          name: null,
          tel: null,
          pageNum: 1,
          pageSize: 10
        },
        editModel: {},
        dataList: [],
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      timestampToTime (row, column) {
        const daterc = row[column.property]
        if (daterc != null) {
          const date = new Date(row.birthday)
          const Y = date.getFullYear() + '-'
          const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
          const D = date.getDate() + ' '
          return Y + M + D
        }
      },
      dateFormat (row, column, cellValue, index) {
        const daterc = row[column.property]
        if (daterc != null) {
          const dateMat = new Date(row.firstCertificateDate)
          const year = dateMat.getFullYear()
          const month = dateMat.getMonth() + 1
          const day = dateMat.getDate()
          const timeFormat = year + '-' + month + '-' + day
          return timeFormat
        }
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        listFtmsorderbills(this.dataForm).then(data=>{
          console.log(data)
          if (data && data.code === 200) {
            this.dataList = data.rows
            this.totalPage = data.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        console.log(val)
        this.dataForm.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.dataForm.current = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (row) {
        this.addOrUpdateVisible = true
        if (row) {
          this.editModel = row
        } else {
          this.editModel = {
            id: null,
            fleetCompanyId: null,
            name: null,
            sex: null,
            birthday: null,
            allowDriverCar: null,
            firstCertificateDate: null,
            idcarNum: null,
            tel: null,
            address: null,
            resumePic: null,
            drivingPic: null,
            creatTime: null,
            modificationTime: null,
            updateType: null
          }
        }
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init()
        })
      },
      // 删除
      deleteHandle (id) {
        var driverMsgIds = id ? [id] : this.dataListSelections.map(item => {
          return item.userId
        })
        this.$confirm(`确定对[id=${driverMsgIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/fleet/driverMsg/delete'),
            method: 'post',
            data: this.$http.adornData(driverMsgIds, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      formatSex: function (row, column) {
        return row.sex === 0 ? '男' : row.sex === 1 ? '女' : '未知'
      }
    }
  }
</script>
