<!-- 今日消耗 -->
<template>
  <div class="container">
    <div class="bar">
      <div class="item">
        <div class="left">{{ titleData.directTotal.technologyType }}</div>
        <div class="center">
          <p>总金额（元）</p>
          <p class="number">{{ titleData.directTotal.totalAmount }}</p>
        </div>
        <div class="right">
          <p>今日消耗（元）</p>
          <p class="number red">{{ titleData.directTotal.consumeAmount }}</p>
        </div>
      </div>
      <div class="item">
        <div class="left">{{ titleData.paperTotal.technologyType }}</div>
        <div class="center">
          <p>总金额（元）</p>
          <p class="number">{{ titleData.paperTotal.totalAmount }}</p>
        </div>
        <div class="right">
          <p>今日消耗（元）</p>
          <p class="number red">{{ titleData.paperTotal.consumeAmount }}</p>
        </div>
      </div>
      <div class="item">
        <div class="left">{{ titleData.heatTotal.technologyType }}</div>
        <div class="center">
          <p>总金额（元）</p>
          <p class="number">{{ titleData.heatTotal.totalAmount }}</p>
        </div>
        <div class="right">
          <p>今日消耗（元）</p>
          <p class="number red">{{ titleData.heatTotal.consumeAmount }}</p>
        </div>
      </div>
      <div class="item more" @click="$router.push('use_detail')">查看详情>></div>
    </div>
    <div class="table-filter">
      <div class="filter-item">
        <Input v-model="key" placeholder="请输入关键字" />
      </div>
      <div class="filter-item" style="width: 200px">
        <my-tree-select v-model="materialCategoryCode" :treeData="materialTreeData"></my-tree-select>
      </div>
      <div class="filter-item">
        <Button type="primary" style="width: 90px;" @click="search">查询</Button>
      </div>
      <div class="filter-item">
        <Button type="primary" style="width: 90px;" @click="exportExcel">导出Excel</Button>
      </div>
      <div class="filter-item" style="float: right;">
        滞留：在库时间超过90天
      </div>
    </div>
    <!-- 表格 -->
    <div class="table-content">
      <div v-if="data.length === 0" style="height: 100%;display: flex;justify-content: center;align-items: center">
        暂无数据
      </div>
      <Collapse :value="item.storageDetails ? '1' : '0'" v-for="(item, index) in data" :key="index" @on-change="open(item)">
        <Panel name="1">
          <div class="table-item">
            <div class="row">
              <div class="col">物料类型：{{ item.categoryName }}</div>
              <div class="col">物料编码：{{ item.materialCode }}</div>
              <div class="col">物料名称：{{ item.materialName }}</div>
              <div class="col"></div>
              <div class="col" style="flex: .2"></div>
              <div class="col blue" style="padding-right: 15px;display: flex;justify-content: space-between;flex: none;width: 140px;">
<!--                <span @click="toHistory(0)">入库记录</span>-->
<!--                <span @click="toHistory(1)">出库记录</span>-->
              </div>
            </div>
            <div class="row">
              <div class="col red">安全库存：{{ item.saveQuantity }}</div>
              <div class="col red">库存数量：{{ item.quantity }}</div>
              <div class="col">库存件数：{{ item.tagCount }}</div>
              <div class="col orange">滞留数量：{{ item.retentionCount }}</div>
              <div class="col" style="flex: .2"></div>
              <div class="col" style="flex: none;width: 140px;"></div>
            </div>
          </div>
          <div slot="content" class="content">
            <Table :columns="columns" :data="item.storageDetails" max-height="250"></Table>
          </div>
        </Panel>
      </Collapse>
    </div>
    <!-- 分页 -->
    <div class="table-page">
      <Page :current="params.pageIndex" :total="params.total" :show-total="true" :page-size="20" show-elevator @on-change="changePage" />
    </div>
  </div>
</template>

