<template>
  <div class="store-activity-base-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('新增') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.activity_name"
              clearable
              :placeholder="__('请输入活动标题')"
              style="margin-right: 10px"
            />
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.activity_state"
              clearable
              :placeholder="__('请选择活动状态')"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" 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
            :disabled="row.activity_state !== 0"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('活动标题')"
        prop="activity_title"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatStarttime"
        :label="__('活动开始时间')"
        prop="activity_starttime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatEndtime"
        :label="__('活动结束时间')"
        prop="activity_endtime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('等级要求')"
        prop="activity_use_level_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('活动状态')"
        prop="activity_state"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.activity_state === 0"
            type="info"
            @click="changeState(scope.row.activity_id, 1)"
          >
            {{ __('未开启') }}
          </el-button>
          <el-button
            v-if="scope.row.activity_state === 1"
            type="success"
            @click="changeState(scope.row.activity_id, 3)"
          >
            {{ __('正常') }}
          </el-button>
          <el-button
            v-if="scope.row.activity_state === 2"
            disabled
            type="danger"
          >
            {{ __('已结束') }}
          </el-button>
          <el-button
            v-if="scope.row.activity_state === 3"
            type="success"
            @click="changeState(scope.row.activity_id, 0)"
          >
            {{ __('商家关闭') }}
          </el-button>
          <el-button
            v-if="scope.row.activity_state === 4"
            disabled
            type="success"
          >
            {{ __('管理员关闭') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('活动规则')"
        prop="item_id"
        show-overflow-tooltip
        width="400"
      >
        <template slot-scope="scope">
          <div
            v-if="
              scope.row.item == undefined ||
              (scope.row.item.length <= 0 && scope.row.activity_state != 2)
            "
          >
            <el-button type="text" @click="handleTable(scope.row)">
              {{ __('请选择商品') }}
            </el-button>
          </div>
          <div v-if="scope.row.item">
            <el-row v-for="item in scope.row.item" :key="item.item_id">
              <el-button type="text" @click="handleTable(scope.row)">
                {{ item.product_item_name }}
              </el-button>
            </el-row>
          </div>
        </template>
      </el-table-column>
    </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" />
    <Table ref="Table" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doDelete, editState, getList } from '@/api/store/activity/base'
  import Edit from './components/StoreActivityBaseEdit'
  import Table from './components/storeActivityBuyItemTable'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'StoreActivityBase',
    components: { Edit, Table },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        options: [
          {
            value: 0,
            label: this.__('未开启'),
          },
          {
            value: 1,
            label: this.__('正常'),
          },
          {
            value: 2,
            label: this.__('已结束'),
          },
          {
            value: 4,
            label: this.__('管理员关闭'),
          },
          {
            value: 3,
            label: this.__('商家关闭'),
          },
        ],
        value: '',
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          activity_type_id: 1103,
        },
      }
    },
    async created() {
      await this.fetchData()
    },
    methods: {
      __,
      handleTable(row) {
        this.$refs['Table'].showTable(row)
      },
      formatStarttime(row) {
        return renderTime(row.activity_starttime)
      },
      formatEndtime(row) {
        return renderTime(row.activity_endtime)
      },
      handleEdit(row) {
        if (row.activity_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            activity_id: row.activity_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 changeState(activity_id, state) {
        this.$baseConfirm(this.__('请确认是否更改状态？'), null, async () => {
          const { msg, status } = await editState({
            activity_id: activity_id,
            activity_state: state,
          })
          this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
          await 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>
