<template>
  <app-container>
    <div class="garbage-blocks">
      <div class="garbage-block-div">
        <div class="block-title">当前库存</div>
        <div class="block-value">{{currentBags}}<span>个</span></div>
      </div>
      <div class="garbage-block-div block-button" @click="inbound">
        <div class="block-middle">入库</div>
      </div>
      <div class="garbage-block-div block-button" @click="outbound">
        <div class="block-middle">出库</div>
      </div>
    </div>
    <search-area :need-clear="true" :need-search-more="false" type="seperate" search-more-type="default" @search="fetchData" @clear="clearInput">
      <!--一般查询条件-->
      <search-item>
        <el-select v-model="listQuery.type" size="small" placeholder="记录类型" clearable>
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.name"
            :value="item.value"/>
        </el-select>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="listQuery.beginTime"
          type="datetime"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择开始时间"/>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="listQuery.endTime"
          type="datetime"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择结束时间"/>
      </search-item>
    </search-area>
    <normal-table :data="list" :head="tableOption.head" :query="listQuery" :total="total" :columns="columns" :list-loading="listLoading" :options="tableOption.options" :tools-option="tableOption.toolsOption" @change="changePage"/>
    <inbound ref="inbound" @watchChild="fetchData"/>
    <outbound ref="outbound" @watchChild="fetchData"/>
  </app-container>
</template>

<script>
import { getBoundList } from '@/api/biz/garbageBag'
import Inbound from './inbound'
import Outbound from './outbound'

export default {
  name: 'GarbageBagList',
  components: { Outbound, Inbound },
  data() {
    return {
      currentBags: 2321,
      listQuery: {
        type: '', // 记录类型
        beginTime: '', // 开始时间
        endTime: '', // 结束时间
        offset: 1,
        limit: 20,
        sort: 'ts',
        order: 'desc'
      }, // 筛选条件
      columns: [
        {
          text: '记录类型',
          value: 'typeName',
          align: 'center'
        },
        {
          text: '记录时间',
          value: 'ts',
          align: 'center'
        },
        {
          text: '数量（个）',
          value: 'num',
          align: 'center'
        },
        {
          text: '库存（个）',
          value: 'remain',
          align: 'center'
        },
        {
          text: '使用时间',
          value: 'useTime',
          align: 'center'
        },
        {
          text: '使用类型',
          value: 'useType',
          align: 'center'
        },
        {
          text: '使用区域',
          value: 'useArea',
          align: 'center'
        },
        {
          text: '回收情况',
          value: 'recyclable',
          align: 'center'
        }
      ], // 显示列
      timeRange: [], // 时间范围
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 列表加载动画
      typeList: [
        { name: '入库', value: '1' },
        { name: '出库', value: '2' }
      ],
      fileList: [],
      tableOption: {
        head: {
          show: true, // 是否需要标题栏，
          text: '入库/出库记录' // 标题名称
        },
        options: {
          needIndex: true // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      }, // 表格属性
      editShow: false // 编辑页面是否显示
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const that = this
      this.list = [
        { type: '1', num: 200, remain: 2000, useTime: '', useType: '', useArea: '', recyclable: '可回收', ts: '2021-05-07 12:00:00' },
        { type: '2', num: 200, remain: 2000, useTime: '2021-05-07', useType: '日常', useArea: '活力大道', recyclable: '可回收', ts: '2021-05-07 12:00:00' },
        { type: '1', num: 200, remain: 2000, useTime: '', useType: '', useArea: '', recyclable: '可回收', ts: '2021-05-07 12:00:00' },
        { type: '2', num: 200, remain: 2000, useTime: '2021-05-07', useType: '日常', useArea: '活力大道', recyclable: '可回收', ts: '2021-05-07 12:00:00' },
        { type: '1', num: 200, remain: 2000, useTime: '', useType: '', useArea: '', recyclable: '可回收', ts: '2021-05-07 12:00:00' },
        { type: '1', num: 200, remain: 2000, useTime: '', useType: '', useArea: '', recyclable: '可回收', ts: '2021-05-07 12:00:00' },
        { type: '1', num: 200, remain: 2000, useTime: '', useType: '', useArea: '', recyclable: '可回收', ts: '2021-05-07 12:00:00' }
      ]
      this.currentBags = this.list[0].remain
      this.list = this.list.map(item => {
        return { ...item, typeName: item.type === '1' ? '入库' : '出库' }
      })
      this.total = this.list.length
      that.listLoading = false
      // getBoundList(this.listQuery).then(response => {
      //   that.list = response.data.rows
      //   that.total = response.data.total
      //   that.listLoading = false
      // })
    },
    // 点击编辑
    inbound() {
      this.$refs.inbound.initDialog()
    },
    // 点击详情
    outbound() {
      this.$refs.outbound.initDialog()
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 重置后的操作， 若不需要显示重置按钮则不需要写
    clearInput() {
      this.listQuery = {
        keywords: '', // 关键字
        type: '', // 垃圾桶类型
        isClassification: '0',
        position: '', // 位置
        offset: 1,
        limit: 20,
        sort: '',
        order: 'desc'
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .edit_btns{
    .edit_btn{
      float:right;
      margin-left:5px;
    }
  }
  .garbage-blocks{
    height:80px;
    border-bottom: 12px solid #ebebeb;
    padding:5px;
    display: flex;
    justify-content: start;
    .garbage-block-div{
      width:200px;
      font-size: 18px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: #66b1ff;
      height: 100%;
      color: white;
      padding:0px 12px;
      margin-right:12px;
      .block-title{

      }
      .block-value{
        font-size:24px;
        span{
          font-size:16px;
        }
      }
      .block-middle{
        flex:1;
        text-align: center;
      }
    }
    .block-button:hover{
      background-color: #86cfff;
      cursor: pointer
    }
  }
</style>
