<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">书籍名称</label>
        <el-input v-model="query.bookName" clearable placeholder="书籍名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">是否逾期</label>
        <template>
          <el-select v-model="query.isOverdue" clearable placeholder="是否逾期" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery">
            <el-option
              v-for="item in selectOptionValue"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </template>
        <label class="el-form-item-label">逾期天数</label>
        <el-input v-model="query.overdueDay" clearable placeholder="逾期天数" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">借阅时间</label>
        <date-range-picker
          v-model="query.readStartTime"
          start-placeholder="readStartTimeStart"
          end-placeholder="readStartTimeStart"
          class="date-item"
        />
        <!--        <date-range-picker-->
        <!--          v-model="query.readEndTime"-->
        <!--          start-placeholder="readEndTimeStart"-->
        <!--          end-placeholder="readEndTimeStart"-->
        <!--          class="date-item"-->
        <!--        />-->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px" />
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="dmUser.userNumber" label="员工工号" />
        <el-table-column prop="borrowUserName" label="借阅人姓名" />
        <el-table-column prop="dmUser.usertype" label="人员种类">
          <!--            人员种类(00-正式人员、01-外协单位、02-租户、03-访客)-->
          <template slot-scope="{ row }">
            {{ row.dmUser.usertype === '00' ? '正式人员': row.usertype === '01'? '外协单位' : row.usertype === '02'? '租户' : '访客' }}
          </template>
        </el-table-column>
        <el-table-column prop="dmUser.dept.name" label="组织机构" />
        <el-table-column prop="dmTsInfo.tsName" label="书籍名称" />
        <el-table-column label="借阅金额">
          <template slot-scope="{ row }">
            {{ row.dmReadingRoomApplayList.map(item => item.overdueAmount || 0).reduce(function(prev, cur) {
              return Number(prev) + Number(cur)
            }, 0) }}
          </template>
        </el-table-column>
        <el-table-column prop="readStartTime" label="借阅起始时间" />
        <el-table-column prop="readEndTime" label="借阅到期时间" />
        <el-table-column prop="isOverdue" label="是否逾期">
          <template slot-scope="{ row }">
            {{ row.isOverdue === 0 ? '否': '是' }}
          </template>
        </el-table-column>
        <el-table-column prop="overdueDay" label="逾期天数" />
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button type="primary" icon="el-icon-share" @click="toDetail(row.readingRoomId)" />
          </template>
        </el-table-column>
        <!--        <el-table-column prop="borrowUserId" label="借阅人id" />-->
        <!--        <el-table-column prop="createBy" label="创建人" />-->
        <!--        <el-table-column prop="borrowUserCard" label="借书证号码" />-->
        <!--        <el-table-column prop="collectionSin" label="图书藏id" />-->
        <!--        <el-table-column prop="borrowStatus" label="0  借出   1 归还" />-->
        <!--        <el-table-column v-if="checkPer(['admin','dmReadingRoom:edit','dmReadingRoom:del'])" label="操作" width="150px" align="center">-->
        <!--          <template slot-scope="scope">-->
        <!--            <udOperation-->
        <!--              :data="scope.row"-->
        <!--              :permission="permission"-->
        <!--            />-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDmReadingRoom from '@/api/dmReadingRoom'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'

import pagination from '@crud/Pagination'
import dateRangePicker from '@/components/DateRangePicker'
const defaultForm = { readingRoomId: null, bookId: null, bookName: null, createTime: null, updateTime: null, updateBy: null, readStartTime: null, readEndTime: null, isOverdue: null, overdueDay: null, returnTime: null, borrowUserId: null, borrowMoney: 0, createBy: null, borrowUserName: null, borrowUserCard: null, collectionSin: null, borrowStatus: null }
const optShow = {
  add: false,
  edit: false,
  del: false,
  download: true,
  reset: true
}
export default {
  name: 'DmReadingRoom',
  components: { pagination, crudOperation, rrOperation, dateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '阅览记录', url: 'api/dmReadingRoom', idField: 'readingRoomId', sort: 'readingRoomId,desc', crudMethod: { ...crudDmReadingRoom }, optShow })
  },
  data() {
    return {
      selectOptionValue: [{ name: '否', value: '0' }, { name: '是', value: '1' }],
      permission: {
        add: ['admin', 'dmReadingRoom:add'],
        edit: ['admin', 'dmReadingRoom:edit'],
        del: ['admin', 'dmReadingRoom:del']
      },
      rules: {
        readingRoomId: [
          { required: true, message: '记录id不能为空', trigger: 'blur' }
        ],
        bookId: [
          { required: true, message: '书籍id不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '书籍创建时间不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '书籍更新时间不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'bookName', display_name: '书籍名称' },
        { key: 'isOverdue', display_name: '是否逾期' },
        { key: 'overdueDay', display_name: '逾期天数' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    toDetail(data) {
      crudDmReadingRoom.get(Number(data))
      console.log(data)
    }
  }
}
</script>

<style scoped>

</style>
