<template>
  <div class="main-container">
    <el-card class="box-card !border-none" shadow="never">

      <div class="flex justify-between items-center">
        <span class="text-lg">{{ pageName }}</span>
        <el-button type="primary" @click="addEvent">
          {{ t('addCoupons') }}
        </el-button>
      </div>

      <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
        <el-form :inline="true" :model="couponsTable.searchParam" ref="searchFormRef">
          <el-form-item :label="t('name')" prop="name">
            <el-input v-model="couponsTable.searchParam.name" :placeholder="t('namePlaceholder')"/>
          </el-form-item>
          <el-form-item :label="t('nominalValue')" prop="nominal_value">
            <range-input v-model="couponsTable.searchParam.nominal_value"/>
          </el-form-item>


          <el-form-item :label="t('isShow')" prop="is_show">
            <el-select class="w-[280px]" v-model="couponsTable.searchParam.is_show" clearable
                       :placeholder="t('isShowPlaceholder')">
              <el-option label="全部" value=""></el-option>
              <el-option
                  v-for="(item, index) in is_showList"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="loadCouponsList()">{{ t('search') }}</el-button>
            <el-button @click="resetForm(searchFormRef)">{{ t('reset') }}</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <div class="mt-[10px]">
        <el-table :data="couponsTable.data" size="large" v-loading="couponsTable.loading">
          <template #empty>
            <span>{{ !couponsTable.loading ? t('emptyData') : '' }}</span>
          </template>
          <el-table-column prop="id" :label="t('id')" min-width="120" :show-overflow-tooltip="true"/>

          <el-table-column prop="name" :label="t('name')" min-width="120" :show-overflow-tooltip="true"/>

          <el-table-column prop="nominal_value" :label="t('nominalValue')" min-width="120"
                           :show-overflow-tooltip="true"/>

          <el-table-column prop="stock" :label="t('stock')" min-width="120" :show-overflow-tooltip="true"/>

          <el-table-column :label="t('isShow')" min-width="180" align="center" :show-overflow-tooltip="true">
            <template #default="{ row }">
              <div v-for="(item, index) in is_showList">
                <div v-if="item.value == row.is_show">{{ item.name }}</div>
              </div>
            </template>
          </el-table-column>

          <el-table-column :label="t('isObsolete')" min-width="180" align="center" :show-overflow-tooltip="true">
            <template #default="{ row }">
              <div v-for="(item, index) in is_showList">
                <div v-if="item.value == row.is_obsolete">{{ item.name }}</div>
              </div>
            </template>
          </el-table-column>

          <el-table-column :label="t('isLongTermValid')" min-width="180" align="center" :show-overflow-tooltip="true">
            <template #default="{ row }">
              <div v-for="(item, index) in is_showList">
                <div v-if="item.value == row.is_long_term_valid">{{ item.name }}</div>
              </div>
            </template>
          </el-table-column>

          <el-table-column :label="t('validTime')" min-width="180" align="center" :show-overflow-tooltip="true">
            <template #default="{ row }">
              {{ row.valid_time || '' }}
            </template>
          </el-table-column>


          <el-table-column :label="t('createTime')" min-width="180" align="center" :show-overflow-tooltip="true">
            <template #default="{ row }">
              {{ row.create_time || '' }}
            </template>
          </el-table-column>

          <el-table-column :label="t('operation')" fixed="right" min-width="120">
            <template #default="{ row }">
              <el-button type="primary" link @click="editEvent(row)">{{ t('edit') }}</el-button>
              <el-button type="primary" link @click="deleteEvent(row.id)">{{ t('delete') }}</el-button>
            </template>
          </el-table-column>

        </el-table>
        <div class="mt-[16px] flex justify-end">
          <el-pagination v-model:current-page="couponsTable.page" v-model:page-size="couponsTable.limit"
                         layout="total, sizes, prev, pager, next, jumper" :total="couponsTable.total"
                         @size-change="loadCouponsList()" @current-change="loadCouponsList"/>
        </div>
      </div>

      <edit ref="editCouponsDialog" @complete="loadCouponsList"/>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import {reactive, ref, watch} from 'vue'
import {t} from '@/lang'
import {useDictionary} from '@/app/api/dict'
import {getCouponsList, deleteCoupons,} from '@/addon/hygl/api/coupons'
import {img} from '@/utils/common'
import {ElMessageBox, FormInstance} from 'element-plus'
import Edit from '@/addon/hygl/views/coupons/components/coupons-edit.vue'
import {useRoute} from 'vue-router'

const route = useRoute()
const pageName = route.meta.title;

let couponsTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  searchParam: {
    "name": "",
    "nominal_value": [],
    "is_show": ""
  }
})

const searchFormRef = ref<FormInstance>()

// 选中数据
const selectData = ref<any[]>([])

// 字典数据
const is_showList = ref([] as any[])
const is_showDictList = async () => {
  is_showList.value = await (await useDictionary('is_show_radio')).data.dictionary
}
is_showDictList();

/**
 * 获取优惠券列表
 */
const loadCouponsList = (page: number = 1) => {
  couponsTable.loading = true
  couponsTable.page = page

  getCouponsList({
    page: couponsTable.page,
    limit: couponsTable.limit,
    ...couponsTable.searchParam
  }).then(res => {
    couponsTable.loading = false
    couponsTable.data = res.data.data
    couponsTable.total = res.data.total
  }).catch(() => {
    couponsTable.loading = false
  })
}
loadCouponsList()

const editCouponsDialog: Record<string, any> | null = ref(null)

/**
 * 添加优惠券
 */
const addEvent = () => {
  editCouponsDialog.value.setFormData()
  editCouponsDialog.value.showDialog = true
}

/**
 * 编辑优惠券
 * @param data
 */
const editEvent = (data: any) => {
  editCouponsDialog.value.setFormData(data)
  editCouponsDialog.value.showDialog = true
}

/**
 * 删除优惠券
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t('couponsDeleteTips'), t('warning'),
      {
        confirmButtonText: t('confirm'),
        cancelButtonText: t('cancel'),
        type: 'warning',
      }
  ).then(() => {
    deleteCoupons(id).then(() => {
      loadCouponsList()
    }).catch(() => {
    })
  })
}


const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  loadCouponsList()
}
</script>

<style lang="scss" scoped>
/* 多行超出隐藏 */
.multi-hidden {
  word-break: break-all;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
