<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"
        fixed="left"
        :label="__('操作')"
        width="100"
      >
        <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
        width="120"
      />

      <el-table-column
        align="center"
        :label="__('优惠券图标')"
        prop="activity_rule.voucher_image"
        show-overflow-tooltip
        width="150px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.activity_rule.voucher_image"
            :preview-src-list="[scope.row.activity_rule.voucher_image]"
            :src="scope.row.activity_rule.voucher_image"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('消费金额')"
        prop="activity_rule.requirement.buy.subtotal"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('优惠券面额')"
        prop="activity_rule.voucher_price"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('需要积分数')"
        prop="activity_rule.requirement.points.needed"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('领取记录')"
        show-overflow-tooltip
        width="100"
      >
        <template #default="{ row }">
          <el-button type="text" @click="showRecord(row)">
            {{ __('领取信息') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('已领取张数')"
        prop="activity_rule.voucher_quantity_use"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :formatter="formatEndtime"
        :label="__('有效期')"
        prop="activity_endtime"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('活动状态')"
        prop="activity_state"
        show-overflow-tooltip
        width="130"
      >
        <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="__('优惠券商品')"
        show-overflow-tooltip
        width="250"
      >
        <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="showTable(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="showTable(scope.row)">
                {{ item.product_item_name }}
              </el-button>
            </el-row>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatType"
        :label="__('线上线下')"
        prop="activity_on_is_off"
        show-overflow-tooltip
        width="120"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.activity_on_is_off === 0">
            {{ __('线上活动') }}
          </span>
          <span v-if="scope.row.activity_on_is_off === 1">
            {{ __('线下活动') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatType"
        :label="__('领取方式')"
        prop="activity_type"
        show-overflow-tooltip
        width="120"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <Table ref="Table" @fetch-data="fetchData" />
    <Record ref="Record" @fetch-data="fetchData" />
    <edit ref="edit" @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/ActivityBuyItemTable'
  import Record from './components/VoucherRecordTable'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'StoreActivityBase',
    components: { Edit, Table, Record },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        options: [
          {
            value: 0,
            label: this.__('未开启'),
          },
          {
            value: 1,
            label: this.__('正常'),
          },
          {
            value: 2,
            label: this.__('已结束'),
          },
          {
            value: 4,
            label: this.__('管理员关闭'),
          },
          {
            value: 3,
            label: this.__('商家关闭'),
          },
        ],
        value: '',
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          activity_type_id: 1105,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatType(row) {
        return row.activity_type === 1
          ? this.__('免费参与')
          : row.activity_type === 2
          ? this.__('积分参与')
          : row.activity_type === 3
          ? this.__('购买参与')
          : this.__('分享参与')
      },
      formatEndtime(row) {
        return renderTime(row.activity_endtime)
      },
      handleEdit(row) {
        if (row.activity_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      showTable(row) {
        this.$refs['Table'].showTable(row)
      },
      showRecord(row) {
        this.$refs['Record'].showTable(row)
      },
      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()
        })
      },
      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()
        })
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
