<template>
  <div class="subPage">
    <div class="list flex-h">
      <div class="flex-item">
        <div style="text-align:right;margin: 20px 0 0;border:none;">
          <el-button
            size="mini"
            type="warning"
            @click="reback">返回</el-button>
        </div>
        <div v-if="courses !== null && (courses.list||[]).length>0">
          <el-table
              :data="courses.list"
              stripe
              style="width: 100%">
              <el-table-column
                prop="CaseName"
                label="所在案场">
              </el-table-column>
              <el-table-column
                prop="LocationName"
                label="课程分类">
              </el-table-column>
              <el-table-column
                prop="CourseName"
                label="课程名称">
              </el-table-column>
              <el-table-column
                prop="DetailName"
                label="课时名称">
              </el-table-column>
              <el-table-column
                prop="StartDate"
                label="课程开始时间">
                <template slot-scope="scope">
                  <span>{{scope.row.StartDate === '0001-01-01T00:00:00Z' ? '-' : toolClass.dateFormat(scope.row.StartDate)}}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="EndDate"
                label="课程结束时间">
                <template slot-scope="scope">
                  <span>{{scope.row.EndDate === '0001-01-01T00:00:00Z' ? '-' : toolClass.dateFormat(scope.row.EndDate)}}</span>
                </template>
              </el-table-column>
              <el-table-column
                label="操作">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="success"
                    v-if="scope.row.VerifyStatus==='useable'"
                    @click="check(scope.row)">核销</el-button>
                  <el-button
                    size="mini"
                    type="info"
                    v-if="scope.row.VerifyStatus==='late'">逾期核销</el-button>
                  <el-button
                    size="mini"
                    type="info"
                    v-if="scope.row.VerifyStatus==='used'">已完成</el-button>
                  <!-- <el-button
                    size="mini"
                    type="success"
                    v-if="scope.row.VerifyStatus!=='used'"
                    @click="check(scope.row)">核销</el-button>
                  <span v-else>已完成</span> -->
                </template>
              </el-table-column>
            </el-table>
        </div>
        <span class="noData" v-else>查不到当前课程信息，请核对二维码或课程详情</span>
      </div>
    </div>
    <el-dialog
      title="选择绑定钥匙"
      :visible.sync="centerDialogVisible"
      width="400"
      center>
      <div>
        <el-table
          :data="dialogList"
          stripe
          style="width: 100%">
          <el-table-column
            prop="CustomerName"
            label="当前用户">
          </el-table-column>
          <el-table-column
            prop="LockNo"
            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="操作"
            width="200">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.Status === 0"
                size="mini"
                type="success"
                @click="toBindKey(scope.row)">绑定</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @current-change="handleCurrentChangeDialog"
          :current-page.sync="postData.page"
          :page-size="postData.pagesize"
          layout="prev, pager, next, jumper"
          :total="dialogTotal">
        </el-pagination>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="200">
      <span>确认核销此课程</span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sureVerify">确 定</el-button>
        <el-button type="primary" @click="sureAndBind">确定并绑定钥匙</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex'

const { mapState: mapVerifState, mapActions: mapVerifActions } = createNamespacedHelpers('verification')
const { mapActions: mapDeviceActions } = createNamespacedHelpers('device')

export default {
  name: '',
  data () {
    return {
      centerDialogVisible: false,
      code: '',
      dialogVisible: false,
      currentItem: {},
      postData: {
        caseid: '',
        page: 1,
        pagesize: 6,
      },
      dialogTotal: 0,
      dialogList: [],
    }
  },
  mounted () {
    const { code } = this.$route.query
    this.code = code
    this.$nextTick(function () {
      this.getList()
    })
  },
  computed: {
    ...mapVerifState({
      courses: x => x.courseVerifs
    }),
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default,
      OrgId: x => x.app.user.OrgId,
    }),
    CaseId: {
      get () {
        return this.postData.caseid || this.defaultCaseId
      },
      set (val) {
        this.postData.caseid = val
      }
    }
  },
  methods: {
    ...mapVerifActions([
      'GetCourseVerList',
      'CourseVerifs',
    ]),
    ...mapDeviceActions([
      'getKeyList',
      'bindKeyByQrCode',
    ]),
    toBindKey (item) {
      this.bindKeyByQrCode({
        keyid: item.KeyId,
        customerid: this.customerId,
      }).then((res) => {
        // console.log(JSON.stringify(res))
        this.CourseVerifs({ id: this.currentItem.CustomerDetailId, callback: this.afterCheck })
        this.centerDialogVisible = false
        this.postData.page = 1
        this.dialogList = []
        this.getList()
      })
    },
    sureVerify () {
      this.CourseVerifs({ id: this.currentItem.CustomerDetailId, callback: this.afterCheck })
    },
    sureAndBind () {
      this.dialogVisible = false
      this.getKeyList({
        ...this.postData,
        caseid: this.currentItem.CaseId,
      }).then((res) => {
        // console.log(JSON.stringify(res))
        this.dialogTotal = res.pagenum
        this.dialogList = res.list
        this.centerDialogVisible = true
      })
    },
    check (item) { // 核销
      this.currentItem = item
      this.dialogVisible = true
    },
    getList () {
      this.GetCourseVerList({ id: this.code }).then((res) => {
        if (res !== null) {
          this.customerId = res.customerInfo.CustomerId
        }
      })
    },
    afterCheck () {
      this.$message({
        type: 'success',
        message: '操作成功!'
      })
      this.dialogVisible = false
      this.getList()
    },
    reback () { // 返回
      this.$router.push({ name: 'qrcodeVerification' })
    },
    handleCurrentChangeDialog (val) {
      this.postData.page = val
      this.getKeyList({
        ...this.postData,
        caseid: this.CaseId,
      }).then((res) => {
        // console.log(JSON.stringify(res))
        this.dialogTotal = res.pagenum
        this.dialogList = res.list
        this.centerDialogVisible = true
      })
    },
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.subPage {
  .list {
    > div {
      margin: 0 20px;
      > div {
        width: 100%;
        position: relative;
        overflow: hidden;
        box-sizing: border-box;
        border: 1px solid #eee;
        border-bottom: none;
        margin-top: 20px;
      }
    }
  }
  .noData {
    width: 100%;
    display: block;
    line-height: 40px;
    font-size: 30px;
    color: #ccc;
    text-align: center;
    margin: 40px auto 0;
  }
}
</style>
