<template>
  <el-drawer v-model="drawer" title="媒体库" direction="ltr" :size="props.size">
    <template #header>
      <div class="flex justify-between items-center">
        <span class="text-lg">媒体库</span>
      </div>
    </template>

    <div class="gva-btn-list">
      <upload-common :image-common="imageCommon" class="upload-btn-media-library" :category="props.category"
        @on-success="open" />
      <el-tooltip effect="dark" content="图片超过 512K 或者 长宽 > 1080 会自动压缩后再上传" placement="top-start">
        <upload-image :image-url="imageUrl" :file-size="512" :max-w-h="1080" :category="props.category"
          @on-success="open" />
      </el-tooltip>
      <el-input v-model="search.keyword" class="keyword" placeholder="请输入文件名或备注" clearable />
      <el-button type="primary" plain icon="search" @click="open">查询</el-button>
    </div>

    <warning-bar title="点击“文件名/备注”可以编辑文件名或者备注内容。" />
    <div class="media">
      <div v-for="(item, key) in picList" :key="key" class="media-box">
        <div class="img-box-list">
          <el-image :key="key" fit="cover" :src="getUrl(item.url)" style="width: 138px; height: 138px;"
            @click="chooseImg(item)">
            <template #error>
              <div class="img-box-list">
                <el-icon>
                  <picture />
                </el-icon>
              </div>
            </template>
          </el-image>
        </div>
        <div class="img-title" @click="editFileNameFunc(item)">{{ item.name }}</div>
      </div>
    </div>
    <el-pagination :current-page="page" :page-size="pageSize" :total="total" :style="{ 'justify-content': 'center' }"
      layout="total, prev, pager, next, jumper" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
  </el-drawer>
</template>

<script setup>
import { getUrl } from '@/utils/image'
import { ref } from 'vue'
import { getFileList, editFileName } from '@/api/mediaFile'
import UploadImage from '@/components/upload/image.vue'
import UploadCommon from '@/components/upload/common.vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import WarningBar from '@/components/warningBar/warningBar.vue'

const imageUrl = ref('')
const imageCommon = ref('')

const search = ref({})
const page = ref(1)
const total = ref(0)
const pageSize = ref(20)

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  open()
}

const handleCurrentChange = (val) => {
  page.value = val
  open()
}

const emit = defineEmits(['on-select', 'on-before-upload', 'on-upload-success', 'on-upload-failure'])
const props = defineProps({
  category: { type: String, required: true },
  size: { type: String, default: '800px' },
})

const drawer = ref(false)
const picList = ref([])

const chooseImg = (item) => {
  emit('on-select', item)
  drawer.value = false
}

const open = async () => {
  search.value.category = props.category
  const res = await getFileList({ page: page.value, pageSize: pageSize.value, ...search.value })
  if (res.code === 0) {
    picList.value = res.data.list
    total.value = res.data.total
    page.value = res.data.page
    pageSize.value = res.data.pageSize
    drawer.value = true
  }
}

/**
 * 编辑文件名或者备注
 * @param row
 * @returns {Promise<void>}
 */
const editFileNameFunc = async (row) => {
  ElMessageBox.prompt('请输入文件名或者备注', '编辑', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    inputPattern: /\S/,
    inputErrorMessage: '不能为空',
    inputValue: row.name
  }).then(async ({ value }) => {
    row.name = value
    // console.log(row)
    const res = await editFileName(row)
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: '编辑成功!',
      })
      open()
    }
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '取消修改'
    })
  })
}

defineExpose({ open })
</script>
<style lang="scss">
.media {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;

  .media-box {
    width: 148px;
    background: #F0F2F5;
    padding: 5px;
    text-align: center;
    border-radius: 5px;
    box-sizing: border-box;

    .img-title {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      line-height: 28px;
      text-align: center;
      cursor: pointer;
      font-size: 12px;
      box-sizing: border-box;
    }
  }
}
</style>
