<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true"
      label-width="70px">
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="起始时间"
        prop="startDate">
        <el-date-picker
          v-model="form.startDate"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="截止时间"
        prop="endDate">
        <el-date-picker
          default-time="23:59:59"
          v-model="form.endDate"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="销售渠道"
        prop="keyWords1">
        <el-select
          v-model="form.keyWords1"
          placeholder="请选择"
        >
          <el-option
            label="全部"
            :value="null"></el-option>
          <el-option
            v-for="(item,index) in saleChannel"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="票种"
        prop="keyWords2">
        <el-select v-model="form.keyWords2">
          <el-option
            value=""
            label="全部"></el-option>
          <el-option
            v-for="item in ticketTypeList"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="售票员"
        prop="keyWords3">
        <el-select v-model="form.keyWords3">
          <el-option
            label="全部"
            value=""></el-option>
          <el-option
            v-for="item in salesList"
            :key="item.id"
            :label="item.nickName"
            :value="item.nickName">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="景点"
        prop="keyWords4">
        <el-select v-model="form.keyWords4">
          <el-option
            value=""
            label="全部"></el-option>
          <el-option
            v-for="item in userScenic"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="统计方式"
        prop="keyWords3">
        <el-select
          v-model="form.keyWords5"
          clearable
          placeholder="请选择">
          <el-option
            v-for="item in statistType"
            :key="item.value"
            :label="item.name"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>

      <div class="btn-wrap">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getList">
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="exportStatistics">
          导出
        </el-button>
      </div>
    </el-form>

    <div
      class="table-box"
      v-loading="loading"
      style="width:100%">
      <!-- <div class="block-title">
        统计信息
      </div> -->
      <div
        class="total-data"
        v-if="totalData">
        <span>总售票张数:<b>{{ totalData.actual_count }}</b></span>
        <span>总售票金额:<b>{{ totalData.actual_price }}</b></span>
        <span>退票张数:<b>{{ totalData.cancel_count }}</b></span>
        <span>退票金额:<b>{{ totalData.cancelPrice }}</b></span>
        <span>总张数:<b>{{ totalData.sum_count }}</b></span>
        <span>总金额:<b>{{ totalData.sum_price }}</b></span>
      </div>
      <v-chart
        autoresize
        style="width:100%"
        :options="dayOption"
        v-if="tableData" />
    </div>
  </div>
</template>

<script>
import { getScenic, getTicketTypeList } from '@/api/ticketType'
import { getOrderStatisticsByDate, exportOrderStatisticsByDate } from '@/api/queryReport'
import moment from 'moment'
import dayOption from './dayOption'
const statistType = [
  {
    name: '天',
    value: 'day'
  }, {
    name: '周',
    value: 'week'
  }, {
    name: '月',
    value: 'month'
  }, {
    name: '年',
    value: 'year'
  }
]

const dataMap = {
  '退票张数': 'cancel_count',
  '退票金额': 'cancelPrice',
  '售票张数': 'actual_count',
  '售票金额': 'actual_price'
}
export default {
  computed: {
    salesList () {
      return this.$store.state.user.salesList
    },
    saleChannel () {
      return this.$store.state.app.saleChannel
    },
    userScenic () {
      return this.$store.state.app.account.managerScenicMatrix ? this.$store.state.app.account.managerScenicMatrix : this.scenicList
    }
  },
  data () {
    return {
      dayOption,
      statistType,
      scenicList: [],
      ticketTypeList: [],
      totalData: null,
      form: {
        startDate: new Date(moment().startOf('day').format('YYYY-MM-DD HH:mm:ss')),
        endDate: new Date(moment().endOf('day').format('YYYY-MM-DD HH:mm:ss')),
        keyWords1: null, // 销售渠道id
        keyWords2: '', // 票种id
        keyWords3: '', // 售票员名
        keyWords4: '', // 景点
        keyWords5: 'day' // 统计方式
      },
      tableData: null,
      loading: false
    }
  },
  created () {
    this.getList()
    this.getScenicList()
    this.getTicketTypeList()
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },
    exportStatistics () {
      exportOrderStatisticsByDate(this.form).then(res => {
        this.$message.success('导出成功。')
        this.goToDownload()
      })
    },
    // 获取票种列表
    getTicketTypeList () {
      return getTicketTypeList({
        pageNum: 1,
        pageSize: 999
      }).then(res => {
        this.ticketTypeList = res.list
      })
    },
    // 获取景点列表
    getScenicList () {
      return getScenic().then(res => {
        this.scenicList = res?.data.children.map(item => item.data)
      })
    },
    getList () {
      this.loading = true
      getOrderStatisticsByDate(this.form).then(res => {
        if (!res.data) {
          this.tableData = []
          this.totalData = {
            actual_count: 0,
            actual_price: 0,
            cancel_count: 0,
            cancelPrice: 0,
            sum_count: 0,
            sum_price: 0
          }
          return
        }
        this.tableData = res.data.filter(i => i.date !== '总计')
        this.totalData = res.data.find(i => i.date === '总计')
        let dayOption = JSON.parse(JSON.stringify(this.dayOption))
        dayOption.xAxis[0].data = this.tableData.map(item => item.date)

        dayOption.series = dayOption.series.map(item => {
          console.log(dataMap[item.name])
          item.data = this.tableData.map(i => {
            return i[dataMap[item.name]]
          })
          return item
        })
        console.log(dayOption.series)
        this.dayOption = dayOption
        // dayOption.series = dataMap.map((key, val) => {
        //   console.log(key, val)
        // })
        // this.dayOption = {
        //   xAxis: [{
        //     data: res.map(item => item.date.slice(5))
        //   }],
        //   series: [
        //     {
        //       type: 'line',
        //       name: '售票金额',
        //       data: res.map(item => item.sum_price),
        //       symbol: 'none',
        //       itemStyle: {
        //         color: '#4E84FD'
        //       },
        //       areaStyle: {
        //         color: '#F1FAFE'
        //       }
        //     },
        //     {
        //       type: 'line',
        //       name: '售票张数',
        //       data: res.map(item => item.sum_count),
        //       symbol: 'none',
        //       itemStyle: {
        //         color: '#A359FD'
        //       },
        //       areaStyle: {
        //         color: '#F1FAFE'
        //       }
        //     },
        //     {
        //       type: 'line',
        //       name: '退票张数',
        //       data: res.map(item => item.cancel_count),
        //       symbol: 'none',
        //       itemStyle: {
        //         color: '#FE635F'
        //       },
        //       areaStyle: {
        //         color: '#F1FAFE'
        //       }
        //     }
        //   ]
        // }
      }).finally(() => {
        this.loading = false
      })
    }
  },
  watch: {
    tableData (res) {

    }
  }
}
</script>

<style scoped lang="scss">
.total-data{
  text-align: center; line-height: 3;
  span{
    margin: 0 15px
  }
  b{
    font-size: 1.4em
  }
}
</style>
