<template>
  <div class="base-user-level-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-button icon="el-icon-refresh" type="primary" @click="queryData">
              {{ __('刷新') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('等级编号')"
        prop="user_level_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('等级名称')"
        prop="user_level_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('升级经验值')"
        prop="user_level_exp"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('发展粉丝')"
        prop="user_level_fans_user"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('推广店铺')"
        prop="user_level_fans_store"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('累计消费')"
        prop="user_level_spend"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('购买产品升级')"
        prop="user_level_product_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('图标')"
        prop="user_level_logo"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.user_level_logo"
            :preview-src-list="[scope.row.user_level_logo]"
            :src="scope.row.user_level_logo"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('折扣率')"
        prop="user_level_rate"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('修改时间')"
        prop="user_level_time"
        show-overflow-tooltip
        width="160"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { doDelete, getList } from '@/api/base/user/level'
  import Edit from './components/BaseUserLevelEdit'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'BaseUserLevel',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      handleEdit(row) {
        if (row.user_level_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      formatTime(row) {
        const user_level_time = row.user_level_time
        return user_level_time ? renderTime(user_level_time) : ''
      },
      handleDelete(row) {
        if (row.user_level_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({
              user_level_id: row.user_level_id,
            })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
