<template>
  <div style="padding: 20px;">
    <h1 style="text-align: center; margin-bottom: 30px;">消費明細</h1>
    <el-divider></el-divider>
    <el-form  ref="queryForm" :inline="true" :model="queryParams" size="small">
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束日期"
          range-separator="-"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-button
        v-hasPermi="['system:purchaseRecord:export']"
        icon="el-icon-download"
        plain
        size="mini"
        type="warning"
        @click="handleExport"
      >导出
      </el-button>
    </el-row>

    <el-table
      :data="listDate"
      style="width: 100%; border-collapse: collapse;">
      <el-table-column
        label="序号"
        width="300"
        style="border: 1px solid #ccc; padding: 10px; text-align: center;">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column
        label="數據名稱"
        width="300"
        style="border: 1px solid #ccc; padding: 10px; text-align: center;">
        <template slot-scope="scope">
          {{ scope.row.dataName }}
        </template>
      </el-table-column>
      <el-table-column
        label="消費金额"
        width="300"
        style="border: 1px solid #ccc; padding: 10px; text-align: center;">
        <template slot-scope="scope">
          {{ scope.row.amount }}
        </template>
      </el-table-column>
      <el-table-column
        label="消費時間"
        width="300"
        style="border: 1px solid #ca8a8a; padding: 10px; text-align: center;">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <el-card style="margin-top: 50px">
      <div>
        <!-- 装ECharts的容器 -->
        <div id="main" style="width: 100%; height: 520px; background: #fff">
        </div>
      </div>
    </el-card>
  </div>


</template>


<script>
import * as echarts from 'echarts' //引用echarts
import { userloginfo } from "@/api/system/user";
import PanelGroup from "@/views/dashboard/PanelGroup.vue";
export default {
  components: {PanelGroup},
  data() {
    return {
      charts: "",
      opinionData: [], // 数据
      b: [],
      dateRange: [],
      listDate: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      }
    }
  },
  created() {
    this.getList();
  },
  mounted() {
    this.drawLine();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/user/purchaseRecord/export', {
        ...this.queryParams
      }, `pay_${new Date().getTime()}.xlsx`)
    },
    getList() {
      userloginfo(this.addDateRange(this.queryParams,this.dateRange)).then(response => {
          this.listDate = response.data.rows;
          this.total = response.data.total;
          this.drawLine();
        }
      );},
    drawLine() {
      // 初始化折线图
      this.charts = echarts.init(document.getElementById('main'));

      this.b = this.listDate.map(item => item.dataName);
      this.opinionData = this.listDate.map(item => item.amount);

      // 设置折线图数据和样式
      this.charts.setOption({
        title: {
          left: "3%",
          top: "5%",
          text: "消费趋势", // 自定义
        },

        tooltip: {
          trigger: "axis",
        },

        legend: {
          align: "right",
          left: "3%",
          top: "15%",
          data: ["消费金额"], // 自定义
        },

        grid: {
          top: "30%",
          left: "5%",
          right: "5%",
          bottom: "5%",
          containLabel: true,
        },

        toolbox: {
          feature: {
            saveAsImage: {},
          },
        },

        // 自定义：设置x轴刻度
        xAxis: {
          type: "category",
          boundaryGap: true,
          axisTick: {
            alignWithLabel: true,
          },
          // 自定义标签
          data:this.b
          // data: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        },

        // 自定义：设置y轴刻度
        yAxis: {
          type: "value",
          boundaryGap: true,
          splitNumber: 4,
          interval: 250,
        },

        // 设置数据
        series: [
          {
            name: "消费金额", // 自定义
            type: "line",
            stack: "总量", // 自定义
            data: this.opinionData, // 自定义
            areaStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "rgb(255,200,213)",
                  },
                  {
                    offset: 1,
                    color: "#ffffff",
                  },
                ],
                global: false,
              },
            },
            itemStyle: {
              color: "rgb(255,96,64)",
              lineStyle: {
                color: "rgb(255,96,64)",
              },
            },
          },
        ],
      });
    },
  },
}
</script>
