<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>
        </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>
        </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 class="item">
              <div class="unit">单位：件</div>
              <div>
                <div class="pucharse" v-for="it in data.barData" :key="it.name">
                  <span class="labels">{{ it.name }}</span>
                  <div class="values">
                    <div class="actual" :style="{ width: (it.actual / it.plan) * 100 + '%' }"></div>
                    <div style="position: absolute; right: 10px">{{ it.actual }}/{{ it.plan }}</div>
                  </div>
                </div>
              </div>
            </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';

  const formate = (name) => {
    let str = name + '：';
    data.pieOption?.series?.data?.forEach((o: any) => {
      if (name == o.name) {
        str += o.value + '吨';
      }
    });
    return str;
  };

  const formate2 = (name) => {
    let str = name + '';
    data.bar2Option?.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: '年度' },
    ],
    barData: [
      {
        name: '5M',
        plan: 111,
        actual: 22,
      },
      {
        name: '环境',
        plan: 211,
        actual: 22,
      },
      {
        name: '材料',
        plan: 11,
        actual: 22,
      },
      {
        name: '检验方法',
        plan: 121,
        actual: 22,
      },
      {
        name: '其他',
        plan: 21,
        actual: 22,
      },
    ],
    cur: 0,
    cur2: 0,
    colors: ['#5E95FF', '#00CACF', '#FF9100', '#995EFF', '#5E6EFF'],
    Statistics: [
      {
        title: '质量总问题数量（件）',
        icon: '/analisis/quality.png',
        value: 710,
        total: 120000,
        color: '#FC7690',
        action: '',
      },
      {
        title: '机加问题数量（件）',
        icon: '/analisis/quality.png',
        value: 126,
        total: 500000,
        color: '#FC7690',
        action: '',
      },
      {
        title: '压铸问题数量（件）',
        icon: '/analisis/quality.png',
        value: 155,
        total: 120000,
        color: '#FC7690',
        action: '环比 1.62%',
      },
      {
        title: '外购件/装配不良问题（件）',
        icon: '/analisis/quality.png',
        value: '84%',
        total: 50000,
        color: '#FC7690',
        action: '',
      },
      {
        title: '客户投诉问题（件）',
        icon: '/analisis/quality.png',
        value: 21,
        total: 50000,
        color: '#FC7690',
        action: '',
      },
    ],
    dataOne: [],
    // 玫瑰饼图
    pieOption: {
      color: ['#4460DE', '#55CCD8', '#FC7690', '#FE8F67', '#FFCF47', '#7A93F7'],
      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: 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: ['#7A93F7', '#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',
          axisLabel: {
            interval: 0, // 坐标轴刻度标签的显示间隔
            rotate: 125, // 标签倾斜的角度
          },
          data: [
            'J1-平面废',
            'J2-碰撞划伤',
            'J3-孔径度',
            'J1-平面废',
            'J1-平面废',
            'J1-平面废',
            'J1-平面废',
          ],
          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(122,147,247, .7)',
              },
              {
                offset: 1,
                color: 'rgba(255, 255, 255, .7)',
              },
            ]),
          },
          smooth: true,
          showSymbol: false,
          tooltip: {
            valueFormatter: function (value) {
              return value + ' 件';
            },
          },
          data: [40, 50, 50, 30, 20, 50, 32, 19],
        },
      ],
    },

    // 占比饼图
    pie2Option: {
      color: ['#00F9FF', '#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',
          axisLabel: {
            interval: 0, // 坐标轴刻度标签的显示间隔
            rotate: 120, // 标签倾斜的角度
          },
          data: ['机加101 车间', '机加102 车间', '机加103 车间', '机加104 车间', '机加105 车间'],
          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: 15,
          data: [3000, 2000, 3333, 5000, 3200, 4200, 3200, 2100, 3000, 5100, 6000, 3200, 4800],
        },
      ],
    },
    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',
          axisLabel: {
            interval: 0, // 坐标轴刻度标签的显示间隔
            rotate: 120, // 标签倾斜的角度
          },
          data: ['M1-气孔', 'M2-漏气', 'M3-缺料', 'M4-裂纹', 'M5-变形', 'M6-杂质', 'M7-霉斑'],
          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: true,
          showSymbol: false,
          tooltip: {
            valueFormatter: function (value) {
              return value + ' 件';
            },
          },
          data: [40, 50, 50, 30, 20, 50, 32, 19],
        },
      ],
    },
    total: 0,

    bar2Option: {
      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',
        },
        formatter: formate2,
      },
      series: {
        type: 'pie',
        radius: ['40%', '60%'],
        center: ['50%', '38%'],
        avoidLabelOverlap: false,
        labelLine: {
          show: false,
        },
        data: [
          { value: 345, name: '压铸一部' },
          { value: 96, name: '压铸二部' },
        ],
      },
    },
  });

  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%);

    .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;
      width: 100%;
      height: 300px;
      box-sizing: border-box;

      .unit {
        font-size: 12px;
        color: #707070;
        margin-bottom: 30px;
      }

      .pucharse {
        display: flex;
        margin-bottom: 24px;
        padding: 0 10px;

        .labels {
          width: 60px;
          font-size: 12px;
          color: #707070;
          line-height: 24px;
          text-align: right;
          padding-right: 10px;
        }

        .values {
          background: rgb(94 149 255 / 50%);
          height: 24px;
          line-height: 24px;
          flex: 1;
          font-size: 12px;
          color: #fff;
          text-align: right;
          display: flex;
          justify-content: space-between;
          position: relative;

          .actual {
            background: linear-gradient(270deg, #5e95ff 0%, #695eff 100%);
            height: 24px;
          }

          &:hover {
            background: rgb(255 196 96 / 50%);

            .actual {
              background: linear-gradient(270deg, #ffc45e 0%, #ff9100 100%);
            }
          }
        }
      }
    }
  }

  .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>
