<template>
  <div class="app-container">

    <div class="filter-container">
      <el-select
        v-model="listQuery.sysOrigin"
        :placeholder="$t('pages.luckyBoxAwardConfig.filter.system')"
        style="width: 120px"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option
          v-for="item in permissionsSysOriginPlatformAlls"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span style="float: left;"> <sys-origin-icon :icon="item.value" :desc="item.value" /></span>
          <span style="float: left;margin-left:10px">{{ item.label }}</span>
        </el-option>
      </el-select>
      <el-select
        v-model="listQuery.lotteryType"
        :placeholder="$t('pages.luckyBoxAwardConfig.filter.lotteryType')"
        style="width:120px;"
        class="filter-item"
        clearable
        @change="handleSearch"
      >
        <el-option :label="$t('pages.luckyBoxAwardConfig.lotteryType.CLASSICS')" :value="'CLASSICS'" />
        <el-option :label="$t('pages.luckyBoxAwardConfig.lotteryType.CONSTELLATION')" :value="'CONSTELLATION'" />
      </el-select>
      <el-button
        :loading="searchLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      >
        {{ $t('common.search') }}
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleAdd"
      >
        {{ $t('pages.luckyBoxAwardConfig.action.add') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      highlight-current-row
    >
      <el-table-column prop="sysOrigin" :label="$t('pages.luckyBoxAwardConfig.table.platform')" align="center" />
      <el-table-column :label="$t('pages.luckyBoxAwardConfig.table.type')" align="center">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.lotteryType === 'CLASSICS'">{{ $t('pages.luckyBoxAwardConfig.lotteryType.CLASSICS') }}</el-tag>
            <el-tag v-else-if="scope.row.lotteryType === 'CONSTELLATION'">{{ $t('pages.luckyBoxAwardConfig.lotteryType.CONSTELLATION') }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" :label="$t('pages.luckyBoxAwardConfig.table.name')" align="center" width="200" />
      <el-table-column prop="createTime" :label="$t('pages.luckyBoxAwardConfig.table.createdAt')" align="center" width="200">
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('pages.luckyBoxAwardConfig.table.actions')" align="center" width="380">
        <template slot-scope="scope">
          <el-button type="text" @click.native="hanldeEidt(scope.row)">{{ $t('pages.luckyBoxAwardConfig.action.edit') }}</el-button>
          <el-button type="text" @click.native="handleAwardConfigList(scope.row)">{{ $t('pages.luckyBoxAwardConfig.action.rewardConfig') }}</el-button>
          <el-button type="text" @click.native="handlDel(scope.row)">{{ $t('pages.luckyBoxAwardConfig.action.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />
    <config-edit
      v-if="awardConfigEditVisible"
      :update-data="updateData"
      :sys-origin="listQuery.sysOrigin"
      @close="awardConfigEditVisible = false"
      @success="awardConfigEditSuccess"
    />

    <details-config
      v-if="detailsConfigVisible"
      :award-id="thatSelectedAwardConfigId"
      :event="luckyBoxEditEvent"
      @close="detailsConfigVisible=false"
    />

  </div>
</template>

<script>
import { getAwardConfig, deleteAwardConfig } from '@/api/game-lucky-box-config'
import Pagination from '@/components/Pagination'
import ConfigEdit from './edit.vue'
import DetailsConfig from './details.vue'
import { mapGetters } from 'vuex'

export default {
  name: 'AwardConfigTable',
  components: {
    Pagination, ConfigEdit, DetailsConfig
  },
  data() {
    return {
      awardConfigEditVisible: false,
      detailsConfigVisible: false,
      thatSelectedAwardConfigId: '',
      updateData: null,
      luckyBoxEditEvent: 'ADD',
      thisRow: {},
      list: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 20,
        sysOrigin: 'HALAR',
        lotteryType: ''
      },
      listLoading: true,
      searchLoading: false,
      clickUserId: ''
    }
  },
  computed: {
    ...mapGetters(['permissionsSysOriginPlatformAlls', 'permissionsFirstSysOrigin'])
  },
  created() {
    if (!this.permissionsFirstSysOrigin) {
      return
    }
    this.listQuery.sysOrigin = this.permissionsFirstSysOrigin.value
    this.renderData(true)
  },
  methods: {
    renderData(isReset) {
      const that = this
      if (isReset === true) {
        that.listQuery.cursor = 1
      }
      that.listLoading = true
      getAwardConfig(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.searchLoading = that.listLoading = false
      }).catch(er => {
        that.searchLoading = that.listLoading = false
      })
    },
    handleSearch() {
      this.searchLoading = true
      this.renderData(true)
    },
    handleAwardConfigList(row) {
      this.thatSelectedAwardConfigId = String(row.id)
      this.detailsConfigVisible = true
    },
    handleAdd() {
      this.awardConfigEditVisible = true
      this.updateData = null
    },
    hanldeEidt(row) {
      this.awardConfigEditVisible = true
      this.updateData = row
    },
    awardConfigEditSuccess(event) {
      this.updateData = null
      this.awardConfigEditVisible = false
      this.renderData(event === 'create')
    },
    // 删除
    handlDel(row) {
      this.$confirm(this.$t('pages.luckyBoxAwardConfig.confirm.deleteMessage'), this.$t('pages.luckyBoxAwardConfig.confirm.title'), {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        deleteAwardConfig(row.id).then((res) => {
          this.listLoading = false
          this.$message({
            message: this.$t('pages.luckyBoxAwardConfig.message.deleteSuccess'),
            type: 'success'
          })
          this.renderData()
        })
      }).catch(() => {
        this.listLoading = false
      })
    }
  }
}
</script>
<style scoped lang="scss">
.this-level {
  padding-bottom: 30px;
  display: flex;
  > div {
    width: 150px;
  }
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
