<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('addAdvertising') }}
        </el-button>
      </div>

      <el-card class="box-card !border-none my-[10px] table-search-wrap" shadow="never">
        <el-form :inline="true" :model="advertisingTable.searchParam" ref="searchFormRef">

          <el-form-item>
            <el-button type="primary" @click="loadAdvertisingList()">{{ 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="advertisingTable.data" size="large" v-loading="advertisingTable.loading">
          <template #empty>
            <span>{{ !advertisingTable.loading ? t('emptyData') : '' }}</span>
          </template>
          <el-table-column prop="id" :label="t('id')" min-width="120" :show-overflow-tooltip="true"/>

          <el-table-column :label="t('imgUrl')" width="100" align="left">
            <template #default="{ row }">
              <el-avatar v-if="row.img_url" :src="img(row.img_url)"/>
              <el-avatar v-else icon="UserFilled"/>
            </template>
          </el-table-column>
          <el-table-column prop="skip_url" :label="t('skipUrl')" min-width="120" :show-overflow-tooltip="true"/>

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

          <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="advertisingTable.page" v-model:page-size="advertisingTable.limit"
                         layout="total, sizes, prev, pager, next, jumper" :total="advertisingTable.total"
                         @size-change="loadAdvertisingList()" @current-change="loadAdvertisingList"/>
        </div>
      </div>

      <edit ref="editAdvertisingDialog" @complete="loadAdvertisingList"/>
    </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 {getAdvertisingList, deleteAdvertising,} from '@/addon/hygl/api/advertising'
import {img} from '@/utils/common'
import {ElMessageBox, FormInstance} from 'element-plus'
import Edit from '@/addon/hygl/views/advertising/components/advertising-edit.vue'
import {useRoute} from 'vue-router'

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

let advertisingTable = reactive({
  page: 1,
  limit: 10,
  total: 0,
  loading: true,
  data: [],
  searchParam: {}
})

const searchFormRef = ref<FormInstance>()

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

// 字典数据


/**
 * 获取广告管理列表
 */
const loadAdvertisingList = (page: number = 1) => {
  advertisingTable.loading = true
  advertisingTable.page = page

  getAdvertisingList({
    page: advertisingTable.page,
    limit: advertisingTable.limit,
    ...advertisingTable.searchParam
  }).then(res => {
    advertisingTable.loading = false
    advertisingTable.data = res.data.data
    advertisingTable.total = res.data.total
  }).catch(() => {
    advertisingTable.loading = false
  })
}
loadAdvertisingList()

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

/**
 * 添加广告管理
 */
const addEvent = () => {
  editAdvertisingDialog.value.setFormData()
  editAdvertisingDialog.value.showDialog = true
}

/**
 * 编辑广告管理
 * @param data
 */
const editEvent = (data: any) => {
  editAdvertisingDialog.value.setFormData(data)
  editAdvertisingDialog.value.showDialog = true
}

/**
 * 删除广告管理
 */
const deleteEvent = (id: number) => {
  ElMessageBox.confirm(t('advertisingDeleteTips'), t('warning'),
      {
        confirmButtonText: t('confirm'),
        cancelButtonText: t('cancel'),
        type: 'warning',
      }
  ).then(() => {
    deleteAdvertising(id).then(() => {
      loadAdvertisingList()
    }).catch(() => {
    })
  })
}


const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  loadAdvertisingList()
}
</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>
