<template>
  <div class="form-wrap">
    <el-form
      class="search-box"
      ref="form"
      :model="form"
      :inline="true">
      <div class="block-title">
        查询条件
      </div>

      <el-row>
        <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
            v-model="form.endDate"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
      </el-row>

      <el-row>
        <el-form-item
          label="年龄段"
        >
          <el-checkbox-group
            v-model="form.keyList1"
            style="display: flex;">
            <div
              v-for="(item,index) in groupKeys"
              :key="index"
              class="checkBoxItem"
            >
              <el-checkbox
                :label="item.value">
                {{ item.label }}
              </el-checkbox>

              <i
                class="el-icon-close close"
                @click="delAgeRange(item,index)"></i>
            </div>
          </el-checkbox-group>

          <el-button @click="dialogVisible=true">
            新增年龄段
          </el-button>
        </el-form-item>
      </el-row>

      <div
        class="btn-wrap"
        style="margin-bottom:20px">
        <el-button
          @click="reset">
          重置
        </el-button>
        <el-button
          :disabled="loading"
          type="primary"
          @click="getAgeSaleStatistic">
          搜索
        </el-button>
        <el-button
          type="primary"
          @click="reportExport">
          导出
        </el-button>
      </div>
    </el-form>

    <div class="table-box">
      <!-- <div class="block-title">
        统计结果
      </div> -->
      <el-table
        border
          v-for="(item,index) in tableRow"
          :key="index"
          :prop="String(index)"
          :label="item">
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      title="新增年龄段"
        <el-button
          type="primary"
          @click="reportExport">
          导出
        </el-button>
      :visible.sync="dialogVisible"
      width="40%"
    >
      <div class="table-box">
        <el-form :inline="true">
          <el-form-item label="年龄下限">
            <el-input-number
              v-model="min"
              :min="0"
              :precision="0"></el-input-number>
          </el-form-item>
          <el-form-item label="年龄上限">
            <el-input-number
              v-model="max"
              :min="0"
              :precision="0"></el-input-number>
          </el-form-item>
          <el-form-item label="预计效果">
            <span :class="desc==='请输入年龄'?'isGrey':''">{{ desc }}</span>
          </el-form-item>
        </el-form>
      </div>

      <span
        slot="footer"
        class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addAgeRange">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getAgeWithIdType, exportAgeWithIdType } from '@/api/queryReport'
// import moment from 'moment'
export default {
  name: 'AgeSale',
  props: {
    activeName: {
      type: String,
      default: ''
    }
  },
  watch: {
    activeName (val) {
      if (val === 'AgeSale') {
        this.getAgeSaleStatistic()
      }
    }
  },
  data () {
    return {
      form: {
        startDate: null,
        endDate: null,
        keyList1: [
          '-6',
          '6-22',
          '22-60',
          '60-'
        ]
      },
      tableData: [],
      tableRow: [], // 列表头部数据
      loading: false,
      groupKeys: [
        { label: '6岁以下', value: '-6' },
        { label: '6岁~22岁', value: '6-22' },
        { label: '22岁~60岁', value: '22-60' },
        { label: '60岁以上', value: '60-' }
      ],
      min: 0,
      max: 0,
      dialogVisible: false
    }
  },
  created () {
    this.getAgeSaleStatistic()
  },
  methods: {
    reset () {
      this.$refs.form.resetFields()
    },

    // 获取日报数据
    getAgeSaleStatistic () {
      this.loading = true
      getAgeWithIdType(this.form).then(res => {
        this.tableRow = res.shift()
        this.tableData = res.map(i => {
          return { ...i }
        })
      }).finally(() => {
        this.loading = false
      })
    },

    // 导出日报数据
    reportExport () {
      exportAgeWithIdType(this.form).then(() => {
        this.$message.success('导出成功。')
        this.goToDownload()
      }, () => {
        this.$message.success('导出失败。')
      })
    },

    addAgeRange () {
      if (this.desc === '请输入年龄') return this.$message.error('请至少一个输入年龄')
      if (this.min && this.max) {
        this.groupKeys.push({ label: `${this.min}岁~${this.max}岁`, value: `${this.min}-${this.max}` })
      } else if (this.min && !this.max) {
        this.groupKeys.push({ label: `${this.min}岁以上`, value: `${this.min}-` })
      } else if (!this.min && this.max) {
        this.groupKeys.push({ label: `${this.max}岁以下`, value: `-${this.max}` })
      }
      this.max = 0
      this.min = 0
      this.dialogVisible = false
    },
    delAgeRange (item, index) {
      this.groupKeys.splice(index, 1)
      this.form.keyList1 = this.form.keyList1.filter(v => v !== item.value)
    }
  },
  computed: {
    desc () {
      if (this.min && this.max) {
        return `${this.min}岁~${this.max}岁`
      } else if (this.min && !this.max) {
        return `${this.min}岁以上`
      } else if (!this.min && this.max) {
        return `${this.max}岁以下`
      } else {
        return `请输入年龄`
      }
    }
  }
}
</script>

  <style scoped>
.isGrey{
    color:#ccc
}
.checkBoxItem{
  position: relative;
  padding-right:40px;
  margin-right:20px;
}
.close{
  position: absolute;
  right:0;
  top:8px;
  text-align: right;
  font-size:16px;
  cursor: pointer;
}
  </style>
