<template>
  <div class="app-container">
    <div style="margin-left:16px;">
      <div v-show="viewState == 1">
        <button-bar ref="btnbar" view-title="在途商品管理表" :btndisabled="btndisabled" @btnhandle="btnHandle" />
        <div class="main-content">
          <div class="searchcon">
            <el-button size="small" class="searchbtn" @click="clicksearchShow">
              {{ searchxianshitit }}
            </el-button>
            <div v-show="isSearchShow" class="search">
              <el-form ref="queryParams" :model="queryParams" :inline="true" class="tab-header">
                <el-form-item label="日期">
                  <el-date-picker v-model="queryParams.date" type="date" clearable value-format="yyyy-MM-dd"
                    placeholder="选择日期" />
                </el-form-item>
                <el-form-item label="类别">
                  <el-select v-model="queryParams.type" filterable placeholder="全部" clearable>
                    <el-option v-for="item in state_list" :key="item.dictKey" :label="item.dictValue"
                      :value="item.dictKey">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-form>
              <div class="btn" style="text-align: center;">
                <el-button type="primary" size="small" icon="el-icon-search" @click="dosearch">查询</el-button>
                <el-button type="primary" size="small" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
              </div>
            </div>
          </div>

          <div id="main" style="width: 100%;height: 500px;min-height: 480px;margin-top: 30px;"></div>

          <div style="margin-top: 30px;">
            <el-table v-loading="tableLoading" :data="thresholdAnalysisTable" border style="width: 100%">
              <el-table-column width="60" prop="num" label="序号" type="index" :index="indexMethod" abort=""
                align="center" />
              <el-table-column prop="date" label="数据日期" align="center" />
              <el-table-column prop="stockValue" label="仓库货值(元)" align="center" />
              <el-table-column prop="onWayGoodsValue" label="在途货值(元)" align="center" />
              <el-table-column prop="storeValue" label="门店货值(元)" align="center" />
              <el-table-column prop="accountBalance" label="账户余额(元)" align="center" />
              <el-table-column prop="accountsReceivable" label="应收账款(元)" align="center" />
              <el-table-column prop="paymentAmount" label="借款金额(元)" align="center" />
              <el-table-column prop="threshold" label="阀值" align="center" />
            </el-table>
          </div>

          <!-- End 项目列表 -->
          <div class="pages">
            <div class="tit" />
            <!--  翻页  -->
            <pagination v-show="thresholdAnalysisTable.length > 0" :total="queryParams.total"
              :page.sync="queryParams.current" :limit.sync="queryParams.size" @pagination="loadList" />
          </div>
        </div>


      </div>
    </div>
  </div>
</template>

