<template>
  <div class="subPage">
    <div class="system-table-search">
      <div class="flex-h">
        <div class="flex-item flex-h">
          <el-button size="mini" type="success" @click='addKey'>新增钥匙</el-button>
        </div>
        <ul>
          <li>
            <!-- <span>选择案场：</span> -->
            <el-select v-model="CaseId" placeholder="请选择">
              <el-option
                v-for="item in cases"
                :key="item.CaseId"
                :label="item.CaseName"
                :value="item.CaseId">
              </el-option>
            </el-select>
          </li>
          <li>
            <el-input
              :placeholder="'请输入锁柜编号'"
              v-model="lockNum"
            >
            </el-input>
          </li>
        </ul>
        <el-button
          size="mini"
          type="primary" @click="search">搜索</el-button>
      </div>
      <div class="moreFilter"></div>
    </div>
    <div class="system-table-box">
      <el-table
        :data="currentList"
        stripe
        style="width: 100%">
        <el-table-column
          prop="LockNum"
          label="锁柜编号">
        </el-table-column>
        <el-table-column
          prop="CustomerName"
          label="当前用户">
        </el-table-column>
        <el-table-column
          prop="Status"
          label="状态">
          <template slot-scope="scope">
            {{scope.row.Status===1?'有人':'无人'}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="scope.row.Status===1"
              size="mini"
              type="danger"
              @click="unBindItem(scope.$index, scope.row)">解绑</el-button>
            <el-button v-if="scope.row.Status===0"
              size="mini"
              type="success"
              @click="bindItem(scope.$index, scope.row)">绑定</el-button>
            <el-button v-if="scope.row.Status===0"
              size="mini"
              type="danger"
              @click="deleteItem(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="postData.page"
      :page-size="postData.pagesize"
      layout="prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog title="钥匙绑定" :visible.sync="dialogTableVisible">
      <ul class="cutBindType">
        <!-- <li :class="{'active':cutBindType==0}" @click="showCardList = false; bindPhone = ''; cutBindType = 0">扫码绑定</li> -->
        <li :class="{'active':cutBindType==1}" @click="showCardList = false; bindPhone = ''; cutBindType = 1">手机号绑定</li>
      </ul>
      <ul class="cutBody">
        <!-- <li :hidden="cutBindType==1">
          <div class="flex-h" v-if="!showCardList">
            <div class="flex-item">
              <el-input
                placeholder="请扫描二维码"
                v-model="bindPhone"
                clearable>
              </el-input>
            </div>
            <el-button type="success" size="mini">确定</el-button>
          </div>
        </li> -->
        <li :hidden="cutBindType==0">
          <div class="flex-h" v-if="!showCardList">
            <div class="flex-item">
              <el-input
                placeholder="请输入正确的手机号"
                v-model="bindPhone"
                clearable>
              </el-input>
            </div>
            <el-button type="success" size="mini" @click="sureBindPhone">确定</el-button>
          </div>
        </li>
      </ul>
      <el-table :hidden="!showCardList" :data="gridData">
        <el-table-column property="date" label="日期" width="150"></el-table-column>
        <el-table-column property="name" label="姓名" width="200"></el-table-column>
        <el-table-column property="address" label="地址"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { createNamespacedHelpers, mapState } from 'vuex'
const { mapActions: mapDeviceActions } = createNamespacedHelpers('device')

export default {
  name: '',
  data () {
    return {
      currentItem: {},
      gridData: [],
      showCardList: false, // 显隐卡列表
      bindPhone: '', // 绑定手机号
      cutBindType: 1, // 切换绑定方式
      dialogTableVisible: false,
      total: 0,
      postData: { // 表格搜索条件
        caseid: '', // 案场id
        page: 1, // 当前页码
        pagesize: 10, // 请求数据量
      },
      currentList: [],
      lockNum: '',
    }
  },
  mounted () {
    this.$nextTick(function () {
      this.getList()
    })
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default
    }),
    CaseId: {
      get () {
        return this.postData.caseid || this.defaultCaseId
      },
      set (val) {
        this.postData.caseid = val
      }
    }
  },
  methods: {
    ...mapDeviceActions([
      'bindKeyByPhone',
    ]),
    addKey () {
      this.$router.push({ name: 'addKey' })
    },
    checkPhone (val) {
      return !!(/^1[34578]\d{9}$/.test(val))
    },
    sureBindPhone () { // 确认绑定手机号
      if (this.bindPhone !== '' && this.checkPhone(this.bindPhone)) {
        this.bindKeyByPhone({
          phone: this.bindPhone,
          keyid: this.currentItem.KeyId
        }).then((res) => {
          this.$message({
            message: '操作成功',
            type: 'success'
          })
          this.dialogTableVisible = false
          this.bindPhone = ''
          this.getList()
        })
      } else {
        this.$message({
          type: 'error',
          message: '请输入正确手机号'
        })
      }
    },
    search () { // 搜索
      this.postData.page = 1
      this.currentList = []
      this.getList()
    },
    getList () { // 获取列表
      // if (this.postData.caseid === '') {
      //   this.postData.caseid = this.cases[0].CaseId
      // }
      this.$ajax(this.$api.caseManager.getKeyList.url, {
        method: this.$api.caseManager.getKeyList.method,
        queryData: { ...this.postData, caseid: this.CaseId, num: this.lockNum }
      }).then(res => {
        this.currentList = res.list
        this.postData.page = res.page
        this.total = res.pagenum
      })
    },
    handleCurrentChange (val) { // 跳转到分页
      this.getList()
    },
    bindItem (index, row) { // 绑定
      this.currentItem = row
      this.dialogTableVisible = true
    },
    unBindItem (index, row) { // 解绑
      this.$confirm('确认解绑此锁柜?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$ajax(this.$api.caseManager.cancelKey.url, {
          method: this.$api.caseManager.cancelKey.method,
          urlData: { id: row.KeyId }
        }).then(res => {
          this.$message({
            type: 'success',
            message: '解绑成功!'
          })
          this.bindPhone = ''
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消解绑'
        })
      })
    },
    deleteItem (index, row) {
      this.$confirm('确认删除此锁柜?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$ajax(this.$api.caseManager.deleteKey.url, {
          method: this.$api.caseManager.deleteKey.method,
          urlData: { id: row.KeyId }
        }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      })
    },
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
@import "page.scss";
</style>
