<template>
  <div class="app-container">
      <div class="app-content">

<div class="title" style="display: flex; justify-content: center; align-items: center;">药品订单统计</div>


<el-form class="search-form" :inline="true" label-width="90px">
                <el-form-item   label="                     统计">
                  <el-select v-model="queryParams.type" placeholder="请选择日期">
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="店铺名" prop="companyId">
               <el-select v-model="queryParams.storeId" placeholder="所属店铺" clearable size="small">
                       <el-option
                         v-for="dict in storeOPtions"
                         :key="dict.dictValue"
                         :label="dict.dictLabel"
                         :value="dict.dictValue"
                       />
                     </el-select>
                </el-form-item>
                <el-form-item>
                   <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                   <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-form>
              </div>
  <div ref="chartContainer" style="width: 100%; height: 400px;"></div>
  <el-button class="export" size="small"  @click="handleExport" style="float: right; margin-bottom: 15px; margin-right: 40px;" >导出</el-button>
 <div class="table-box" style="margin-left: 40px; margin-right: 40px;">

                        <el-table
                        :data="list"
                        border

                        show-summary
                        style="width: 100%;">
                        <el-table-column
                          prop="name"
                          label="店铺名">
                        </el-table-column>
                        <el-table-column
                          prop="count"
                          label="订单数">
                        </el-table-column>
                        <el-table-column
                          prop="money"
                          label="订单金额">
                        </el-table-column>
                      </el-table>
                  </div>
  </div>

</template>

<script>
import { listStore } from "@/api/his/storeProduct";
import echarts from 'echarts'
import { orderChartData,storeOrderChart,storeOrderChartStore ,storeExport} from "@/api/his/index";
export default {
  data() {
    return {
      exportLoading: false,
      queryParams:{
        type: null,
        storeId: null,
      },
      list:[],
      storeOPtions:[],
      chart: null,
      data: [
      ],
      options: [{
         value: '1',
         label: '今天'
       }, {
         value: '2',
         label: '昨天'
       }, {
         value: '3',
         label: '本周'
       }, {
         value: '4',
         label: '上周'
       }, {
         value: '5',
         label: '本月'
       }
       , {
         value: '6',
         label: '上月'
       },
       {
         value: '7',
         label: '本季度'
       },
       {
         value: '8',
         label: '上季度'
       }
       , {
         value: '9',
         label: '本年'
       }
       , {
         value: '10',
         label: '去年'
       }],
    };
  },
  created() {
    listStore().then(response => {
      this.storeOPtions = response.rows;
    });
  },
  mounted() {
    this.chart = echarts.init(this.$refs.chartContainer, 'macarons');
    this.chart.on('mouseout', this.hideTooltip);
    this.updateChart();
    this.getorderChartData();
  },

  watch: {
    data() {
      this.updateChart();
    },
  },



  methods: {
 /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
            this.exportLoading = true;
          return storeExport(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },

    handleQuery(){
      storeOrderChart(this.queryParams).then(response => {
        this.data = response.data;
      });

      storeOrderChartStore(this.queryParams).then(response => {
        this.list = response.data;
      });
    },
 /** 重置按钮操作 */
    resetQuery() {
     this.queryParams={
       type: null,
       storeId: null,
     }
     this.getorderChartData()
    },

    getorderChartData(){
      orderChartData().then(response => {
        this.data = response.data;
      });
      storeOrderChartStore(this.queryParams).then(response => {
        this.list = response.data;
      });
    },


    formatter(params) {
      const item = params[0];
      return  `
              <div>
                <div>日期：${item.axisValue}</div>
                <div>收入金额：${item.value}元</div>
                <div>订单数量：${item.data.info}</div>
              </div>
            `;;
    },

    updateChart() {
      const option = {
        xAxis: {
          type: 'category',
          data: this.data.map((item) => item.label),
        },
        yAxis: {
          type: 'value',
        },
        tooltip: {
          trigger: 'axis',
          formatter: this.formatter,
          axisPointer: {
            type: "line",
            lineStyle: {
              color: "rgba(227, 242, 252, 0.39)",
              width: 40,
              type: "solid",
            },
            z: 0,  //注意要设置层级，不然会在覆盖在柱子前面，设置为0就在柱子后面显示了。
          },
        },
        series: [
          {
            name: 'Value',
            type: 'line',
            barWidth: '40%',
            itemStyle: {
              color: '#000000',
            },
            label: {
              show: true,
              position: 'top',
            },
            data: this.data.map((item) => ({ value: item.value, info: item.info })),
          },
        ],


      };
      this.chart.setOption(option);
    },

    hideTooltip() {
      this.chart.dispatchAction({
        type: 'hideTip',
      });
    },
  },
};
</script>
<style>
  .title{
    padding: 20px 30px 0px 30px;
    font-size: 18px;
    font-weight: bold;
    color: black;

  }
  .search-form{
    margin: 20px 30px 0px 30px;
  }
  .echart-box{
    margin: 0 auto;
    text-align: center;
  }
  .el-select{
    margin: 5px 10px;
  }
  .table-box{
    margin-top: 15px;
    .export{
      float: right;
      margin: 10px 0px;
    }
  }
.app-container{
    border: 1px solid #e6e6e6;
    padding: 12px;


}
 .app-content{
      background-color: white;



    }
    .data-box{
      padding: 30px;
      background-color:  rgb(255, 255, 255);
      height: 100%;


    }
</style>
