<template>
  <my-page>
    <template slot="left">
      <CustList style="width: 320px;" custType="1" @change="handleCustsChange"></CustList>
    </template>
    <div class="app-container">
      <Search :show-search="showSearch" @search="handleSearch" />
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['tm:driver:add']"
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['tm:driver:edit']"
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['tm:driver:remove']"
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['tm:driver:export']"
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
      </el-row>

      <el-table
        v-loading="loading"
        :data="driverList"
        :highlight-current-row="true"
        @sort-change="handleSortChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="" type="index" width="55" align="center" />
        <el-table-column label="姓名" align="center" prop="driverName" sortable="custom" />
        <el-table-column label="性别" align="center" prop="sex" sortable="custom" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
          </template>
        </el-table-column>
        <el-table-column label="企业名称" align="center" prop="custName" sortable="custom" />
        <el-table-column label="驾驶证类型" align="center" prop="licenseType" sortable="custom">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.license_type" :value="scope.row.licenseType" />
          </template>
        </el-table-column>
        <el-table-column label="手机号码" align="center" prop="mobileNumber" sortable="custom" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['tm:driver:query']"
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleRead(scope.row)"
            >查看</el-button>
            <el-button
              v-hasPermi="['tm:driver:edit']"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              v-hasPermi="['tm:driver:remove']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    <pagination hide-on-single-page :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改驾驶员信息对话框 -->
    <el-dialog v-dialogDrag :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false">
      <DriverEdit ref="edit" v-if="open" :bid="showId" :readonly="readonly" @saved="refresh"></DriverEdit>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!readonly">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
  </my-page>

</template>

<script>
import { pageListDriver, getDriver, delDriver, addDriver, updateDriver } from '@/api/tm/driver'
import CustList from '@/views/tm/components/CustList'
import DriverEdit from '@/views/tm/components/DriverEdit'
import Search from './modules/search.vue'
export default {
  name: 'Driver',
  dicts: ['license_type', 'sys_user_sex'],
  components: {
    CustList,
    DriverEdit,
    Search
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中名字
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 驾驶员信息表格数据
      driverList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        custCode: null,
        driverName: null,
        SEX: null,
        birthDate: null,
        mobileNumber: null,
        licenseType: null,
        licenseNo: null,
        idNumber: null,
        validBegin: null,
        validEnd: null,
        nextinspecTime: null,
        driveModel: null,
        STATUS: null,
        createUser: null,
        createDate: null,
        modifyUser: null,
        modifyDate: null,
        REMARK: null,
        MOTORCADE: null,
        carNo: null,
        truckNo: null,
        driverType: null,
        driverAttribute: null,
        pinyinCode: null,
        entryDate: null,
        repartureDate: null,
        repartureStatus: null,
        professionalCertificateId: null,
        drivingCertificateId: null,
        identityCardId: null,
        orderByColumn: 'createTime',
        isAsc: 'DESC',
      },
      showId: null,
      readonly: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleCustsChange(custs) {
      const cust = custs[0] || {};
      this.queryParams.custId = cust.id;
      this.handleSearch();
    },
    /** 查询驾驶员信息列表 */
    getList() {
      this.loading = true
      pageListDriver(this.queryParams).then(response => {
        this.driverList = response.rows
        this.total = response.total
        this.loading = false
      }).catch(e => {
        this.loading = false
      })
    },
    refresh() {
      this.open = false;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false
    },
    // 排序
    handleSortChange(col) {
      this.$sortBy(col, this.queryParams)
      this.getList()
    },
    /**
     * 搜索
     * @param {*} payload 搜索参数
     */
    handleSearch(payload) {
      this.queryParams.pageNum = 1
      this.queryParams = {
        ...this.queryParams,
        ...payload
      }
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.driverId)
      this.names = selection.map(item => item.driverName)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleRead(row) {
      const id = row.driverId || this.ids
      this.open = true
      this.showId = id
      this.readonly = true
      this.title = '查看司机信息'
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.showId = ''
      this.readonly = false
      this.title = '新增司机'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.driverId || this.ids
      this.open = true
      this.showId = id
      this.readonly = false
      this.title = '编辑司机信息'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.edit.submitForm()
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const driverIds = row.driverId || this.ids
      const names = row.driverName || this.names
      this.$modal.confirm('是否确认删除"' + names + '"这些司机？').then(function() {
        return delDriver(driverIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tm/driver/export', {
        ...this.queryParams
      }, `driver_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
