<template>
  <div class="pa-3 white">
    <m-search class="mb-3" :items="searchItems" v-model="searchValues" @search="onSearch"></m-search>
    <m-table
      v-loading="loading"
      :items="items"
      :headers="headers"
      :page-size="pageInfo.size"
      :page-current="pageInfo.current"
      :total="total"
      @page-change="onPageChange"
    >
      <template #card-tools>
        <m-button type="orange" icon="el-icon-plus" @click="onAdd">申报</m-button>
      </template>
      <template #actions="{ row }">
        <m-button type="primary" text @click="onEdit(row)">编辑</m-button>
        <m-button type="danger" text @click="onDelete(row)">删除</m-button>
      </template>
    </m-table>
    <AccumulatePointsApplyEdit ref="accumulatePointsApplyEditRefs"></AccumulatePointsApplyEdit>
  </div>
</template>

<script>
import {
  getAccumulatePointList,
  deleteAccumulatePoint
} from '@/api/accumulatePoint'
import AccumulatePointsApplyEdit from './accumulatePointsApplyEdit.vue'
export default {
  name: 'accumulatePointsEntering',
  components: {
    AccumulatePointsApplyEdit
  },
  data () {
    return {
      searchItems: [
        {
          label: '名称',
          prop: 'name',
          type: 'input',
          options: {
            placeholder: '请输入名称'
          }
        }
      ],
      searchValues: {
        name: null
      },
      headers: [
        { label: '名称', prop: 'name' },
        { label: '状态', prop: 'status' },
        { label: '操作', prop: 'actions', fixed: 'right', width: 300 }
      ],
      items: [],
      total: 0,
      pageInfo: {
        current: 1,
        size: 10
      },
      loading: false
    }
  },
  created () {
    this.onInit()
  },
  methods: {
    async onInit () {
      this.loading = true
      try {
        const { data } = await getAccumulatePointList({
          ...this.searchValues,
          ...this.pageInfo
        })
        this.items = data.records
        this.total = data.total
      } catch (error) {
        this.$message.error(error)
      } finally {
        this.loading = false
      }
    },
    onAdd () {
      this.$refs.accumulatePointsApplyEditRefs.open()
    },
    onEdit (item) {
      this.$refs.accumulatePointsApplyEditRefs.open(item)
    },
    onDelete (row) {
      this.$confirm('确定删除吗？', '提示')
        .then(async () => {
          try {
            await deleteAccumulatePoint({ id: row.id })
            this.$message.success('删除成功')
            this.onInit()
          } catch (error) {
            this.$message.error(error)
          }
        })
        .catch(_ => {})
    },
    onSearch () {
      this.pageInfo.current = 1
      this.onInit()
    },
    onPageChange (index) {
      this.pageInfo.current = index
      this.onInit()
    }
  }
}
</script>

<style lang="scss" scoped>
  /* 自定义样式 */
</style>
