<template>
  <div>
    <lx-header icon="md-apps" style="margin-bottom: 10px; border: 0px; margin-top: 15px" text="借阅记录">
      <div slot="content"></div>
      <slot>
        <div class="selects">

          <div>
            <span style="padding: 0 6px;word-break: keep-all;">
              关键字
            </span>
            <Input v-model="select.keyword" placeholder="请输入关键字" style="width: 180px"></Input>
          </div>

          <Button style="margin-left: 10px" type="primary" @click="select={page:1,keyword:''}">重置
          </Button>
          <Button style="margin-left: 10px" type="primary" @click="doSearch">查询</Button>
        </div>
      </slot>
    </lx-header>


    <xy-table :list="list" :table-item="table" :total="total">
      <template v-slot:btns>
        <el-table-column fixed="right" header-align="center" align="center" label="操作" width="220">
          <template slot-scope="scope">
            <div class="slot-btns">
              <Button type="primary" @click="back(scope.row)" v-if="!scope.row.return_time">归还</Button>
              <Button type="error" @click="deleteitem(scope.row)" style="margin-left: 10px;">删除</Button>
            </div>
          </template>
        </el-table-column>
      </template>
    </xy-table>

  </div>
</template>

<script>
  import {
    list as index,
    save,
    del,
    show
  } from "@/api/bookRecord/recordIndex";
  import {
    getInfo
  } from '../../api/user.js'

  export default {
    data() {
      return {
        select: {
          page: 1,
          page_size: 20,
          keyword: '',
          user_name: ''
        },
        total: 0,
        list: [],
        table: [{
            prop: 'type.name',
            label: '图书类目',
            align: 'center',
            width: 120,
            fixed: 'left',

          }, {
            prop: 'book.name',
            label: '图书名称',
            align: 'left',
            minWidth: 280,
            fixed: 'left',

          }, {
            prop: 'book.author',
            label: '作者',
            align: 'center',
            width: 120,
            fixed: 'left',

          }, {
            prop: 'book_detail.no',
            label: '库存编号',
            align: 'center',
            width: 120,
            fixed: 'left',

          }, {
            prop: 'user.name',
            label: '借阅人',
            align: 'center',
            width: 120,
            fixed: 'left',

          }, {
            prop: 'department.name',
            label: '借阅人部门',
            align: 'center',
            width: 120,

          },
          {
            prop: 'created_at',
            label: '借阅时间',
            width: 180,
            formatter: (v1, v2, value) => {
              return value;
            }
          },
          {
            prop: 'return_time',
            label: '归还时间',
            width: 180,
            formatter: (v1, v2, value) => {
              return value;
            }
          }
        ],
      }
    },
    methods: {

      doSearch() {
        this.select.page = 1;
        this.load();
      },
      pageSizeChange(e) {
        this.select.page_size = e;
        this.select.page = 1;
        this.load();
      },
      async load() {

        var that = this;
        let type = parseInt(this.$route.path.split("_")[1]);
        if (type == 1) {
         await getInfo().then(res => {
            this.select.user_name = res.name;
            const resData =  index(this.select)
            this.total = resData.total
            this.list = resData.data
          })
        } else {
          const resData = await index(this.select)
          this.total = resData.total
          this.list = resData.data

        }

      },
      deleteitem(row) {
        del({
          id: row.id
        }).then(res => {
          this.load();
          this.$Message.success("操作成功");
        })
      },
      back(row) {
        save({
          id: row.id,
          status: 1,
          user_id: row.user?.id,
          return_time: this.$moment().format("YYYY-MM-DD HH:mm:ss")
        }).then(res => {
          this.load();
          this.$Message.success("归还成功");
        })
      },
      pageChange(e) {
        this.select.page = e
        this.load()
      },
    },
    mounted() {
      this.load()
    },
    created() {}
  }
</script>

<style lang="scss" scoped>
  .selects {
    display: flex;
    flex-wrap: wrap;

    &>div {
      margin-bottom: 6px;
    }
  }
</style>
