<template>
  <div class="subPage">
    <div class="system-table-search">
      <div class="flex-h" style="align-items: flex-start;">
        <div class="flex-item flex-h"></div>
        <ul style="white-space: normal;">
          <li style="margin-bottom: 15px;">
            <el-input placeholder="请输入条形码" v-model="postData.barcode"></el-input>
          </li>
          <li style="margin-bottom: 15px;">
            <el-input placeholder="请输入图书名" v-model="postData.bookname"></el-input>
          </li>
          <li style="margin-bottom: 15px;">
            <el-input placeholder="请输入用户名" v-model="postData.customername"></el-input>
          </li>
          <li style="margin-bottom: 15px;">
            <el-input placeholder="请输入手机号" v-model="postData.customerphone"></el-input>
          </li>
          <li style="margin-bottom: 15px;">
            <el-select v-model="CaseId" placeholder="请选择案场">
              <el-option key="all" label="所有案场" value="all"></el-option>
              <el-option
                v-for="item in cases"
                :key="item.CaseId"
                :label="item.CaseName"
                :value="item.CaseId"
              ></el-option>
            </el-select>
          </li>
        </ul>
        <el-button type="primary" @click="search">搜索</el-button>
      </div>
      <div class="moreFilter"></div>
    </div>
    <div class="system-table-box">
      <el-table :data="list" stripe style="width: 100%">
        <el-table-column label="案场">
          <template slot-scope="scope">{{returnCaseName(scope.row.CaseId)}}</template>
        </el-table-column>
        <el-table-column label="图片" width="300">
          <template slot-scope="scope">
            <img :src="scope.row.BookImg" style="height: 150px;">
          </template>
        </el-table-column>
        <el-table-column prop="BookBarcode" label="条形码"></el-table-column>
        <el-table-column prop="BookName" label="图书名"></el-table-column>
        <el-table-column prop="CustomerName" label="用户名"></el-table-column>
        <el-table-column prop="CustomerPhone" label="手机号"></el-table-column>
        <el-table-column label="预约时间">
          <template slot-scope="scope">{{toolClass.dateFormat(scope.row.ReserveDate)}}</template>
        </el-table-column>
        <el-table-column label="保留时间">
          <template slot-scope="scope">{{toolClass.dateFormat(scope.row.ReserveEndDate)}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="cancel(scope.row.BookBorrowRecordId)">取消预约</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="postData.page"
        :page-size="postData.pagesize"
        layout="prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { createNamespacedHelpers, mapState } from 'vuex'
const { mapState: mapLibSate, mapActions: mapLibActions } = createNamespacedHelpers('library')
export default {
  name: 'borrowList',
  data () {
    return {
      total: 1,
      postData: {
        barcode: '',
        bookname: '',
        customername: '',
        customerphone: '',
        borrowstatus: '4',
        caseid: '',
        page: 1,
        pagesize: 10
      },
      statusList: [{
        id: '',
        value: ''
      }]
    }
  },
  created () {
    this.getBorrowList({ ...this.postData }).then((res) => {
      this.page = res.page
      this.total = res.pagenum
    })
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCaseId: x => x.app.cases.default,
      orgid: x => x.app.user.OrgId
    }),
    ...mapLibSate({
      list: x => x.borrow.list
    }),
    CaseId: {
      get () {
        return this.postData.caseid || this.defaultCaseId
      },
      set (val) {
        this.postData.caseid = val
      }
    }
  },
  methods: {
    ...mapLibActions(['getBorrowList', 'cancelBorrow']),
    cancel (id) {
      this.$confirm('确认取消预约?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.cancelBorrow(id).then(() => {
          this.getBorrowList({ ...this.postData }).then((res) => {
            this.page = res.page
            this.total = res.pagenum
          })
        })
      })
    },
    search () {
      this.page = 1
      this.getBorrowList({ ...this.postData }).then((res) => {
        this.page = res.page
        this.total = res.pagenum
      })
    },
    handleCurrentChange (val) {
      this.postData.page = val
      this.getBorrowList({ ...this.postData }).then((res) => {
        this.page = res.page
        this.total = res.pagenum
      })
    },
    returnCaseName (id) {
      var name = ''
      this.cases.map((item) => {
        if (item.CaseId === id) name = item.CaseName
      })
      return name
    }
  }
}
</script>

<style lang="scss">
</style>
