<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="imagedataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据源标题" align="center" prop="title">
        <template #default="scope">
          {{ scope.row.dataSource.title }}
        </template>
      </el-table-column>
      <el-table-column
        label="预览缩略图"
        align="center"
        width="180"
        prop="thumbnailUrl"
      >
        <template #default="scope">
          <QImgPreview :src="scope.row.thumbnailUrl" />
        </template>
      </el-table-column>
      <el-table-column label="预览图片" align="center" width="180" prop="url">
        <template #default="scope">
          <QImgPreview :src="scope.row.url" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleUpdate(scope.row)"
            >管理</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </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="dialogTitle" v-model="open" width="500px" append-to-body>
      <el-form ref="imgdataRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="datasourcetitle" :disabled="true" />
        </el-form-item>
        <el-form-item label="图片" prop="path">
          <QImgUpload v-model="form.path" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Imgdata">
import {
  listImgData,
  getImgData,
  delImgData,
  addImgData,
  updateImgData,
} from '@/api/VRdemo/img'

const { proxy } = getCurrentInstance()

const imagedataList = ref([])
const open = ref(false)
const loading = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const dialogTitle = ref('')
const datasourcetitle = ref('')
const add = ref(false)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dataSourceId: null,
    path: null,
  },
})
const { queryParams, form, rules } = toRefs(data)

/** 查询图片数据编辑列表 */
function getList() {
  loading.value = true
  listImgData(queryParams.value).then((response) => {
    imagedataList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    imageDataId: null,
    companyId: null,
    dataSourceId: null,
    storageId: null,
    size: null,
    path: null,
    url: null,
    createTime: null,
    updateTime: null,
  }
  proxy.resetForm('imagedataRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.imageDataId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  add.value = true
  title.value = '添加图片数据编辑'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _imageDataId = row.imageDataId || ids.value
  datasourcetitle.value = row.dataSource.title
  getImgData(_imageDataId).then((response) => {
    form.value = response.data
    open.value = true
    dialogTitle.value = '修改图片数据编辑'
  })
}

/** 提交按钮 */
function submitForm() {
  updateImgData(form.value).then((response) => {
    proxy.$modal.msgSuccess('修改成功')
    open.value = false
    getList()
  })
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _imageDataIds = row.imageDataId || ids.value
  proxy.$modal
    .confirm('是否确认删除图片数据编辑编号为"' + _imageDataIds + '"的数据项？')
    .then(function () {
      return delImgData(_imageDataIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'imagedata/imagedata/export',
    {
      ...queryParams.value,
    },
    `imagedata_${new Date().getTime()}.xlsx`
  )
}

getList()
</script>
