// 入库柱状图
export const transformInBoundChart = (data) => {
  if (!data || !Array.isArray(data)) {
    data = [];
  }
  const xAxis = [];
  const series1 = [];
  const series2 = [];
  data.forEach((_) => {
    const { countnum, countnum1, statusname } = _;
    xAxis.push(statusname);
    series1.push(countnum || 0);
    series2.push(countnum1 || 0);
  });
  return {
    color: ["#1890FF", "#08C571"],
    title: {
      text: "入库信息",
      top: 20,
      left: 20,
    },
    tooltip: {
      trigger: "axis",
    },
    legend: {
      data: ["采购", "销退"],
      right: 20,
      top: 20,
    },
    grid: {
      top: 70,
      left: 20,
      right: 20,
      bottom: 15,
      containLabel: true,
    },
    xAxis: [
      {
        top: 20,
        type: "category",
        boundaryGap: true,
        data: xAxis,
      },
    ],
    yAxis: [{ type: "value" }],
    series: [
      {
        name: "采购",
        type: "bar",
        data: series1,
        itemStyle: {
          barBorderRadius: [5, 5, 0, 0],
        },
      },
      {
        name: "销退",
        type: "bar",
        data: series2,
        itemStyle: {
          barBorderRadius: [5, 5, 0, 0],
        },
      },
    ],
  };
};
// 出库柱状图
export const transformOutBoundChart = (data) => {
  if (!data || !Array.isArray(data)) {
    data = [];
  }
  const xAxis = [];
  const series1 = [];
  data.forEach((_) => {
    const { num, key } = _;
    xAxis.push(key || "");
    series1.push(num || 0);
  });
  return {
    color: ["#FFB447"],
    title: {
      text: "出库信息",
      top: 20,
      left: 20,
    },
    tooltip: {
      trigger: "axis",
    },

    grid: {
      top: 70,
      left: 20,
      right: 20,
      bottom: 15,
      containLabel: true,
    },
    xAxis: {
      type: "category",
      nameTextStyle: {
        color: "red",
      },
      data: xAxis,
    },
    yAxis: {
      type: "value",
    },
    series: {
      type: "bar",
      data: series1,
      itemStyle: {
        barBorderRadius: [5, 5, 0, 0],
      },
    },
  };
};
// 库存总数
export const transformTotalStockPie = (data) => {

    if (!data || !Array.isArray(data)) {
        data = [];
      }
      const xAxis = [];
      const series1 = [];
      data.forEach((_) => {
        const { num, key } = _;
        xAxis.push(key || "");
        series1.push(num || 0);
      });
      return {
        color: ["#FFB447"],
        title: {
          text: "库存总数",
          top: 20,
          left: 20,
        },
        tooltip: {
          trigger: "axis",
        },
    
        grid: {
          top: 70,
          left: 20,
          right: 20,
          bottom: 15,
          containLabel: true,
        },
        xAxis: {
          type: "category",
          nameTextStyle: {
            color: "red",
          },
          data: xAxis,
        },
        yAxis: {
          type: "value",
        },
        series: {
          type: "bar",
          data: series1,
          itemStyle: {
            barBorderRadius: [5, 5, 0, 0],
          },
        },
      };
      return






  if (!data || !Array.isArray(data)) {
    data = [];
  }
  const legend = [];
//   const series1 = [];
  data.forEach((_) => {
    const { key, num } = _;
    legend.push(key);
    series1.push({
      name: key,
      value: num,
    });
  });
  console.log(series1, "====");
  return {
    color: ["#FFA92B", "#1989FA", "#DA60FF", "#08C571"],
    title: {
      text: "库存总数",
      top: 20,
      left: 20,
    },
    grid: {
      x: 0,
    },
    tooltip: {
      trigger: "item",
      formatter: "{b} : {c} ({d}%)",
    },
    legend: {
      z: 2,
      itemWidth: 10,
      itemHeight: 10,
      orient: "horizontal",
      x: "center",

      bottom: 20,
      data: legend,
    },

    calculable: true,
    series: [
      {
        name: "库存总数",
        type: "pie",
        radius: ["50%", "30%"],
        itemStyle: {
          normal: {
            label: {
              show: true,
            },
            labelLine: {
              show: true,
            },
          },
        },
        data: series1,
      },
    ],
  };
};
// 出库条目
export const transformOutBoundPie = (data) => {
  if (!data || !Array.isArray(data)) {
    data = [];
  }
  const legend = [];
  const series1 = [];
  let total = 0;
  data.forEach((_) => {
    const { key, num } = _;
    if (!num) total += num || 0;
    legend.push(key);
    series1.push({
      name: key,
      value: num,
    });
  });
  return {
    color: ["#FAD337", "#3AA0FF"],
    title: {
      text: "出库条目信息",
      top: 20,
      left: 20,
    },
    grid: {
      x: 0,
    },
    tooltip: {
      trigger: "item",
      formatter: "{b} : {c} ({d}%)",
    },
    legend: {
      z: 2,
      itemWidth: 10,
      itemHeight: 10,
      orient: "horizontal",
      x: "center",

      bottom: 20,
      data: legend,
      formatter(name) {
        const currentLegend = series1.find((_) => _.name === name);
        if (!currentLegend || total === 0) return `${name} 0%`;
        const p = Math.round(((currentLegend.value || 0) / total) * 100) + "%";
        return `${name} ${p}`;
      },
    },

    calculable: true,
    series: [
      {
        name: "出库条目信息",
        type: "pie",
        radius: ["50%", "30%"],
        itemStyle: {
          normal: {
            label: {
              show: true,
            },
            labelLine: {
              show: true,
            },
          },
        },
        data: series1,
      },
    ],
  };
};
// 订单出库信息
export const transformCustomerPie = (data) => {
  let { totalOrder = 0, backOrder = 0 } = data;
  const noOrder = totalOrder - backOrder;
  const total = totalOrder;
  return {
    color: ["#FFA92B", "#08C571"],
    title: {
      text: "订单出库信息",
      top: 20,
      left: 20,
    },
    grid: {
      x: 0,
    },
    tooltip: {
      trigger: "item",
      formatter: "{b} : {c} ({d}%)",
    },
    legend: {
      z: 2,
      itemWidth: 10,
      itemHeight: 10,
      orient: "horizontal",
      x: "center",

      bottom: 20,
      data: ["已出库", "未出库"],
      formatter(name) {
        let currentValue = 0;
        if (name === "已出库") {
          currentValue = backOrder;
        }
        if (name === "未出库") {
          currentValue = noOrder;
        }
        if (!total) {
          return `${name} 0%`;
        }
        const p = Math.round(((currentValue || 0) / total) * 100) + "%";
        return `${name} ${p}`;
      },
    },

    calculable: true,
    series: [
      {
        name: "订单出库信息",
        type: "pie",
        radius: ["50%", "30%"],
        itemStyle: {
          normal: {
            label: {
              show: true,
            },
            labelLine: {
              show: true,
            },
          },
        },
        data: [
          { value: backOrder, name: "已出库" },
          { value: noOrder, name: "未出库" },
        ],
      },
    ],
  };
};
