<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="参数名称" prop="sysAttrName">
        <el-input
          v-model="queryParams.sysAttrName"
          placeholder="请输入参数名称"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数键名" prop="sysAttrKey">
        <el-input
          v-model="queryParams.sysAttrKey"
          placeholder="请输入参数键名"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数键值" prop="sysAttrValue">
        <el-input
          v-model="queryParams.sysAttrValue"
          placeholder="请输入参数键值"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['iscs:attribute:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="danger"
          plain
          @click="handleRefreshCache"
          v-hasPermi="['iscs:attribute:refresh']"
        >
          <Icon icon="ep:refresh" class="mr-5px" /> 刷新缓存
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="参数名称" width="210px" prop="sysAttrName" />
      <el-table-column label="参数键名" prop="sysAttrKey" :show-overflow-tooltip="true" />
      <el-table-column label="参数类型" width="90" prop="sysAttrType" :show-overflow-tooltip="true">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYS_TYPE" :value="scope.row.sysAttrType + ''" />
        </template>
      </el-table-column>
      <el-table-column label="参数键值" prop="sysAttrValue" :show-overflow-tooltip="true">
        <template #default="scope">
          <div class="img-box" v-if="isImageUrl(scope.row.sysAttrValue)">
            <el-image
              style="width: 50px; height: 50px"
              :preview-teleported="true"
              class="images"
              :hide-on-click-modal="true"
              :src="scope.row.sysAttrValue"
              :zoom-rate="1.2"
              :preview-src-list="[scope.row.sysAttrValue]"
              :initial-index="1"
            />
            <Icon icon="ep:zoom-in" class="eye-icon" />
          </div>
          <div v-else>{{ scope.row.sysAttrValue }}</div>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" width="300" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['iscs:attribute:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['iscs:attribute:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ConfigForm ref="formRef" @success="getList" />
</template>

<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import * as ConfigApi from '@/api/basic/configuration'
import ConfigForm from './ConfigForm.vue'


defineOptions({ name: 'SystemConfig' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const ids = ref<number[]>([]) // 选中的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  sysAttrName: undefined,
  sysAttrKey: undefined,
  sysAttrValue: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询配置列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ConfigApi.getIsSystemAttributePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    await message.delConfirm()
    await ConfigApi.deleteIsSystemAttributeByIds(id)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

/** 刷新缓存操作 */
const handleRefreshCache = async () => {
  try {
    await ConfigApi.refreshAttrCache()
    message.success('刷新成功')
  } catch {}
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map(item => item.sysAttrId)
}

/** 判断是否为图片URL */
const isImageUrl = (url: string) => {
  if (typeof url !== 'string') return false
  return /\.(jpg|jpeg|png|gif|webp|bmp)$/i.test(url)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.img-box {
  width: 50px;
  height: 50px;
  position: relative;

  .eye-icon {
    display: none;
  }

  &:hover {
    background: #000;

    .images {
      opacity: 0.6;
    }

    .eye-icon {
      display: block;
      position: absolute;
      top: 40%;
      left: 30%;
      z-index: 100;
      color: white;
      pointer-events: none;
    }
  }
}
</style>
