<template>
  <div style="padding: 10px; box-sizing: border-box">
    <div>
      <div class="mumber-box">
        <div class="mumber" v-for="(it, index) in data.Statistics" :key="index">
          <div class="mum-left">
            <!-- <img class="index-img" :src="'/src/assets/erp/index-icon' + (index + 1) + '.svg'" /> -->
            <a-image class="index-img" :src="it.icon" :width="70" :preview="false" />
          </div>
          <div class="mum-right">
            <div class="mum-title">{{ it.title }}</div>
            <div class="mum" :style="{ color: it.color }">{{ it.value }}</div>
            <div class="mum-than">
              {{ it.action }}
              <Icon
                :icon="it.isUp ? 'ant-design:caret-up-outlined' : 'ant-design:caret-down-outlined'"
                :color="it.isUp ? '#3dba84' : '#ee3b3b'"
                :size="14"
                class="icon"
              />
            </div>
          </div>
        </div>
      </div>
      <a-row :gutter="10">
        <a-col :span="7">
          <div class="box-bg">
            <div class="box-bg-title">
              <span class="title-text">各生产部门本月产量</span>
            </div>
            <div class="item" ref="homePieChart"></div>
          </div>
        </a-col>
        <a-col :span="10">
          <div class="box-bg">
            <div class="box-bg-title">
              <span class="title-text">产量分析</span>
              <div class="title-chose">
                <span
                  v-for="item in data.priceArray"
                  :key="item.key"
                  :class="item.key == data.cur ? 'cur' : ''"
                  @click="changePrice(item, 1)"
                  >{{ item.name }}</span
                >
              </div>
            </div>
            <div class="item" ref="homeLineChart"></div>
          </div>
        </a-col>
        <a-col :span="7">
          <div class="box-bg">
            <div class="box-bg-title">
              <span class="title-text">本日计划&实际完成情况</span>
            </div>
            <div class="item" ref="homePieChart2"></div>
          </div>
        </a-col>
        <a-col :span="7">
          <div class="box-bg" style="position: relative">
            <div class="box-bg-title">
              <span class="title-text">产线工料废分析</span>
            </div>
            <!-- <div class="total">
              <div>{{ data.total }}万吨</div>
              <div class="text">总出库产品金额</div>
            </div> -->
            <!-- <div class="item" ref="homeBar2Chart"></div> -->
            <div class="item" style="padding-top: 10px">
              <VisitRadar class="md:w-3/3 w-full" :loading="loading" />
            </div>
          </div>
        </a-col>
        <a-col :span="10">
          <div class="box-bg">
            <div class="box-bg-title">
              <span class="title-text">产线平均稼动率</span>
              <div class="title-chose">
                <span
                  v-for="item in data.priceArray2"
                  :key="item.key"
                  :class="item.key == data.cur2 ? 'cur' : ''"
                  @click="changePrice(item, 2)"
                  >{{ item.name }}</span
                >
              </div>
            </div>
            <div class="item" ref="homeBarChart"></div>
          </div>
        </a-col>
        <a-col :span="7">
          <div class="box-bg">
            <div class="box-bg-title">
              <span class="title-text">本日班次完成情况</span>
            </div>
            <div class="item" ref="homePieChart3"></div>
          </div>
        </a-col>
      </a-row>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { onMounted, nextTick, reactive, ref, unref, markRaw } from 'vue';

  import Icon from '/@/components/Icon/index';
  import * as echarts from 'echarts';
  import VisitRadar from './components/VisitRadar.vue';
  const formate = (name) => {
    let str = name + '：';
    data.pieOption?.series?.data?.forEach((o: any) => {
      if (name == o.name) {
        str += o.value + '吨';
      }
    });
    return str;
  };
  const changePrice = (o, i) => {
    if (i == 1) {
      data.cur = o.key;
      // data.myChart4.setOption(unref(barOption), true);
      // data.myChart4.resize(); //显示区域大小发生改变更新图表
    } else {
      data.cur2 = o.key;
      // data.myChart4.setOption(unref(lineOption), true);
      // data.myChart4.resize(); //显示区域大小发生改变更新图表
    }
  };
  const data = reactive({
    priceArray: [
      { key: 0, name: '月度' },
      { key: 1, name: '季度' },
      { key: 2, name: '年度' },
    ],
    priceArray2: [
      { key: 0, name: '月度' },
      { key: 1, name: '季度' },
      { key: 2, name: '年度' },
    ],
    cur: 0,
    cur2: 0,
    colors: ['#5E95FF', '#00CACF', '#FF9100', '#995EFF', '#5E6EFF'],
    Statistics: [
      {
        title: '本月计划总产量（万吨）',
        icon: '/analisis/a1.png',
        value: 2000,
        total: 120000,
        color: '#37C3D0',
        action: '同比 1.14%',
        isUp: true,
      },
      {
        title: '本月实际总产量（万吨）',
        icon: '/analisis/a2.png',
        value: 20000,
        total: 500000,
        color: '#FBA837',
        action: '同比 0.6%',
        isUp: true,
      },
      {
        title: '分页废品量（万吨）',
        icon: '/analisis/a3.png',
        value: 8000,
        total: 120000,
        color: '#5E95FF',
        action: '环比 1.62%',
      },
      {
        title: '本月废品率',
        icon: '/analisis/a4.png',
        value: '12%',
        total: 50000,
        color: '#FC7690',
        action: '同比 1.14%',
      },
      {
        title: '今年总产量（万吨）',
        icon: '/analisis/a5.png',
        value: 5000,
        total: 50000,
        color: '#435FDD',
        action: '环比 10.14%',
      },
    ],
    dataOne: [],
    // 玫瑰饼图
    pieOption: {
      color: ['#FFCF47', '#7A93F7', '#4460DE', '#55CCD8', '#FC7690', '#FE8F67'],
      legend: {
        orient: 'horizontal',
        x: 'center',
        y: 'bottom',
        top: '78%',
        itemWidth: 10,
        itemHeight: 10,
        itemGap: 20,
        padding: [5, 5, 5, 5],
        textStyle: {
          color: '#85878e',
        },
        formatter: formate,
      },
      series: {
        name: '',
        center: ['50%', '32%'],
        type: 'pie',
        radius: [0, 85],
        roseType: 'area',
        itemStyle: {
          borderRadius: 0,
        },
        label: {
          color: '#6e7079',
          overflow: 'none',
          formatter: '{b}\n{d}%',
        },
        data: [
          { value: 500, name: '压铸一部' },
          { value: 310, name: '压铸二部' },
          { value: 274, name: '机加一部' },
          { value: 400, name: '机加二部' },
          { value: 300, name: '机加三部' },
          { value: 200, name: '机加四部' },
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)',
          },
        },
      },
    },

    lineOption: {
      color: ['#5E95FF', '#FF9100'],
      title: {
        text: '单位：万吨',
        left: 20,
        textStyle: {
          color: '#333',
          fontWeight: 'normal',
          fontSize: 12,
        },
      },
      grid: {
        left: 20,
        right: 30,
        bottom: 20,
        containLabel: true,
      },
      legend: {
        itemWidth: 20,
        itemHeight: 10,
        itemGap: 20,
        right: 20,
        textStyle: {
          color: '#85878e',
        },
        data: ['实际产量', '偏差率'],
      },
      xAxis: [
        {
          type: 'category',
          data: [
            '1月',
            '2月',
            '3月',
            '4月',
            '5月',
            '6月',
            '7月',
            '8月',
            '9月',
            '10月',
            '11月',
            '12月',
          ],
          axisPointer: {
            type: 'shadow',
          },
        },
      ],
      yAxis: [
        {
          type: 'value',
          name: '',
          // minInterval: 1,
          min: 0,
          max: 8000,
          splitNumber: 4,
        },
        {
          type: 'value',
          name: '',
          min: 0,
          // max: 10000,
          // interval: 5,
          axisLabel: {
            formatter: '{value} %',
          },
        },
      ],
      series: [
        {
          name: '实际产量',
          type: 'bar',
          barWidth: 20,
          data: [3000, 2000, 3333, 5000, 3200, 4200, 3200, 2100, 3000, 5100, 6000, 3200, 4800],
        },
        {
          name: '偏差率',
          type: 'line',
          yAxisIndex: 1,
          data: [1000, 2000, 3333, 5000, 3200, 4200, 2200, 2100, 3000, 5100, 2000, 3200, 1800],
        },
      ],
    },

    // 占比饼图
    pie2Option: {
      color: ['#55CCD8', '#FC7690', '#FFCF47', '#2ec7c9'],
      tooltip: {
        trigger: 'item',
        backgroundColor: 'rgba(51, 51, 51, 0.7)',
        textStyle: {
          color: '#fff',
        },
        formatter: '{b}<br/>{c}' + '万吨<br/>' + '{d}%',
      },
      legend: {
        bottom: '10%',
        left: 'center',
        itemWidth: 10,
        itemHeight: 10,
        itemGap: 20,
        textStyle: {
          color: '#85878e',
        },
      },
      series: {
        type: 'pie',
        radius: ['40%', '60%'],
        center: ['50%', '38%'],
        avoidLabelOverlap: false,
        labelLine: {
          show: false,
        },
        data: [
          { value: 345, name: '计划完成' },
          { value: 96, name: '实际完成' },
        ],
      },
    },
    pie3Option: {
      color: ['#55CCD8', '#FC7690', '#FFCF47', '#2ec7c9'],
      series: {
        name: '',
        type: 'pie',
        radius: '50%',
        selectedMode: 'single',
        center: ['50%', '50%'],
        label: {
          color: '#6e7079',
          overflow: 'none',
          formatter: '{b} {d}%\n{c}',
        },
        data: [
          { value: 30, name: '早班（数量）' },
          { value: 32, name: '中班（数量）' },
          { value: 18, name: '晚班（数量）' },
        ],
      },
    },

    barOption: {
      color: ['#00CACF', '#995EFF', '#FF9100'],
      title: {
        text: '单位：万吨',
        left: 20,
        textStyle: {
          color: '#333',
          fontWeight: 'normal',
          fontSize: 12,
        },
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'none',
        },
      },
      legend: {
        itemWidth: 20,
        itemHeight: 10,
        itemGap: 20,
        right: 20,
        textStyle: {
          color: '#85878e',
        },
        data: ['压铸产线', '机加产线'],
      },
      xAxis: [
        {
          type: 'category',
          data: ['2019-07-10', '2019-07-11', '2019-07-12', '2019-07-13', '2019-07-14'],
          axisPointer: {
            type: 'shadow',
          },
        },
      ],
      yAxis: [
        {
          type: 'value',
          name: '',
          min: 0,
          max: 100,
          splitNumber: 4,
        },
        // {
        //   type: 'value',
        //   name: '',
        //   min: 0,
        //   max: 10,
        //   interval: 5,
        //   axisLabel: {
        //     formatter: '{value}%',
        //   },
        // },
      ],
      series: [
        {
          name: '压铸产线',
          type: 'line',
          stack: 'Total',
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: 'rgba(0, 202, 207, .7)',
              },
              {
                offset: 1,
                color: 'rgba(255, 255, 255, .7)',
              },
            ]),
          },
          smooth: false,
          showSymbol: true,
          tooltip: {
            valueFormatter: function (value) {
              return value + ' 万吨';
            },
          },
          data: [30, 10, 40, 60, 30, 60],
        },
        {
          name: '机加产线',
          type: 'line',
          stack: 'Total',
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: 'rgba(152, 99, 193, .7)',
              },
              {
                offset: 1,
                color: 'rgba(255, 255, 255, .7)',
              },
            ]),
          },
          smooth: false,
          showSymbol: true,
          tooltip: {
            valueFormatter: function (value) {
              return value + ' 万吨';
            },
          },
          data: [40, 50, 50, 30, 20, 50],
        },
      ],
    },
    total: 0,

    bar2Option: {
      // legend: {
      //   bottom: 0,
      //   data: ['工废数', '料废数', '其他'],
      // },
      // tooltip: {},
      // radar: {
      //   radius: '60%',
      //   splitNumber: 8,
      //   indicator: [
      //     {
      //       name: '机加一部',
      //     },
      //     {
      //       name: '机加二部',
      //     },
      //     {
      //       name: '机加三部',
      //     },
      //     {
      //       name: '机加四部',
      //     },
      //     {
      //       name: '压铸一部',
      //     },
      //     {
      //       name: '压铸二部',
      //     },
      //   ],
      // },
      // series: [
      //   {
      //     type: 'radar',
      //     symbolSize: 0,
      //     areaStyle: {
      //       shadowBlur: 0,
      //       shadowColor: 'rgba(0,0,0,.2)',
      //       shadowOffsetX: 0,
      //       shadowOffsetY: 10,
      //       opacity: 1,
      //     },
      //     data: [
      //       {
      //         value: [90, 50, 86, 40, 50, 20],
      //         name: '工废数',
      //         itemStyle: {
      //           color: 'rgba(0,202,207,0.4)',
      //         },
      //       },
      //       {
      //         value: [70, 75, 70, 76, 20, 85],
      //         name: '料废数',
      //         itemStyle: {
      //           color: 'rgba(254,143,103,0.4)',
      //         },
      //       },
      //       {
      //         value: [50, 75, 70, 36, 20, 23],
      //         name: '其他',
      //         itemStyle: {
      //           color: 'rgba(140,125,252,0.4)',
      //         },
      //       },
      //     ],
      //   },
      // ],
    },
  });

  const homePieChart = ref<HTMLDivElement>();
  const homePieChart2 = ref<HTMLDivElement>();
  const homePieChart3 = ref<HTMLDivElement>();
  const homeLineChart = ref<HTMLDivElement>();
  const homeBar2Chart = ref<HTMLDivElement>();
  const homeBarChart = ref<HTMLDivElement>();

  onMounted(async () => {
    // data.Statistics = ;
    // data.dataOne = [];
    // data.pieOption.series.data = data.dataOne;
    // data.lineOption.series = [];
    // data.lineOption.xAxis.data = [];

    nextTick(() => {
      let myChart = markRaw(echarts.init(unref(homePieChart) as HTMLDivElement));
      myChart.setOption(data.pieOption, true);
      myChart.resize(); //显示区域大小发生改变更新图表

      let myChart2 = markRaw(echarts.init(unref(homePieChart2) as HTMLDivElement));
      myChart2.setOption(data.pie2Option, true);
      myChart2.resize(); //显示区域大小发生改变更新图表

      let myChart3 = markRaw(echarts.init(unref(homePieChart3) as HTMLDivElement));
      myChart3.setOption(data.pie3Option, true);
      myChart3.resize(); //显示区域大小发生改变更新图表

      let myChart4 = markRaw(echarts.init(unref(homeLineChart) as HTMLDivElement));
      myChart4.setOption(data.lineOption, true);
      myChart4.resize(); //显示区域大小发生改变更新图表

      // let myChart5 = markRaw(echarts.init(unref(homeBar2Chart) as HTMLDivElement));
      // myChart5.setOption(data.bar2Option, true);
      // myChart5.resize(); //显示区域大小发生改变更新图表

      let myChart6 = markRaw(echarts.init(unref(homeBarChart) as HTMLDivElement));
      myChart6.setOption(data.barOption, true);
      myChart6.resize(); //显示区域大小发生改变更新图表
    });
  });
