<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="图表名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入图表名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建用户ID" prop="userId">
        <el-input
            v-model="queryParams.userId"
            placeholder="请输入创建用户ID"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="handleQuery">
          <template #icon>
            <el-icon><Search /></el-icon>
          </template>
          <template #default>
            搜索
          </template>
        </el-button>
        <el-button size="mini" @click="resetQuery">
          <template #icon>
            <el-icon><RefreshRight /></el-icon>
          </template>
          <template #default>
            重置
          </template>
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="chartList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="图表ID" align="center" prop="id" />
      <el-table-column label="图表名称" align="center" prop="name" />
      <el-table-column label="分析目标" align="center" prop="analysisTarget" />
      <el-table-column label="图标类型" align="center" prop="chartType" />
      <!--<el-table-column label="生成的分析结论" align="center" prop="analysisConclusion" />-->
      <el-table-column label="创建用户ID" align="center" prop="userId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              type="primary"
              size="mini"
              @click="handleUpdate(scope.row)"
          >
            <template #icon>
              <el-icon><TrendCharts /></el-icon>
            </template>
            详情
          </el-button>
          <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              :icon="InfoFilled"
              icon-color="#626AEF"
              title="确定删除这个分析的图表结果吗"
              @confirm="handleDelete(scope.row)"
              @cancel="cancelEvent"
          >
            <template #reference>
              <el-button type="danger">
                <template #icon>
                  <el-icon><Remove /></el-icon>
                </template>
                <template #default>
                  删除
                </template>
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
        v-show="total>0"
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 15, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="getList"
        @current-change="getList"
    />

    <!-- 显示图表和结论详情的对话框 -->
    <el-dialog v-model="open" width="60%" center append-to-body>
      <template #header>数据分析详情</template>
      <template #default>
        <el-row>
          <el-col :span="12">
            <el-card>
              <template #header>数据图表</template>
              <template #default>
                <div ref="chart" style="height:50vh"/>
              </template>
            </el-card>
          </el-col>
          <el-col :span="12">
            <el-card>
              <template #header>分析结论</template>
              <template #default>
                <div style="height:50vh">
                  <el-text type="primary" size="large" style="margin-top: 20px">
                    {{analysisConclusion}}
                  </el-text>
                </div>
              </template>
            </el-card>
          </el-col>
        </el-row>
      </template>
      <template #footer class="dialog-footer">

      </template>
    </el-dialog>
  </div>
</template>

<script>
import { listChart, delChart } from "@/common/api/chart";
import {ElMessage} from "element-plus";
import * as echarts from 'echarts';


export default {
  name: "Chart",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 图表管理表格数据
      chartList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        analysisTarget: null,
        chartType: null,
        userId: null,
      },
      //图表的配置对象
      option:{
        xAxis: {
          type: 'category',
          data: ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'],
          boundaryGap: true,
          axisLabel:{
            interval:0,
            rotate : 50
          },
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          name:'增长人数',
          type:'line',
          data:[34.0, 34.0, 3.0, 3.0, 43.0, 34.0, 43.0, 34.0, 34.0, 12.0, 12.0, 12.0, 12.0, 32.0, 12.0, 32.0, 32.0, 32.0, 32.0],
          markPoint: {
            data: [
              {type: 'max', name: '最大值'},
              {type: 'min', name: '最小值'}
            ]
          },
          markLine: {data: [
              {type: 'average', name: '平均值'}
            ]},
          smooth: true
        }]
      },
      analysisConclusion:'',
      chart:undefined
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询图表管理列表 */
    getList() {
      this.loading = true;
      listChart(this.queryParams).then(response => {
        this.chartList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        analysisTarget: null,
        chartData: null,
        chartType: null,
        generatedChartData: null,
        analysisConclusion: null,
        userId: null,
        createdTime: null,
        updatedTime: null,
        deleteFlag: null
      };
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.analysisConclusion = row.analysisConclusion;
      let generatedChartData = row.generatedChartData;
      let optionJson = eval("(" +generatedChartData+ ")");
      //返回格式不稳定,有可能会多封装一层option,需要判断一下
      if (optionJson.option === undefined || optionJson.option === null){
        this.option = optionJson;
      }else{
        this.option = optionJson.option;
      }
      this.open = true;
      this.refreshChart();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      delChart(ids).then(res => {
        if (res.code === 200) {
          ElMessage({
            message: '删除成功',
            type: 'success'
          })
        }else {
          ElMessage({
            message: res.msg,
            type: 'error'
          })
        }
        this.getList();
      }).error(err => {
        ElMessage({
          message: 'err',
          type: 'error'
        })
      });
      // this.$modal.confirm('是否确认删除图表管理编号为"' + ids + '"的数据项？').then(function() {
      //   return delChart(ids);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("删除成功");
      // }).catch(() => {});
    },
    //更新图表
    refreshChart(){
      setTimeout(()=>{
        if (this.chart === undefined){
          this.chart = echarts.init(this.$refs.chart);
        }
        this.chart.clear();
        setTimeout(()=>{
          this.chart.setOption(this.option);
        },500)
      },500);
    }
  }
};
</script>
