<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper" @keyup.enter="handleEnter">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="关键字">
              <a-input v-model="queryParam.spareName" placeholder="请输入备件名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="年份">
              <a-select v-model="queryParam.year" placeholder="请选择年份">
                <a-select-option
                  label=""
                  value="">全部</a-select-option>
                <a-select-option
                  v-for="(value,index) in yearList"
                  :key="index"
                  :label="value"
                  :value="value">{{ value }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="24">
              <a-form-item label="月份">
                <a-select v-model="queryParam.month" placeholder="请选择月份">
                  <a-select-option
                    label=""
                    value="">全部</a-select-option>
                  <a-select-option
                    v-for="(value,index) in monthList"
                    :key="index"
                    :label="value"
                    :value="index+1">{{ value }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="8 || 24" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm">重置</a-button>
              <a @click="()=>{ this.advanced = !this.advanced}" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator" style="margin-bottom: 8px;">
      <a-button v-if="$auth('monthly-sb-three-rates-add')" type="primary" icon="plus" @click="$refs.baseModal.base()">新增</a-button>
      <a-button style="margin-left: 8px" v-if="$auth('monthly-sb-three-rates-export')" type="primary" icon="download" @click="doExport">导出</a-button>
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0 && $auth('monthly-sb-three-rates-del')">
        <a-menu slot="overlay">
          <a-popconfirm title="是否要删除所选数据？" @confirm="batchDelete()">
            <a-menu-item key="1"><a-icon type="delete" /><a>删除</a></a-menu-item>
          </a-popconfirm>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      showPagination="auto"
    />
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { statisticsByGroupBySparePage } from '@/api/sqarepartmanage/sparepartused'

export default {
  name: 'MonthlySbThreeRateList',
  components: {
    STable,
    Ellipsis
  },
  data () {
    return {
      // 查询参数
      queryParam: {
        spareName: null,
        year: '',
        month: ''
      },
      advanced: false,
      yearList: [],
      monthList: [],
      // 表头
      columns: [
        {
          title: '序号',
          checked: true,
          dataIndex: 'index',
          customRender: (text, record, index) => {
            return `${(this.$refs.table.localPagination.current - 1) * this.$refs.table.localPagination.pageSize + index + 1}`
          }
        },
        {
          title: '年份',
          checked: true,
          dataIndex: 'year',
          customRender: (text, record, index) => {
            return text + '年'
          }
        },
        {
          title: '月份',
          checked: true,
          dataIndex: 'month',
          customRender: (text, record, index) => {
            return text + '月'
          }
        },
        {
          title: '备件',
          checked: true,
          dataIndex: 'spareName'
        },
        {
          title: '更换次数',
          checked: true,
          dataIndex: 'num'
        }
      ],
      // 下拉框map
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          dataScope: {
          }
        }
        return statisticsByGroupBySparePage(Object.assign(parameter, this.queryParam))
          .then(res => {
            for (let i = 0; i < res.data.rows.length; i++) {
              res.data.rows[i].id = i + ''
            }
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  created () {
    const year = this.BaseTool.Moment().format(this.BaseTool.Date.PICKER_NORM_YEAR)
    const yearList = []
    const monthList = []
    for (let i = 0; i <= (year - 2015); i++) {
      yearList.push(year - i)
    }
    for (let i = 1; i <= 12; i++) {
      monthList.push(i + '月')
    }
    this.yearList = yearList
    this.monthList = monthList
    // 下拉框map
    this.tableOption()
  },
  methods: {
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            getCheckboxProps: record => ({
              props: {
                disabled: false,
                name: record.id
              }
            })
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
        spareName: null,
        year: '',
        month: ''
      }
      this.$refs.table.refresh(true)
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      statisticsByGroupBySparePage(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handleEnter () {
      this.$refs.table.refresh(true)
    }
  }
}
</script>