<script>
  import req from '@/api/supervise/thresholdanalysis'
  import ButtonBar from '@/components/ButtonBar'
  import Pagination from '@/components/pagination'
  import pageye from '@/components/pagination/pageye'
  import * as echarts from 'echarts';

  export default {
    components: {
      ButtonBar,
      Pagination,
      pageye
    },
    // components: {  },
    data() {
      return {
        btndisabled: false,
        viewState: 1, // 1、列表 2、添加 3、修改 4、查看
        isSearchShow: false,
        searchxianshitit: '显示查询条件',
        tableLoading: false,
        tableKey: 0,
        // 类型1.全部,2.常规商品3.烟草
        state_list: [{
            dictKey: 1,
            dictValue: '全部'
          },
          {
            dictKey: 2,
            dictValue: '常规商品'
          }, {
            dictKey: 3,
            dictValue: '烟草'
          }
        ],
        btnList: [ // 右上角按钮
          {
            type: 'success',
            size: 'small',
            icon: 'export',
            btnKey: 'build',
            btnLabel: '导出'
          },
          {
            type: 'info',
            size: 'small',
            icon: 'cross',
            btnKey: 'doClose',
            btnLabel: '关闭'
          }
        ],
        queryParams: {
          current: 1,
          size: 10,
          total: 0,
          params: {
            customerSid: '',
            date: "",
            type: '',
          } // 类别key
        },
        nowDate: new Date().getFullYear() + "-" + (new Date().getMonth() + 1) + "-" + new Date().getDate(),
        thresholdAnalysisTable: [{
          num: '1',
          date: '2023-01-01',
          stockValue: '319314',
          onWayGoodsValue: '3474943',
          storeValue: '369546',
          accountBalance: '435234',
          accountsReceivable: '565234',
          paymentAmount: '544632',
          threshold: "454657"
        }, {
          num: '2',
          date: '2023-01-02',
          stockValue: '434577',
          onWayGoodsValue: '324536',
          storeValue: '8765234',
          accountBalance: '876543',
          accountsReceivable: '324365',
          paymentAmount: '7238734',
          threshold: "3454743"
        }, {
          num: '3',
          date: '2023-01-03',
          stockValue: '345467',
          onWayGoodsValue: '634242',
          storeValue: '2367364',
          accountBalance: '734534',
          accountsReceivable: '2356734',
          paymentAmount: '743345',
          threshold: "36345"
        }, {
          num: '4',
          date: '2023-01-04',
          stockValue: '435632',
          onWayGoodsValue: '2455234',
          storeValue: '32434523',
          accountBalance: '2343523',
          accountsReceivable: '565234',
          paymentAmount: '234234',
          threshold: "45676453"
        }, {
          num: '5',
          date: '2023-01-05',
          stockValue: '453423',
          onWayGoodsValue: '1324354',
          storeValue: '65342',
          accountBalance: '13243',
          accountsReceivable: '54342',
          paymentAmount: '34323',
          threshold: "3243424"
        }],
        analysisChartData: {
          title: {
            text: ''
          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          toolbox: {
            feature: {
              saveAsImage: {
                show:false
              }
            }
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
          },
          yAxis: {
            type: 'value'
          },
          series: [{
              name: 'Email',
              type: 'line',
              stack: 'Total',
              data: [120, 132, 101, 134, 90, 230, 210]
            },
            {
              name: 'Union Ads',
              type: 'line',
              stack: 'Total',
              data: [220, 182, 191, 234, 290, 330, 310]
            },
            {
              name: 'Video Ads',
              type: 'line',
              stack: 'Total',
              data: [150, 232, 201, 154, 190, 330, 410]
            },
            {
              name: 'Direct',
              type: 'line',
              stack: 'Total',
              data: [320, 332, 301, 334, 390, 330, 320]
            },
            {
              name: 'Search Engine',
              type: 'line',
              stack: 'Total',
              data: [820, 932, 901, 934, 1290, 1330, 1320]
            }
          ]
        }


      }
    },
    mounted() {
      this.$refs['btnbar'].setButtonList(this.btnList)
    },
    created() {
      this.loadList()
    },
    methods: {
      // 搜索条件效果
      clicksearchShow() {
        this.isSearchShow = !this.isSearchShow
        if (this.isSearchShow) {
          this.searchxianshitit = '隐藏查询条件'
        } else {
          this.searchxianshitit = '显示查询条件'
        }
      },
      btnHandle(btnKey) {
        switch (btnKey) {
          case 'toAdd':
            this.toAdd()
            break
          case 'doDel':
            this.doDel()
            break
          case 'doClose':
            this.doClose()
            break
          default:
            break
        }
      },
      loadList() {
        this.tableLoading = true
        let _this = this
        req.listPage(_this.queryParams).then((resp) => {
          _this.tableLoading = false
          if (resp.success) {
            const data = resp.data
            _this.queryParams.total = data.total
            _this.thresholdAnalysisTable = data.thresholdAnalysisTable
            _this.analysisChartData = data.analysisChartData
             _this. drawLine()
             _this.tableLoading = false
          } else {
            // 根据resp.code进行异常情况处理
            _this.thresholdAnalysisTable = []
            _this.analysisChartData = {}
            _this.queryParams.total = 0
          }
        }).catch(() => {
          _this.tableLoading = false
        })
      },
      // 序号
      indexMethod(index) {
        var pagestart = (this.queryParams.current - 1) * this.queryParams.size
        var pageindex = index + 1 + pagestart
        return pageindex
      },
      // 折线图初始化
      drawLine() {
        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
        // 绘制图表
        console.log('option', this.analysisChartData)
        myChart.setOption(this.analysisChartData);
      },

      dosearch() {

        this.nowDate = this.queryParams.date;
        this.loadList();

      },
      resetQuery() {
        this.queryParams = {
          current: 1,
          size: 10,
          total: 0,
          params: {
            customerSid: '',
            date: '',
            type: '',
          }
        }
        this.loadList()
      },
      // 导出
      doBuild() {
        this.$message({
          showClose: true,
          type: 'success',
          message: '功能暂未完善...'
        })
      },
      // 返回
      doClose() {
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.go(-1)
      }
    }
  }
</script>

<style>
  .current-data {
    width: 100%;
    height: 500px;
  }

  .current-data .upper {
    width: 100%;
    height: 100px;
  }

  .current-data .lower {
    width: 100%;
    height: 600px;
    float: left;
    margin-left: 10px;

  }
</style>
