<template>
  <div class="card-type-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button v-if="rid == 2" icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.card_type_name"
              clearable
              :placeholder="__('请输入券码名称')"
              style="margin-right: 10px"
            />
          </el-form-item>
          <el-form-item v-if="rid == 9">
            <el-select
              v-model="queryForm.store_id"
              clearable
              filterable
              :loading="loading"
              :placeholder="__('请输入店铺名称')"
              remote
              :remote-method="remoteFindUserList"
              reserve-keyword
            >
              <el-option
                v-for="item in dataList"
                :key="item.store_id"
                :label="item.store_name"
                :value="item.store_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.card_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
        v-if="rid == 2"
        align="center"
        fixed="left"
        :label="__('操作')"
        width="120"
      >
        <template #default="{ row }">
          <el-button
            :disabled="row.card_state === 1"
            type="text"
            @click="handleEdit(row)"
          >
            {{ __('修改') }}
          </el-button>
          <el-button
            :disabled="row.card_state === 1"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        v-if="rid == 9"
        align="center"
        :label="__('店铺编号')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="rid == 9"
        align="center"
        :label="__('店铺名称')"
        prop="store_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('券码编号')"
        prop="card_type_id"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        :label="__('券码名称')"
        prop="card_type_name"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('券码类型')"
        prop="app_id"
        show-overflow-tooltip
        width="130"
      >
        <template slot-scope="scope">
          <el-button v-if="scope.row.app_id === 0" type="info">
            {{ __('自营券码') }}
          </el-button>
          <el-button v-if="scope.row.app_id === 1" type="success">
            {{ __('第三方券码') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        v-if="rid == 9"
        align="center"
        :label="__('券码数量')"
        prop="card_use_num"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="rid == 2"
        align="center"
        :label="__('券码数量')"
        prop="card_use_num"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-row>
            <el-button type="text" @click="showCardHistoryTable(scope.row)">
              {{ scope.row.card_use_num }}
            </el-button>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('券码描述')"
        prop="card_type_desc"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :formatter="formatStarttime"
        :label="__('有效开始时间')"
        prop="card_type_starttime"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :formatter="formatEndtime"
        :label="__('有效结束时间')"
        prop="card_type_endtime"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('卡片Logo')"
        prop="card_type_image"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-popover placement="top-start" title="" trigger="hover">
            <img
              v-if="scope.row.card_type_image"
              alt=""
              :src="scope.row.card_type_image"
              style="width: 150px; height: 150px"
            />
            <img
              v-if="scope.row.card_type_image"
              slot="reference"
              :src="scope.row.card_type_image"
              style="width: 30px; height: 30px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('券码状态')"
        prop="card_state"
        show-overflow-tooltip
        width="130"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.card_state === 0"
            type="info"
            @click="changeState(scope.row.card_type_id, 1)"
          >
            未开启
          </el-button>
          <el-button
            v-if="scope.row.card_state === 1"
            type="success"
            @click="changeState(scope.row.card_type_id, 0)"
          >
            正常
          </el-button>
          <el-button v-if="scope.row.card_state === 2" disabled type="danger">
            {{ __('已结束') }}
          </el-button>
        </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 { getList, doEdit, doDelete } from '@/api/card/type'
  import { renderTime } from '@/utils/date'
  import Edit from './components/payCardTypeEdit'
  import Table from './components/payCardHistoryTable'
  import {mapGetters} from "vuex";
  import {getList as storeList} from "@/api/store/base";

  export default {
    name: 'PayCardType',
    components: { Edit, Table },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        dataList: [],
        loading: true,
        options: [
          {
            value: 0,
            label: this.__('未开启'),
          },
          {
            value: 1,
            label: this.__('正常'),
          },
          {
            value: 2,
            label: this.__('已结束'),
          },
          {
            value: 4,
            label: this.__('管理员关闭'),
          },
          {
            value: 3,
            label: this.__('商家关闭'),
          },
        ],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          pay_type: 3,
          title: '',
        },
      }
    },
    computed: {
      ...mapGetters({
        rid: 'user/rid',
      }),
    },
    created() {
      this.fetchData()
    },
    methods: {
      renderTime,
      formatEndtime(row) {
        const cardTypeEndtime = row.card_type_endtime
        if (!cardTypeEndtime) return ''
        return renderTime(cardTypeEndtime)
      },
      formatStarttime(row) {
        const cardTypeStarttime = row.card_type_starttime
        if (!cardTypeStarttime) return ''
        return renderTime(cardTypeStarttime)
      },
      __,
      handleEdit(row) {
        if (row.card_type_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('请确认是否删除该券码？'), null, async () => {
          const { msg, status } = await doDelete({
            card_type_id: row.card_type_id,
          })
          this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
          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()
      },
      showCardHistoryTable(row) {
        this.$refs['Table'].showTable(row)
      },
      async remoteFindUserList(store_name) {
        if (store_name !== '') {
          this.loading = true
          setTimeout(() => {
            this.loading = false
            storeList({ store_name: store_name,pageSize: 200 }).then((res) => {
              this.dataList = res.data.items
            })
          }, 200)
        } else {
          this.dataList = []
        }
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      async changeState(card_type_id, state) {
        this.$baseConfirm(this.__('请确认是否更改状态？'), null, async () => {
          const { msg, status } = await doEdit({
            card_type_id: card_type_id,
            card_state: state,
          })
          this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
          await this.fetchData()
        })
      },
    },
  }
</script>
