<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-select v-model="query.type" clearable placeholder="请选择统计方式" class="filter-item" style="width: 185px" @keyup.enter.native="crud.toQuery">
          <el-option v-for="item in options" :key="item.name" :label="item.name" :value="item.id" />
        </el-select>
        <date-range-picker v-model="orderTime" class="date-item" />

        <!-- <rrOperation :crud="crud" /> -->
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="getData">搜索</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="resetDate">重置</el-button>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />

      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @row-dblclick="showInfo" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="组织名称" />
        <el-table-column prop="money" label="消费金额" />
        <el-table-column prop="avgMoney" label="人均消费" />
        <el-table-column prop="orderTime" label="统计时段">
          <template slot-scope="{ row }">
            <div v-if="query.type === 'month'">{{ row.orderTime ? row.orderTime.slice(0, 7) : '' }}</div>
            <div v-if="query.type === 'year'">{{ row.orderTime ? row.orderTime.slice(0, 4) : '' }}</div>
          </template>
        </el-table-column>
      </el-table>

      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>
<script>
import crudDmStatement from '@/api/dm/statement/dmStatement'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import DateRangePicker from '@/components/DateRangePicker'
import pagination from '@crud/Pagination'

const defaultForm = { type: 'month' }
export default ({
  name: 'DmStatement',
  components: { pagination, crudOperation, DateRangePicker }, //, rrOperation,deviceTemp, userTemp
  mixins: [presenter(), header(), crud(), form(defaultForm)],
  cruds() {
    return CRUD({ title: '部门用餐报表', url: '/api/dmStatement', tempBtnShow: { user: false }, selectOne: true,
      crudMethod: { ...crudDmStatement }, optShow: { del: false, edit: false, add: false, download: true, reset: true }})
  },
  data() {
    return {
      type: 'month',
      orderTime: [],
      permission: {
        add: ['admin', 'dmPrintingCount:add'],
        edit: ['admin', 'dmPrintingCount:edit'],
        del: ['admin', 'dmPrintingCount:del']
      },
      options: [
        {
          id: 'month',
          name: '按月统计'
        },
        {
          id: 'year',
          name: '按年统计'
        }
      ]
    }
  },
  created() {
    // this.crud.query.type = 'month'
    this.$set(this.crud.query, 'type', 'month')
    this.getData()
  },
  methods: {
    resetDate() {
      this.orderTime = []
      this.crud.query.type = 'month'
      this.getData()
    },
    getData() {
      this.crud.query.stateDate = this.orderTime[0] || ''
      this.crud.query.endDate = this.orderTime[1] || ''
      crudDmStatement.dmStatement({
        ... this.crud.form,
        ... this.crud.query
      }).then(res => {
        this.crud.page.total = res.totalElements
        this.crud.data = res.content
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return false
    }
  }
})
</script>
