<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属仓库" prop="storeCode">
        <el-select v-model="queryParams.storeCode" placeholder="请选择所属仓库">
          <el-option
            v-for="item in storeList"
            :key="item.storeCode"
            :label="item.storeName"
            :value="item.storeCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          plain-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--        >导出-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>-->
    <!--    </el-row>-->
    <div class="charts-container">
      <div class="chart">
        <el-card>
          <div class="chart-title">物料分类统计</div>
          <div ref="pieChart" class="chart-content"></div>
        </el-card>
      </div>
      <div class="chart">
        <el-card>
          <div class="chart-title">货道占用统计(货道容量:{{this.spaceNumber}} 已占用容量:{{this.occupancyNumber}})</div>
          <div ref="barChart" class="chart-content"></div>
        </el-card>
      </div>
    </div>
    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
<!--      <el-table-column label="主键标识" align="center" prop="OBJ_ID"/>-->
      <el-table-column label="在制品库编号" align="center" prop="MATERIALCODE"/>
      <el-table-column label="在制品库名称" align="center" prop="MATERIALNAME"/>
      <el-table-column label="物料类型" align="center" prop="STORECODE"/>
      <el-table-column label="物料名称" align="center" prop="STORENAME"/>
      <el-table-column label="库存数量" align="center" prop="SPACESTOCK" />
    </el-table>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import { getInventoryStatistics, getSpaceList } from '@/api/base/spaceInfo'
import { findStoreList } from '@/api/base/storeInfo'

export default {
  name: 'reportInfo',
  dicts: ['space_type', 'store_status', 'is_flag'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报表信息表格数据
      reportList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        spaceCode: null,
        spaceName: null,
        spaceCapacity: null,
        spaceStock: null,
        spaceStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 仓库选项
      storeList: [],
      pieChart: null,
      barChart: null,
      pieChartData: [
        { value: 30, name: '物料01' },
        { value: 20, name: '物料02' },
        { value: 15, name: '物料03' },
      ],
      barChartData: {
        xValue: ['货道01', '货道02', '货道03', '货道04', '货道05', '货道06', '货道07'],
        yValueOne: [120, 132, 101, 134, 90, 230, 210],
        yValueTwo: [150, 232, 201, 154, 190, 330, 410]
      },
      spaceNumber: 0,
      occupancyNumber: 0,
    }
  },
  created() {
    if (this.$route.params != null){
      this.queryParams.storeCode = this.$route.params && this.$route.params.storeCode;
    }
    this.getList()
    findStoreList().then(response => {
      this.storeList = response.data
    })
  },
  mounted() {
    this.$nextTick(() => {
      this.barChart = echarts.init(this.$refs.barChart);
      this.pieChart = echarts.init(this.$refs.pieChart);
      this.initPieChart();
      this.initBarChart();
    });
  },
  methods: {
    /** 查询货道信息列表 */
    getList() {
      this.loading = true
      getInventoryStatistics(this.queryParams).then(response => {
        this.reportList = response.data
        this.pieChartData = response.data.map(item => ({
          value: item.SPACESTOCK,
          name: item.STORENAME
        }));
        this.initPieChart();
        this.loading = false
      });
      getSpaceList(this.queryParams).then(response => {
        this.barChartData = {
          xValue: response.data.map(item =>  item.storeName + item.spaceName),
          yValueOne: response.data.map(item => item.spaceStock),
          yValueTwo: response.data.map(item => item.spaceCapacity - item.spaceStock),
        };
        this.spaceNumber = response.data.map(item => item.spaceCapacity).reduce((accumulator, currentValue) => accumulator + currentValue);
        this.occupancyNumber = response.data.map(item => item.spaceStock).reduce((accumulator, currentValue) => accumulator + currentValue);
        console.log(this.spaceNumber)
        this.initBarChart();
      });
    },
    initPieChart() {
      // 在这里添加饼图的数据和配置
      const option = {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            name: '物料库存数：',
            type: 'pie',
            radius: ['30%', '70%'],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: '#fff',
              borderWidth: 2
            },
            label: {
              show: false,
              position: 'center'
            },
            labelLine: {
              show: false
            },
            data: this.pieChartData
          }
        ]
      };
      this.pieChart.setOption(option);
    },
    initBarChart() {
      // 在这里添加柱状图的数据和配置
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {},
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.barChartData.xValue
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '货道库存数',
            type: 'bar',
            stack: 'Ad',
            emphasis: {
              focus: 'series'
            },
            data: this.barChartData.yValueOne
          },
          {
            name: '货道空余容量',
            type: 'bar',
            stack: 'Ad',
            emphasis: {
              focus: 'series'
            },
            data: this.barChartData.yValueTwo,
          },
        ]
      };
      this.barChart.setOption(option);
    },
    // 表单重置
    reset() {
      this.form = {
        objId: null,
        spaceCode: null,
        spaceName: null,
        spaceCapacity: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 返回按钮操作 */
    handleClose() {
      const obj = { path: '/wcs/storeInfo' }
      this.$tab.closeOpenPage(obj)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.objId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },


    /** 导出按钮操作 */
    handleExport() {
      this.download('base/spaceInfo/export', {
        ...this.queryParams
      }, `reportInfo_${new Date().getTime()}.xlsx`)
    }

  }
}
</script>
<style scoped>
.charts-container {
  display: flex;
  justify-content: space-between;
  padding: 20px;
}
.chart {
  flex: 1;
  margin-right: 20px;
}

.chart-content {
  height: 300px;
}
.chart-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}
</style>