</script>

<style lang="less" scoped>
  .title-chose {
    float: right;
    margin-right: 20px;

    span {
      text-align: center;
      padding: 0 12px;
      display: inline-block;
      color: #333;
      height: 30px;
      line-height: 30px;
      font-size: 14px;
      cursor: pointer;
      margin-left: 10px;

      &.cur,
      &:active {
        background-color: #5e95ff;
        color: #fff;
      }
    }
  }

  .box-bg {
    background-color: #fff;
    box-sizing: border-box;
    border-radius: 5px;
    box-shadow: 0 3px 6px 1px rgb(0 0 0 / 10%);

    .total {
      margin: 20px 20px 0;
      height: 46px;
      padding: 0 10px;
      position: absolute;
      left: 0;
      right: 0;
      box-shadow: 0 3px 6px 1px rgb(0 0 0 / 16%);
      display: flex;
      justify-content: space-between;
      font-size: 30px;
      font-weight: 400;
      align-items: center;
      color: #5e95ff;

      .text {
        font-size: 18px;
        font-weight: 400;
        color: #707070;
      }
    }

    .box-bg-title {
      border-bottom: 1px solid #eee;
      height: 50px;
      line-height: 50px;
      font-size: 16px;
      box-sizing: border-box;
    }

    .title-text {
      padding: 0 15px;
    }

    .item {
      padding: 10px 10px 0;
      height: 312px;
    }
  }

  .mumber-box {
    margin: 2px -5px 10px;
    display: flex;
    justify-content: space-between;

    .mumber {
      padding: 20px;
      box-sizing: border-box;
      margin: 0 5px;
      flex: 1;
      height: 120px;
      background: #fff;
      box-shadow: 0 3px 6px 1px rgb(0 0 0 / 10%);
      border-radius: 5px;

      .mum-left {
        float: left;
        width: 24%;
        margin-top: 6px;

        .index-img {
          display: inline-block;
          width: 100%;
        }
      }

      .mum-right {
        margin-left: 6%;
        float: left;
        width: 70%;

        .mum-title {
          font-size: 14px;
        }

        .mum {
          font-size: 24px;
          font-weight: bolder;
          line-height: 40px;
          color: #5e95ff;
        }

        .mum-than {
          font-size: 14px;

          .icon {
            padding-left: 8px;
          }
        }
      }
    }
  }

  .ant-col {
    margin-bottom: 10px;
  }
</style>
