<template>
  <div class="classes-base-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="6">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="18">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-select
              v-model="queryForm.status"
              clearable
              :placeholder="__('请选择')"
              @change="queryData"
            >
              <el-option
                v-for="item in searchList"
                :key="item.lable"
                :label="item.lable"
                :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-button icon="el-icon-refresh" type="primary" @click="queryData">
            {{ __('刷新') }}
          </el-button>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
      @selection-change="setSelectRows"
    >
      <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="name"
        show-overflow-tooltip
      />
      <el-table-column align="center" :label="__('价格类型')" width="100">
        <template slot-scope="scope">
          <div class="tag-group">
            <el-tag v-if="scope.row.priceType == 1">{{ __('一口价') }}</el-tag>
            <el-tag v-else-if="scope.row.priceType == 2">
              {{ __('价格区间') }}
            </el-tag>
            <el-tag v-else>{{ __('显示折扣价') }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('价格1')"
        prop="price"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('价格2')"
        prop="price2"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('小程序路径')"
        prop="url"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.offset"
      :layout="layout"
      :page-size="queryForm.limit"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getApproved, deleteGood } from '@/api/base/product/live'
  import Edit from './components/LiveEdit.vue'

  export default {
    name: 'Live',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        searchList: [
          { value: 0, lable: this.__('未审核') },
          { value: 1, lable: this.__('审核中') },
          { value: 2, lable: this.__('审核通过') },
          { value: 3, lable: this.__('审核驳回') },
        ],
        queryForm: {
          offset: 1,
          limit: 20,
          status: 2,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.item_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.goodsId) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await deleteGood({
              goodsId: row.goodsId,
            })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        } else {
          this.$baseMessage(this.__('未选中任何行'), 'error')
          return false
        }
      },
      handleSizeChange(val) {
        this.queryForm.limit = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.offset = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.offset = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getApproved(this.queryForm)
        this.list = data.items
        this.total = data.total
        this.listLoading = false
      },
    },
  }
</script>
