<template>
  <div style="background-color:#F2F3F5">
    <div class="history"  v-loading="loading">
      <div style="background-color:#Fff;padding:20px 20px 0 20px">
          <!-- 顶部搜索 -->
          <div class="toolbar">
            <el-form ref="formParams" :inline="true" :model="formParams">
              <el-form-item label="日期">
                <el-date-picker
                  v-model="formParams.dates"
                  type="daterange"

                   style="width:220px"
                  range-separator="至"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  value-format="yyyy-MM-dd"
                />
              </el-form-item>

              <el-form-item label-width="0">
                <el-button   @click="search"  style="background-color: #3F9B6A;color: #fff;">查询</el-button>
                <el-button  @click="clear" class="buttonHover"
            style="color: #606266;border: 1px solid #dddfe5;background-color: #fff;" >重置</el-button>
              </el-form-item>
            </el-form>
          </div>
      </div>
      <div style="margin-bottom: 20px;background-color: #fff;">

        <div style="border: #EBEEF5 1px solid;width: 100%;">
          <div style="border-bottom: #EBEEF5 1px solid;background-color: #F7F7F7;padding: 10px 15px;">收货地成交图</div>
          <div ref="container1" style="width: 100%; height: 500px;"></div>
        </div>
      </div>
        <!-- 表格 -->
        <div class="content_table" style="padding: 15px 0px 10px 0px;background-color:#fff">
             <div style="padding: 10px;margin-bottom: 20px;">
          <div class="table">
            <el-table
               show-summary
              :data="tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
               :header-cell-style="{fontSize: '14px',color:'#0009',fontWeight: 'normal',backgroundColor:'#F2F3F5'}"
              style="width: 100%"
            >
              <el-table-column prop="address" label="收货地" />
              <el-table-column label="下单数量" prop="num">
                <template slot-scope="scope">
                  {{scope.row.refundOder.length + scope.row.transactionOrder.length}}
                  </template>

              </el-table-column>
              <el-table-column prop="refundOder" label="退单数">
                  <template slot-scope="scope">
                    {{scope.row.refundOder.length}}
                    </template>
              </el-table-column>
              <el-table-column prop="transactionOrder" label="成交数" >
                <template slot-scope="scope">
                      {{scope.row.transactionOrder.length}}
                      </template>
                </el-table-column>
              <el-table-column prop="refundAmount" label="退单金额(元)"  :show-overflow-tooltip="true"/>
              <el-table-column prop="transactionAmount" label="成交金额(元)" :show-overflow-tooltip="true" />
            </el-table>
          </div>
          <!-- 分页 -->
          <div class="fenye">
			  <div style="line-height: 200%;">共{{total}}条</div>
            <el-pagination class="pagination" :hide-on-single-page="flag" background  :current-page="currentPage"
          :page-size="pageSize" layout="prev, pager,next" :total="total"
            @current-change="handleCurrentChange" />
          </div>
       </div>
        </div>
      </div>

  </div>
</template>

<script>
  import * as echarts from 'echarts';
  import {getAllNotPage,deliveryAddressStatistics} from '@/api/psRanking'
  export default {
  data() {
    return {
      total: 0,
      flag:false,
      pageSize: 10,
      currentPage: 1,
      formParams: {
        dates: [],
        page: 1,
        pageSize: 10
      },
      formInline: {
        searchType: '1',
        search: '', // 搜索字段
        state: '',
        // afterState: '', // 售后状态 0-无售后 1-售后中 2-售后成功 3-售后关闭
        dates: [], // 下单时间数组
        page: 1,
        shopName: '',
        pageSize: 10
      },
       tableData: [],
	loading:true
    }
  },
  mounted() {
    this.getAll()

  },
   methods: {
     async getAll(){

     const res =  await getAllNotPage(this.formInline)
	 if(res.code ==''){
		 this.loading = false
	 }
     const Orderlist = await deliveryAddressStatistics(res.data)
     this.tableData = Orderlist.data
     this.total = Orderlist.data.length
    this.echar1(Orderlist.data)

     },
async search(){
    this.formInline.dates  =  this.formParams.dates
  const res =  await getAllNotPage(this.formInline)
  if(res.code ==''){
  		 this.loading = false
  }
  this.currentPage = 1
  const Orderlist = await deliveryAddressStatistics(res.data)
  this.tableData = Orderlist.data
  this.total = Orderlist.data.length
  this.echar1(Orderlist.data)
},
  clear(){
     this.formInline.dates = []
     this.formParams.dates = []
	 this.currentPage = 1
     this.getAll()
  },
  handleCurrentChange(val){
	this.currentPage = val
  },
     echar1(diameter) {
   let Dom = this.$refs.container1;
   let myChart = echarts.init(Dom);
   let processedData = diameter.map(item => ({
       name: item.address,
       value: item.refundAmount
   }));
   console.log(processedData)
   // 指定图表的配置项和数据
   const option = {
	     tooltip: {
	           trigger: 'axis',
	           axisPointer: {
	               type: 'shadow'
	           },
	           formatter: function (params) {
	               return params[0].name + ': ' + params[0].value+'元';
	           }
	       },
       color: ['#3F9B6A'],
       xAxis: {
           type: 'category',
           data: processedData.map(item => item.name),
		   axisLabel: {
		               interval: 0, // 强制显示所有标签
		               formatter: function (value) {
		                   if (value.length > 4) {
		                       return value.slice(0, 4) + '...';
		                   }
		                   return value;
		               }
		           }
       },
       yAxis: {
           type: 'value'
       },
       series: [
           {
               name: '退款金额',
               type: 'bar',
               data: processedData.map(item => item.value),
               label: {
                   show: true,
                   position: 'top'
               },
               itemStyle: {
                   borderRadius: [5, 5, 0, 0]
               },
               emphasis: {
                   label: {
                       fontSize: 20
                   }
               }
           }
       ]
   };


       // 使用刚指定的配置项和数据显示图表。
       option && myChart.setOption(option);
     },
   }
}
</script>

<style lang='scss' scoped>
.history{
  padding: 0px;
  padding-left: 10px;
  min-height: calc(100vh - 50px - 20px);
}
 ::v-deep .el-form-item__label{
        font-weight: normal;
         font-size: 14px;

       }
       ::v-deep .btn .el-button:focus,
        .el-button:hover {
          border: 1px solid #3F9B6A;
        }
        ::v-deep .el-button {
           border: 1px solid #3F9B6A;
         }
          ::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
           background-color: #3F9B6A;
         }
</style>
<style scoped>
.history /deep/ .el-table .cell.el-tooltip img {
  max-height: 100px;
}
.history{
	margin-left:10px;
	background-color: #fff;
}
  .fenye {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    position: relative;
  }
  .pagination {
    text-align: right;
    line-height: 20px;
  }

  
 .toolbar{
   .el-form-item{
     margin-bottom:0px;
   }
 }
</style>
<style>
.el-tooltip__popper {
  max-width: 50%;
}
</style>
