<template>
  <div class="subPage edit-book" :style="{ padding: '20px' }">
    <div v-if="!active">
      <el-form style="text-align: center;">
        <el-form-item>
          <el-input v-model="customerInfo" placeholder="会员手机号或条形码" :style="{ width: '400px' }"></el-input>
        </el-form-item>
        <el-form-item :style="{ paddingTop: '20px' }">
          <el-button type="primary" @click="nextStep()">下一步</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div v-else>

      <!-- 用户信息区 -->
      <div :style="{ marginBottom: '20px', paddingBottom: '20px', borderBottom: '1px solid #eee' }">
        <el-row :gutter="20">
          <el-col :span="4">
            <span>姓名: </span><span>{{ borrowHistory.Name }}</span>
          </el-col>
          <el-col :span="4">
            <span>手机号: </span><span>{{ borrowHistory.Phone }}</span>
          </el-col>
          <el-col :span="4">
            <span>在借数量: </span><span>{{ borrowHistory.BorrowNum }}</span>
          </el-col>
          <el-col :span="4">
            <span>逾期数量: </span><span>{{ borrowHistory.LateNum }}</span>
          </el-col>
          <el-col :span="4">
            <span>历史是否有逾期: </span><span>{{ borrowHistory.IsLate === 'yes' ? '是' : '否' }}</span>
          </el-col>
        </el-row>
      </div>

      <!-- 过滤条件 -->
      <el-form inline>
        <el-form-item label="案场">
          <el-select v-model="formData.caseid" size="medium" placeholder="请选择案场">
            <el-option
              v-for="item in cases"
              :key="item.CaseId"
              :label="item.CaseName"
              :value="item.CaseId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="条形码">
          <el-input ref="barcode" v-model="formData.barcode" :style="{ width: '200px' }"></el-input>
        </el-form-item>
        <el-form-item label="书名">
          <el-input v-model="formData.bookname" :style="{ width: '200px' }"></el-input>
        </el-form-item>
        <el-form-item :style="{ float: 'right' }">
          <el-button type="primary" @click="search()">搜索</el-button>
        </el-form-item>
      </el-form>

      <!-- 可借阅列表 -->
      <el-table :data="list" border style="width: 100%" @selection-change="x => selectList = x">
        <el-table-column
          type="selection"
          width="55">
        </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="BookTypeName" label="分类"></el-table-column>
        <el-table-column prop="LeftNum" label="可借数量"></el-table-column>
        <el-table-column prop="BorrowDays" label="规定借阅天数"></el-table-column>
      </el-table>

      <div :style="{ paddingTop: '40px' }">
        <el-button type="primary" @click="submit()">立即借阅</el-button>
        <el-button @click="$router.back()">取消</el-button>
      </div>
    </div>
  </div>
</template>

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

const {
  mapState: mapLibSate,
  mapActions,
} = createNamespacedHelpers('library')

export default {
  name: 'bookBorrow',
  data() {
    return {
      active: 0,
      customerInfo: '',
      formData: { caseid: '', barcode: '', bookname: '' },
      selectList: [],
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCase: x => x.app.cases.default,
      orgid: x => x.app.user.OrgId
    }),
    ...mapLibSate({
      list: s => s.borrow.list,
      borrowHistory: s => s.borrow.history,
    }),
  },
  watch: {
    defaultCase(nv) {
      if (!this.formData.caseid) {
        this.formData.caseid = nv
      }
    }
  },
  methods: {
    ...mapActions({
      getBorrowHistory: 'getBorrowHistory',
      getList: 'getBookList',
      borrowBooks: 'borrowBooks'
    }),

    nextStep() {
      if (!this.customerInfo) {
        this.$message({
          showClose: true,
          message: '请录入会员手机号或条形码',
          type: 'error'
        })
        return
      }

      this.getBorrowHistory({ customerInfo: this.customerInfo }).then(() => {
        this.active = 1
      }).catch(err => {
        this.$message({
          showClose: true,
          message: err,
          type: 'error'
        })
      })
    },

    search() {
      if (!this.formData.caseid) {
        this.$message({
          showClose: true,
          message: '请选择案场',
          type: 'error'
        })

        return
      }

      this.getList({
        ...this.formData,
        // customerphone: this.borrowHistory.Phone,
      }).catch(err => {
        this.$message({
          showClose: true,
          message: err,
          type: 'error'
        })
      })
    },

    submit() {
      if (!this.selectList || this.selectList.length === 0) {
        this.$message({
          showClose: true,
          message: '请选择借阅图书',
          type: 'error'
        })

        return
      }

      this.$confirm('确定借阅此书?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const bookIds = this.selectList.map(({BookId}) => BookId).join(',')
          this.borrowBooks({
            bookIds,
            customerId: this.borrowHistory.CustomerId,
          }).then(() => {
            this.$message({
              showClose: true,
              message: '借阅成功',
              type: 'success'
            })
          }).catch(err => {
            this.$message({
              showClose: true,
              message: err,
              type: 'error'
            })
          })
        })
        .catch(x => x)
    },
  }
}
</script>

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