<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="80px">
      <el-form-item label="所屬用戶" prop="username">
        <el-input
            v-model="queryParams.username"
            placeholder="請輸入所屬用戶"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜尋</el-button>
        <el-button icon="Refresh" @click="resetQuery">重設</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="imageList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="用戶id" align="center" prop="userId" />
      <el-table-column label="所屬用戶" align="center" prop="userName" />
      <el-table-column label="圖片" align="center" prop="url" >
        <template #default="{ row }">
          <image-preview :src="getImageUrl(row.url)" :width="80" :height="80"/>
        </template>
      </el-table-column>
      <el-table-column label="圖片時間" align="center" prop="createDate" />
      <el-table-column label="操作" align="center" width="140" class-name="small-padding fixed-width">
        <template #default="{ row }">
          <el-tooltip content="查看" placement="top">
            <el-button link type="primary" icon="View" @click.stop="handleView(row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="formRef" :model="form"  label-width="80px">
        <el-form-item label="用戶id" prop="userId">
          <el-input v-model="form.userId" placeholder="請輸入用戶id" />
        </el-form-item>
        <el-form-item label="所屬用戶" prop="userName">
          <el-input v-model="form.userName" />
        </el-form-item>
        <el-form-item label="圖片" prop="url">
          <image-preview v-if="form.url" :src="getImageUrl(form.url)" :width="100" :height="100"/>
          <image-upload :limit="1" v-model="form.url"/>
        </el-form-item>
        <el-form-item label="圖片時間" prop="createDate">
          <el-input v-model="form.createDate" placeholder="請輸入圖片時間" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {ref, reactive, getCurrentInstance, onMounted, nextTick} from 'vue'
import {getImagePageList} from '@/api/system/image.js'
import { isExternal } from '@/utils/validate'

// 获取文件路径前缀
const filePath = import.meta.env.VITE_APP_FILE_PATH || ''

const imageList = ref([])
const total = ref(0)
const title = ref("")
const open = ref(false)
const form = ref({})
const formRef = ref()
const loading = ref(true)
const queryFormRef = ref()
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  username: null,
})

// 获取完整的图片URL
const getImageUrl = (url) => {
  if (!url) return ''
  // 如果是外部链接，直接返回
  if (isExternal(url)) {
    return url
  }
  // 如果已经包含文件路径前缀，直接返回
  if (filePath && url.startsWith(filePath)) {
    return url
  }
  // 在前面加上文件路径前缀
  return filePath ? filePath + url : url
}

onMounted(()=>{
  getList()
})
// 表单重设
const reset = () => {
  form.value = {
    id: null,
    url: null,
    createDate: null,
    userId: null
  }
  if (formRef.value) {
    formRef.value.resetFields()
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
const getList = () => {
  loading.value = true
  const params = {
    pageNum: queryParams.pageNum,
    pageSize: queryParams.pageSize,
    userName: queryParams.username,
  }
  getImagePageList(params).then(response => {
    imageList.value = response.data.records
    total.value = response.data.total
    loading.value = false
  })
}

/** 查看按钮操作 */
const handleView = (row) => {
  reset()
  form.value = {...row}
  open.value = true
  title.value = "查看圖片資訊"
  nextTick(() => {
    if (formRef.value) formRef.value.clearValidate && formRef.value.clearValidate()
  })
}
// 取消按钮
const cancel = () => {
  open.value = false
  reset()
}
</script>
<style scoped lang="scss">

</style>