<template>
  <a-card :bordered="false" class="userManage-page-info">
    <!-- 搜索条件 -->
    <div class="userManage-search">
      <a-form-model layout="inline" :model="queryParam" v-bind="formItemLayout" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="24">
          <a-col :xs="24" :sm="12" :md="6" :lg="5">
            <a-form-model-item label="司机姓名" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input allowClear placeholder="请输入司机姓名" v-model.trim="queryParam.name" id="driverManage-name" :maxLength="30"/>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="6" :lg="5">
            <a-form-model-item label="手机号码" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <a-input allowClear placeholder="请输入手机号码" :maxLength="11" v-model.trim="queryParam.phone" id="driverManage-phone" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="6" :lg="5">
            <a-form-model-item label="状态" :label-col="formItemLayout.labelCol" :wrapper-col="formItemLayout.wrapperCol">
              <v-select code="ENABLE_FLAG" v-model="queryParam.loginFlag" allowClear placeholder="请选择状态" id="driverManage-loginFlag"></v-select>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12" :md="6" :lg="5">
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin: 4px 10px 0 20px" id="driverManage-refresh">查询</a-button>
            <a-button type="" @click="reset" style="margin-top: 4px" id="driverManage-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <!-- 新增 -->
    <div class="table-operator">
      <a-button
        type="primary"
        icon="plus"
        @click="openModal"
        id="driverManage-openModal"
        style="margin-top: 18px;"
        v-hasPermission="'B_driverManage_add'">新增</a-button>
    </div>
    <!-- 列表 -->
    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      bordered>
      <template slot="loginFlag" slot-scope="text, record">
        <span :style="{'color': (record.loginFlag == 0 ? 'red' : 'green')}">{{ record.loginFlag == 0 ? '禁用' :record.loginFlag == 1 ? '启用' : '--' }}</span>
      </template>
      <span slot="action" slot-scope="text, record">
        <a-icon type="unlock" title="重置密码" class="actionBtn icon-orange" @click="resetPassword(record)" v-hasPermission="'B_driverManage_restPwd'"/>
        <a-icon type="edit" title="编辑" class="actionBtn icon-blues" @click="handleEdit(record)" v-hasPermission="'B_driverManage_edit'"/>
        <a-icon v-if="record.loginFlag == 0 && $hasPermissions('B_driverManage_del')" type="delete" title="删除" class="actionBtn icon-red" @click="delect(record)" />
        <span v-if="!$hasPermissions('B_driverManage_edit') && !($hasPermissions('B_driverManage_restPwd')) && !($hasPermissions('B_driverManage_del') && record.loginFlag==0) ">--</span>
      </span>
    </s-table>
    <!-- 新增、编辑弹窗 -->
    <addDiverModal :visible="showModal" @refresh="refreshTable" :itemId="itemId" @close="cancel"></addDiverModal>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import addDiverModal from './addDiverModal.vue'
import { resetPSD, getDriverList, delectDriver } from '@/api/cleanManage.js'
export default {
  name: 'DriverManage',
  components: { STable, VSelect, addDiverModal },
  data () {
    return {
      formItemLayout: {
        labelCol: {
          span: 7
        },
        wrapperCol: {
          span: 17
        }
      },
      // 查询参数
      queryParam: {
        name: '',
        phone: '',
        loginFlag: ''
      },
      showModal: false,
      itemId: null, // 编辑行id
      // 表头
      columns: [{ title: '序号', dataIndex: 'no', width: 60, align: 'center' },
        { title: '司机姓名', dataIndex: 'name', width: 150, align: 'center' },
        { title: '手机号码', dataIndex: 'phone', width: 100, align: 'center' },
        { title: '状态', dataIndex: 'loginFlag', width: 100, align: 'center', scopedSlots: { customRender: 'loginFlag' } },
        { title: '操作', width: 100, align: 'center', scopedSlots: { customRender: 'action' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getDriverList(Object.assign(parameter, this.queryParam)).then(res => {
          const no = (res.data.pageNo - 1) * res.data.pageSize
          for (let i = 0; i < res.data.list.length; i++) {
            const _item = res.data.list[i]
            _item.no = no + i + 1
            _item.loginFlag = _item.loginFlag + '' === '1'
          }
          return res.data
        })
      }
    }
  },
  methods: {
    cancel () {
      this.itemId = null
      this.showModal = false
    },
    // 刷新列表
    refreshTable () {
      if (this.itemId) {
        this.$refs.table.refresh()
      } else {
        this.$refs.table.refresh(true)
      }
    },
    // refreshTable () {
    //   if (!this.itemId) {
		  // console.log(!this.itemId)
    //     this.$refs.table.refresh()
    //   }
    // },
    // 新建
    openModal () {
      this.showModal = true
      this.itemData = {}
    },
    // 重置
    reset () {
      this.queryParam.name = ''
      this.queryParam.phone = ''
      this.queryParam.loginFlag = ''
      this.refreshTable()
    },
    // 重置密码
    resetPassword (row) {
      resetPSD({
        id: row.id
      }).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
          this.$refs.table.refresh()
        }
      })
    },
    // 删除
    delect (row) {
      const _this = this
      this.$confirm({
        title: '警告',
        centered: true,
        content: '删除后无法恢复，确认删除?',
        okText: '确定',
        cancelText: '取消',
        onOk () {
          delectDriver({
            id: row.id
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
            }
          })
        }
      })
    },
    // 编辑
    handleEdit (row) {
      this.itemId = row.id
      this.showModal = true
    }
  }
}
</script>
<style lang="less">
    .userManage-page-info {
        .userManage-search {
            .ant-form-inline .ant-form-item {
                width: 100%;
            }
        }
    }
</style>
