<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 optionState"
                :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-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      width="500px"
      @close="close"
    >
      <el-form ref="form" :model="form">
        <el-form-item :label="__('活动编号')" prop="activity_is_finish">
          <el-select
            v-model="form.activity_is_finish"
            :placeholder="__('请选择')"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">{{ __('取消') }}</el-button>
        <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
      </div>
    </el-dialog>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" fixed :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="activity_title"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('活动开始时间')"
        prop="activity_starttime"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('活动结束时间')"
        prop="activity_endtime"
        show-overflow-tooltip
        width="160"
      />
      <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, 1)"
          >
            {{ __('商家关闭') }}
          </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="activity_is_finish"
        show-overflow-tooltip
        width="160"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.activity_is_finish === 0"
            type="info"
            @click="changeFinsh(scope.row)"
          >
            {{ __('未完成') }}
          </el-button>
          <el-button
            v-if="scope.row.activity_is_finish === 1"
            disabled
            type="success"
          >
            {{ __('已完成') }}
          </el-button>
          <el-button
            v-if="scope.row.activity_is_finish === 2"
            disabled
            type="danger"
          >
            {{ __('已解散') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('团购商品设定数量')"
        prop="activity_rule.group_quantity"
        show-overflow-tooltip
        width="140"
      />

      <el-table-column
        align="center"
        :label="__('团购商品已售数量')"
        prop="activity_rule.group_sale_quantity"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :label="__('团购活动商品')"
        prop="activity_rule.product_name"
        show-overflow-tooltip
        width="250"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <Update ref="Update" @fetch-data="fetchData" />
    <Edit ref="Edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import {
    doDelete,
    doEdit,
    getList,
    editState,
  } from '@/api/store/activity/base'
  import Edit from './components/StoreActivityBaseEdit'

  export default {
    name: 'StoreActivityBase',
    components: {
      Edit,
    },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        options: [
          {
            value: 0,
            label: this.__('未完成'),
          },
          {
            value: 1,
            label: this.__('已完成'),
          },
          {
            value: 2,
            label: this.__('已解散'),
          },
        ],
        value: '',
        optionState: [
          {
            value: 0,
            label: this.__('未开启'),
          },
          {
            value: 1,
            label: this.__('正常'),
          },
          {
            value: 2,
            label: this.__('已结束'),
          },
          {
            value: 4,
            label: this.__('管理员关闭'),
          },
          {
            value: 3,
            label: this.__('商家关闭'),
          },
        ],
        title: '',
        listLoading: true,
        dialogFormVisible: false,
        form: {},
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          activity_type_id: 1126,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      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()
        })
      },
      changeFinsh(row) {
        this.form = {
          activity_id: row.activity_id,
          activity_is_finish: row.activity_is_finish,
        }

        this.dialogFormVisible = true
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.fetchData()
            this.close()
          } else {
            return false
          }
        })
      },
      handleEdit(row) {
        if (row.activity_id) {
          this.$refs['Edit'].showEdit(row)
        } else {
          this.$refs['Edit'].showEdit()
        }
      },
      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>
