<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="展厅图标" prop="icon">
                <el-input v-model="queryParams.icon" placeholder="请输入展厅图标" clearable @keyup.enter="handleQuery" />
            </el-form-item> -->
      <!-- <el-form-item label="缩略图url" prop="cover">
                <el-input v-model="queryParams.cover" placeholder="请输入缩略图url" clearable @keyup.enter="handleQuery" />
            </el-form-item> -->
      <!-- <el-form-item label="视频url" prop="video">
                <el-input v-model="queryParams.video" placeholder="请输入视频url" clearable @keyup.enter="handleQuery" />
            </el-form-item> -->
      <!-- <el-form-item label="展厅浏览量" prop="viewCount">
                <el-input
                    v-model="queryParams.viewCount"
                    placeholder="请输入展厅浏览量"
                    clearable
                    @keyup.enter="handleQuery"
                />
            </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          @change="handleQuery"
          style="width: 120px;"
        >
          <el-option label="不展示" :value="-1" />
          <el-option label="审核中" :value="0" />
          <el-option label="已展示" :value="1" />
          <el-option label="已打回" :value="2" />
        </el-select>
      </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-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['vrone:app:add']"
                    >新增</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="success"
                    plain
                    icon="Edit"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['vrone:app:edit']"
                    >修改</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button
                    type="danger"
                    plain
                    icon="Delete"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['vrone:app:remove']"
                    >删除</el-button
                >
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['vrone:app:export']"
                    >导出</el-button
                >
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->

    <el-table
      v-loading="loading"
      :data="appList"
      @selection-change="handleSelectionChange"
    >
      <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="title" />
      <el-table-column label="展厅图标" align="center" prop="icon" />
      <el-table-column label="缩略图" align="center" prop="cover">
        <template #default="scope">
          <el-image
            style="width: 60px; height: 60px"
            :src="scope.row.cover"
            fit="cover"
            preview-src-list="[scope.row.cover]"
          />
        </template>
      </el-table-column>
      <el-table-column label="视频" align="center" prop="video" />
      <el-table-column label="展厅浏览量" align="center" prop="viewCount" />
      <el-table-column label="简介" align="center" prop="introduction" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          {{ getStatusText(scope.row.status) }}
        </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="Edit"
            @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="title" v-model="open" width="500px" append-to-body>
      <el-form ref="appRef" :model="form" :rules="rules" 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="title">
                    <el-input v-model="form.title" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="展厅图标" prop="icon">
                    <el-input v-model="form.icon" placeholder="请输入展厅图标" />
                </el-form-item>
                <el-form-item label="缩略图url" prop="cover">
                    <el-input v-model="form.cover" placeholder="请输入缩略图url" />
                </el-form-item>
                <el-form-item label="视频url" prop="video">
                    <el-input v-model="form.video" placeholder="请输入视频url" />
                </el-form-item>
                <el-form-item label="展厅浏览量" prop="viewCount">
                    <el-input v-model="form.viewCount" placeholder="请输入展厅浏览量" />
                </el-form-item>
                <el-form-item label="简介" prop="introduction">
                    <el-input v-model="form.introduction" placeholder="请输入简介" />
                </el-form-item> -->
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="不展示" :value="-1" />
            <el-option label="审核中" :value="0" />
            <el-option label="已展示" :value="1" />
            <el-option label="已打回" :value="2" />
          </el-select>
        </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="App">
import { listApp, getApp, delApp, addApp, updateApp } from "@/api/vrone/app";

const { proxy } = getCurrentInstance();

const appList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    phone: null,
    title: null,
    icon: null,
    cover: null,
    video: null,
    viewCount: null,
    introduction: null,
    status: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询展厅列表 */
function getList() {
  loading.value = true;
  listApp(queryParams.value).then((response) => {
    appList.value = response.data;
    total.value = response.total;
    loading.value = false;
  });
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    title: null,
    icon: null,
    cover: null,
    video: null,
    viewCount: null,
    introduction: null,
    status: null,
    createTime: null,
    updateTime: null,
  };
  proxy.resetForm("appRef");
}

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

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

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加展厅";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getApp(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改展厅";
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const appIds = row?.id ? [row.id] : ids.value;
  proxy.$modal
    .confirm("是否确认删除选中的展厅？")
    .then(() => {
      return delApp(appIds.join(","));
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["appRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateApp(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addApp(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 修改状态  */
function getStatusText(status) {
  switch (status) {
    case -1:
      return "不展示";
    case 0:
      return "审核中";
    case 1:
      return "已展示";
    case 2:
      return "已打回";
    default:
      return "未知";
  }
}

getList();
</script>
