<template>
  <div class="div-new-box-new">
    <div class="content-new-content">
      <div class="div-line"></div>
      <div class="title">近12月延期出货情况分析</div>
      <div style="float: right">
        <departmentSalesperson ref="departmentSalesperson" class="inline-16" @depSelect="depSelect" @personSelect="personSelect"></departmentSalesperson>
        <span class="search__label inline-16">分析日期:</span>
        <el-date-picker
            v-model="enddate"
            style="margin-right: 10px !important;"
            type="date"
            :clearable="false"
            @change="changeDate"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            size="small"
            range-separator="至"
            start-placeholder="开始月份"
            end-placeholder="结束月份">
        </el-date-picker>
      </div>
    </div>
    <div class="chart">
      <div id="delayedShipmentAnalysisChart" style="height: 400px;"></div>
    </div>
    <div style="font-size: 16px;color: #333333;margin: 10px 0 10px 0">{{param.content.dateType?param.content.dateType:'全部'}}</div>
    <div style="height: 400px">
      <tableTemplate  ref="table" :layout="tablecols" :data="list" :opwidth="200" :custom="true" height="356px">
        <template v-slot:customcol="scope">
          <p v-if="scope.column.columnname === 'province'">
            {{scope.column.data.province+scope.column.data.city+scope.column.data.county}}
          </p>
          <p v-else-if="scope.column.columnname === 'marketprice'">
                <span v-if="scope.column.data[scope.column.columnname]">
                  ¥{{tool.formatAmount(scope.column.data[scope.column.columnname],2)}}
                </span>
            <span v-else>--</span>
          </p>
          <p v-else-if="scope.column.columnname === 'defaultprice'">
                <span v-if="scope.column.data[scope.column.columnname]">
                  ¥{{tool.formatAmount(scope.column.data[scope.column.columnname],2)}}
                </span>
            <span v-else>--</span>
          </p>
          <p v-else-if="scope.column.columnname === 'defaultamount'">
                <span v-if="scope.column.data[scope.column.columnname]">
                  ¥{{tool.formatAmount(scope.column.data[scope.column.columnname],2)}}
                </span>
            <span v-else>--</span>
          </p>
          <p v-else-if="scope.column.columnname === 'price'">
                <span v-if="scope.column.data[scope.column.columnname]">
                  ¥{{tool.formatAmount(scope.column.data[scope.column.columnname],2)}}
                </span>
            <span v-else>--</span>
          </p>
          <p v-else-if="scope.column.columnname === 'amount'">
                <span v-if="scope.column.data[scope.column.columnname]">
                  ¥{{tool.formatAmount(scope.column.data[scope.column.columnname],2)}}
                </span>
            <span v-else>--</span>
          </p>
          <p v-else>{{scope.column.data[scope.column.columnname]?scope.column.data[scope.column.columnname]:'--'}}</p>
        </template>
      </tableTemplate>
      <div  class=" " style="text-align:right;float: right;margin: 10px 0 0 0">
        <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :pager-count="7"
            :page-sizes="[20, 50,100, 200]"
            :page-size="20"
            layout="total,sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { Bar } from '@antv/g2plot';
import departmentSalesperson from "@/views/salesData/components/departmentSalesperson";
import tableTemplate from '@/views/salesData/components/table';
export default {
  name: "delayedShipmentAnalysis",
  props:['dataid'],
  components:{departmentSalesperson,tableTemplate},
  data(){
    return {
      chartBar:null,
      param:{
        "id": 20231012163804,
        "content": {
          "type": 0,
          "dataid": '',
          "enddate": "",//分析日期
          "dateType": "",//图标联动  一周 一月 一季 一季以上 可不传 不传默认查全部
          "pageSize":20,
          "pageNumber":1
        }
      },
      list:[],
      chartData:[],
      tablecols:[],
      currentPage:0,
      total:0,
      enddate:new Date().getFullYear() + '-' +  (new Date().getMonth() + 1) + '-' + new Date().getDate(),
      hoverData:[]
    }
  },
  methods:{
    async listData(val){
      this.renderPie(val)
    },
    async queryModel(val){
      this.param.content.dataid = val || this.dataid
      const res = await this.$api.requested(this.param)
      if (res.data.length === 1){
        if (res.data[0].isEmpty == 1){
          this.list = []
          this.total = 0
          this.currentPage = 0
        }else {
          this.list = res.data
          this.total = res.total
          this.currentPage = res.data.currentPage
        }
      }else {
        this.list = res.data
        this.total = res.total
        this.currentPage = res.data.currentPage
      }
      this.chartData = res.data[0].trend.map(item=>{
        return {
          "sequence": item.sequence,
          "value": item.value,
          "key": item.key,
          "type":'订单行数'
        }
      })
      this.chartBar.changeData(this.chartData)
    },
    renderPie(val){
     this.chartBar = new Bar('delayedShipmentAnalysisChart', {
        data:this.chartData,
        xField: 'value',
        yField: 'key',
        seriesField: 'type',
        legend: {
          position: 'top-left',
        },
       interactions: [{ type: 'element-single-selected' }]
      });
      this.chartBar.on('plot:click',ev=>{
        const states = this.chartBar.getStates()
        this.param.content.dateType = states.length === 0 ?'':ev.data.data.key
        this.param.content.pageNumber = 1
        this.queryTable(this.param.content.dataid)
      })
      this.chartBar.render();
      this.queryModel(val)
    },
    async queryTable(val){
      this.param.content.dataid = val || this.dataid
      const res = await this.$api.requested(this.param)
      this.list = res.data
      this.total = res.total
      this.currentPage = res.pageNumber
    },
    changeDate(){
      this.queryModel(this.param.content.dataid)
    },
    /*选择部门*/
    depSelect(val){
      this.param.content.type = 1
      this.param.content.dataid = val
      this.queryModel(val)
    },
    /*选择业务员*/
    personSelect(val){
      this.param.content.type = 0
      this.param.content.dataid = val
      this.queryModel(val)
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.queryTable(this.param.content.dataid)
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.queryTable(this.param.content.dataid)
    },
  },
  created() {
    this.tablecols = this.tool.tabelCol(this.$route.name).delayedShipmentTable.tablecols
  }
}
</script>

<style scoped>

</style>
