<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="骑手名称">
              <a-input
                allowClear
                v-model.trim="queryParam.userName"
                placeholder="请输入骑手名称"
                id="userList-userName"
                :maxLength="30"
                @pressEnter="$refs.table.refresh(true)" />
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="24">
            <a-form-item label="骑手电话">
              <a-input
                allowClear
                v-model.trim="queryParam.userMobile"
                placeholder="请输入骑手电话"
                :maxLength="11"
                id="userList-userMobile"
                @pressEnter="$refs.table.refresh(true)" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)" id="userList-handSubmit" style="margin-right: 10px;">查询</a-button>
            <a-button type="" @click="reset" id="userList-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="table-operator">
      <a-button
        type="primary"
        icon="plus"
        @click="openModal"
        id="userList-openModal"
        v-hasPermission="'B_riderSet_enable'"
        style="margin-top:18px ;">新增</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      bordered>
      <span slot="status" slot-scope="text, record">
        <a-switch
          v-hasPermission="'B_riderSet_enable'"
          checkedChildren="启用"
          unCheckedChildren="禁用"
          v-model="record.loginFlag"
          @change="changeFlagHandle(text, record)"
          id="userList-changeFlagHandle" />
        <span v-if="!$hasPermissions('B_riderSet_enable')">{{record.loginFlag== true ? '启用' : '禁用'}}</span>
      </span>
      <span slot="action" slot-scope="text, record">
        <a-icon
         v-if="$hasPermissions('B_riderSet_edit')"
          type="edit"
          title="编辑"
          class="actionBtn icon-blues"
          @click="handleEdit(record)"
        />
        <span v-else>--</span>
      </span>
    </s-table>
    <riderModal :visible="showModal" @refresh="$refs.table.refresh(true)" :data="itemData" @close="showModal = false"></riderModal>
  </a-card>
</template>

<script>
import {
  STable,
  VSelect
} from '@/components'
import riderModal from './riderModal.vue'
import {
  getRiderList,
  updateEnableStatus
} from '@/api/recoveryManage.js'
export default {
  name: 'RiderSet',
  components: {
    STable,
    VSelect,
    riderModal
  },
  data () {
    return {
      // 查询参数
      queryParam: {
        userName: '',
        userMobile: ''
      },
      showModal: false,
      itemData: {}, // 编辑行数据
      // 表头
      columns: [{
        title: '序号',
        dataIndex: 'no',
        width: 60,
        align: 'center'
      },
      {
        title: '创建时间',
        dataIndex: 'createDate',
        width: 120,
        align: 'center'
      },
      {
        title: '骑手名称',
        dataIndex: 'userName',
        width: 100,
        align: 'center'
      },
      {
        title: '骑手电话',
        dataIndex: 'userMobile',
        width: 100,
        align: 'center'
      },
      {
        title: '状态',
        width: 100,
        align: 'center',
        scopedSlots: {
          customRender: 'status'
        }
      },
      {
        title: '操作',
        width: 100,
        align: 'center',
        scopedSlots: {
          customRender: 'action'
        }
      }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getRiderList(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
        })
      },
      optionAlertShow: false
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      // vm.$refs.table.refresh()
    })
  },
  methods: {
    // 刷新列表
    refreshTable () {
      this.$refs.table.refresh(true)
    },
    // 新建
    openModal () {
      this.showModal = true
      this.itemData = {}
    },
    // 重置
    reset () {
      this.queryParam.userName = ''
      this.queryParam.userMobile = '',
      this.refreshTable()
    },
    handleEdit (row) {
      console.log(row, 'row')
      this.showModal = true
      this.itemData = row
    },
    // 修改状态
    changeFlagHandle (text, record) {
      const _data = {
        id: record.id,
        flag: record.loginFlag ? '1' : '0'
      }
      updateEnableStatus(_data).then(res => {
        if (res.status + '' === '200') {
          this.$message.success(res.message)
        } else {
          record.loginFlag = !record.loginFlag
        }
      })
    }
  }
}
</script>
<style scoped>
	.table-page-search-wrapper .ant-form-inline .ant-form-item {
		margin-bottom: 0;
	}

	.action-button {
		line-height: 40px;
		white-space: nowrap;
		padding: 5px 10px;
		background-color: #1890ff;
		border-radius: 4px;
		color: #fff;
		margin-right: 5px;
	}

	.red-text {
		background-color: red;
	}

	.reset-text {
		background-color: #31b50b;
	}
</style>
