<template>
  <div class="ele-body">
    <el-row :gutter="15">
      <el-col :span="16" style="height: calc(57% - 51px)">
        <el-radio-group v-model="form.timeType" @change="timeTypeChange">
          <el-radio-button label="1">企业</el-radio-button>
          <el-radio-button label="2">工厂</el-radio-button>
          <el-radio-button label="3">班组</el-radio-button>
          <el-radio-button label="4">产线</el-radio-button>
        </el-radio-group>
        <div style="height: 20%" class="item">
          <div v-for="item in arr" :key="item" :style="{ color: item.color }">
            <img :src="item.imgUrl" />
            <div>
              <span>
                {{ item.name }}
              </span>
              <span>
                {{ item.num }}
                <span>{{ item.unit }}</span>
              </span>
            </div>
          </div>
        </div>
        <el-card class="box-card" style="height: calc(80% - 15px)">
          <div slot="header" class="clearfix">
            <span>工单完成统计</span>
          </div>
          <v-chart ref="barRef" style="height: 100%" :option="barOption" />
        </el-card>
      </el-col>
      <el-col :span="8" style="height: 57%">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>不良品分布图</span>
          </div>
          <v-chart ref="pieRef" style="height: 100%" :option="pieOptions" />
        </el-card>
      </el-col>
      <el-col :span="24" style="height: 43%; margin-top: 15px">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>工单进度执行跟踪</span>
          </div>
          <ele-pro-table
            ref="table"
            height="calc(100%)"
            :columns="columns"
            :datasource="datasource"
            :toolbar="false"
            :needPage="false"
          >
          </ele-pro-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
  import dictMixins from '@/mixins/dictMixins';
  import { pieOption, barOption, columns, pieOptions } from './data';
  import { use } from 'echarts/core';
  import { CanvasRenderer } from 'echarts/renderers';
  import { BarChart, PieChart, LineChart } from 'echarts/charts';
  import {
    GridComponent,
    TooltipComponent,
    LegendComponent
  } from 'echarts/components';

  import VChart from 'vue-echarts';
  import { echartsMixin } from '@/utils/echarts-mixin';
  import { getCount, getCompletionCount, getOrderList,queryProportionDefectiveTypes } from '@/api/mes/index';
  // 按需加载 echarts
  use([
    CanvasRenderer,
    BarChart,
    PieChart,
    GridComponent,
    TooltipComponent,
    LegendComponent,
    LineChart
  ]);
  export default {
    mixins: [dictMixins, echartsMixin(['pieRef', 'barRef'])],
    components: { VChart },
    data() {
      return {
        columns,
        arr: [
          {
            name: '生产总数',
            num: '188',
            imgUrl: require('../../assets/Group1.png'),
            unit: ''
          },
          {
            name: '待生产数量',
            num: '188',
            imgUrl: require('../../assets/Group2.png'),
            unit: ''
          },
          {
            name: '已完成数量',
            num: '188',
            imgUrl: require('../../assets/Group3.png'),
            unit: ''
          },
          {
            name: '生产中数量',
            num: '188',
            imgUrl: require('../../assets/Group4.png'),
            unit: '',
            color: '#f97876'
          }
        ],

        pieOption: {},
        barOption: {},
        pieOptions: {},
        form: {
          finishTime: '',
          timeType: '1'
        }
      };
    },

    created() {
      // this.pieOption = pieOption([
      //   { value: 40, name: '  外观检测' },
      //   { value: 30, name: '  尺寸检测' },
      //   { value: 15, name: '  性能检测' },
      //   { value: 10, name: '  密度检测' },
      //   { value: 5, name: '  重量检测' }
      // ]);

      this.getCount();
      this.getCompletionCount();
    },
    mounted() {
      this.proportion();
      setTimeout(() => {
        this.$refs.barRef.resize();
        this.$refs.pieRef.resize();
      }, 300);
    },
    methods: {
      async proportion() {
        const res = await queryProportionDefectiveTypes();
        let list = res.map((el) => {
          return {
            value: el.unQualifiedNumber,
            name: el.categoryLevelClassName
          };
        });
        this.pieOptions = pieOptions(list);
        console.log(res, 'res ----');
      },
      datasource({ page, where, limit }) {
        where.factoriesId = 0;
        return getOrderList({
          ...where,
          pageNum: page,
          size: limit
        });
      },
      timeTypeChange(){},
      //首页统计
      async getCount() {
        let rest = await getCount({ factoriesId: 0 });
        this.arr.forEach((item, index) => {
          if (item.name === '生产总数') {
            item.num = rest.formingNum || 0;
          } else if (item.name === '待生产数量') {
            item.num = rest.pendingProductionCount || 0;
          } else if (item.name === '已完成数量') {
            item.num = rest.formedNum || 0;
          } else if (item.name === '生产中数量') {
            item.num = rest.inProgressWorkOrderCount || 0;
          }
        });
        // console.log(rest, 'rest');
      },

      //工单完成统计
      async getCompletionCount() {
        let params = {
          startDate: new Date().getFullYear() + '-01-01',
          endDate: new Date().getFullYear() + '-12-31',
          factoriesId: 0
        };
        let rest = await getCompletionCount(params);
        let inWarehouseCountList = []; //入库数量
        let inProductCountList = []; //在制工单数
        for (let i = 0; i < rest.length; i++) {
          inWarehouseCountList.push(rest[i].inWarehouseCount);
          inProductCountList.push(rest[i].inProductCountList);
        }
        this.barOption = barOption([
          {
            name: '工单入库数量',
            barWidth: '25%',
            data: inWarehouseCountList,
            type: 'bar',
            yAxisIndex: 0 // 使用第一个Y轴
          },
          {
            name: '工单数',
            symbolSize: 10,
            data: inProductCountList,
            type: 'line',
            smooth: true,
            itemStyle: {
              color: '#ffab08'
            }
          }
        ]);
      }
    }
  };
</script>
<style lang="scss" scoped>
  .clearfix {
    font-size: 0.7vw;

    > span {
      font-weight: bold;
    }
    :deep(.el-radio-button__inner) {
      font-size: 0.7vw;
    }
  }
  .ele-body {
    height: calc(100vh - 111px);
    > .el-row {
      height: 100%;
    }
    .el-card {
      height: 100%;
      :deep(.el-card__body) {
        padding: 0.3vw;
      }
      :deep(.el-card__header) {
        padding: 0.8vw;
      }
      :deep(.el-card__body) {
        height: calc(100% - 1.6vw - 39px);
      }
      :deep(.ele-pro-table) {
        height: 99%;
      }
      :deep(.el-table) {
        font-size: 0.65vw;
      }
    }
  }
  .item {
    width: 100%;
    display: flex;
    margin-bottom: 15px;
    margin-top: 15px;
    > div {
      border-radius: 4px;
      flex: 1;
      margin-left: 15px;
      background: url(../../assets/item_frame.png) no-repeat;
      background-size: 100% 100%;
      display: flex;
      align-items: center;
      padding: 12px;
      img {
        width: 2.3vw;
        // height:1.7vw;
      }
      > div {
        display: flex;
        flex-direction: column;
        // margin-left: 0.8vw;
        justify-content: center;
        align-items: center;
        width: 80%;
        span:nth-of-type(1) {
          font-size: 0.75vw;
        }
        span:nth-of-type(2) {
          font-weight: bold;
          font-size: 1.2vw;
          margin-top: 0.8vw;
          span {
            font-size: 0.7vw;
          }
        }
      }
    }
  }
</style>