<script>
import { exportExcel } from '@/libs/util'
import { GetStockPageList, GetStockTagDetailList } from '@/api/stock'
import MyTable from '_c/my-table/my-table'
import MyTreeSelect from '_c/my-tree-select/my-tree-select'
import { GetMaterialCategoryZTree } from '@/api/baseData'
import { GetTodayConsumeTotal } from '@/api/storageHome'
export default {
  name: 'use_today',
  components: {
    MyTable,
    MyTreeSelect
  },
  data () {
    return {
      materialTreeData: [
        { id: 1, title: 'aaaa', value: '2222' },
        { id: 2, title: 'bbbbb', value: '3333' },
        { id: 3,
          title: 'Ccc',
          value: '4444',
          children: [
            { id: 5, title: 'ddd', value: '2222' }
          ] }
      ],
      materialCategoryCode: '',
      key: '',
      filterData: {},
      formData: {},
      show: false,
      titleData: {
        'directTotal': {
          'technologyType': '直喷',
          'totalAmount': 0,
          'consumeAmount': 0
        },
        'heatTotal': {
          'technologyType': '热转',
          'totalAmount': 0,
          'consumeAmount': 0
        },
        'paperTotal': {
          'technologyType': '打纸',
          'totalAmount': 0,
          'consumeAmount': 0
        }
      },
      data: [],
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '物料标签值',
          key: 'rfid',
          tooltip: true,
          minWidth: 150
        },
        {
          title: '物料数量',
          key: 'quantity',
          minWidth: 150
        },
        {
          title: '入库时间',
          key: 'addStorageTime',
          minWidth: 150
        },
        {
          title: '在库时间',
          key: 'inStorageDay',
          minWidth: 150
        },
        {
          title: '质检状态',
          key: 'quantityState',
          minWidth: 150,
          render: (h, params) => {
            return h('span', params.row.quantityState === 0 ? '未质检' : params.row.quantityState === 1 ? '已质检' : params.row.quantityState === 2 ? '无需质检' : '')
          }
        }
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0
      }
    }
  },
  methods: {
    open (item) {
      if (!item.storageDetails) {
        GetStockTagDetailList({
          materialCode: item.materialCode
        }).then(res => {
          if (res.code === 0) {
            this.$set(item, 'storageDetails', res.result)
          }
        })
      }
    },
    // 跳转对应记录列表 0入库记录 1出库记录
    toHistory (type) {
      if (type === 0) {
        this.$router.push({
          name: 'store_in_record'
        })
      } else if (type === 1) {
        this.$router.push({
          name: 'store_out_record'
        })
      }
    },
    // 获取控件宽度
    getComWidth (item) {
      if (item.name === 'Button') {
        return '80px'
      } else if (item.name === 'Input') {
        return '200px'
      }
    },
    cfm (type, formData) {
      if (type === 'add') {
        FacPriceAdd({
          ...formData,
          materialCode: formData.code
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      } else if (type === 'edit') {
        FacPriceChange({
          ...formData,
          factoryPriceId: formData.id
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      }
    },
    exportExcel () {
      if (this.data.length < 1) return this.$Message.error('数据为空！')
      let arr = []
      GetStockPageList({
        ...this.params,
        pageIndex: 1,
        pageSize: 999999
      }).then(res => {
        if (res.code === 0) {
          let data = res.result.list
          data.forEach(item => {
            if (item.storageDetails) {
              arr = [...arr, ...item.storageDetails]
            }
          })
          exportExcel(this.columns, arr, '库存管理')
        }
      })
    },
    // 检索条件事件处理
    eventHandle (option) {
      switch (option._evnet) {
        case 'search':
          this.params.pageIndex = 1
          this.params.key = option.key
          this.params.purContractState = option.purContractState
          this.getList()
          break
        case 'add':
          this.formType = 'add'
          this.show = true
          break
        case 'back':
          this.$router.go(-1)
          break
      }
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    search () {
      this.params.key = this.key
      this.params.materialCategoryCode = this.materialCategoryCode
      this.getList()
    },
    getList () {
      GetStockPageList(this.params).then(res => {
        if (res.code === 0) {
          this.data = res.result.list
          this.params.total = res.result.totalCount
        }
      })
    },
    handleData (data) {
      data.forEach(item => {
        item.title = item.name
        // item.expand = true
        if (item.children) {
          this.handleData(item.children)
        }
      })
    },
    getTree () {
      GetMaterialCategoryZTree().then(res => {
        if (res.code === 0) {
          this.materialTreeData = res.result
          this.handleData(this.materialTreeData)
        }
      })
    },
    getTitleData () {
      GetTodayConsumeTotal().then(res => {
        if (res.code === 0) {
          this.titleData = res.result
        }
      })
    }
  },
  mounted () {
    this.getList()
    this.getTree()
    this.getTitleData()
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  .bar {
    height: 88px;
    display: flex;
    justify-content: space-between;
    .item {
      flex: 1;
      margin-right: 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 70px;
      border-radius: 5px;
      background-color: #FFFFFF;
      border: 1px solid #e6e6e6;
      .left, .center, .right {
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        text-align: center;
        border-right: 1px solid #e6e6e6;
        .number {
          margin-top: 5px;
          font-size: 16px;
          font-weight: bold;
          color: #3F92F9;
          &.red {
            color: #DB0020;
          }
        }
      }
      .left {
        flex: 3;
        font-size: 16px;
        font-weight: bold;
      }
      .center {
        flex: 5;
      }
      .right {
        flex: 4;
        border-right: none;
      }
      &.more {
        flex: none;
        margin-right: 0;
        padding: 0 20px;
        justify-content: center;
        width: 140px;
        background-color: #333333;
        color: #FFFFFF;
        cursor: pointer;
      }
    }
  }
  .table-filter {
    padding-bottom: 10px;
    height: 42px;
    line-height: 32px;
    overflow: hidden;
    .filter-item {
      margin-right: 10px;
      float: left;
      //&:last-child {
      //  margin-right: 0;
      //}
    }
  }
  .table-content {
    overflow: auto;
    height: ~"calc(100% - 173px)";
    .table-item {
      width: 100%;
      .row {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .col {
          padding: 0 5px;
          flex: 1;
          display: flex;
          align-items: center;
          height: 30px;
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          &.red {
            color: red;
          }
          &.orange {
            color: orange;
          }
          &.blue {
            color: #169bd5;
          }
        }
      }
    }
    .content {
      padding-left: 30px;
    }
  }
  .table-page {
    padding-top: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 43px;
    overflow: hidden;
  }
}
/deep/ .ivu-collapse-header {
  min-width: 750px;
  display: flex;
  height: 60px !important;
  background-color: #FFFFFF;
  border-bottom: none !important;
}
/deep/ .ivu-icon-ios-arrow-forward {
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
}
/deep/ .ivu-collapse-content-box {
  padding: 0;
  padding-bottom: 10px;
}
/deep/ .ivu-collapse {
  margin-bottom: 5px;
}
</style>
