<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="互动视频名称" prop="interactionName">
        <el-input
          v-model="queryParams.interactionName"
          placeholder="请输入互动视频名称"
          clearable
          style="width: 240px"
          @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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
    </el-row>
    <!-- 列表 -->
    <el-table
      border
      v-loading="loading"
      :data="videoList"
      @selection-change="handleSelectionChange"
      @sort-change="sortChange"
    >
      <el-table-column type="selection" width="50" />
      <el-table-column type="index" label="序号" align="center" width="80" />
      <el-table-column prop="interactionName" label="视频名称" align="center" />
      <el-table-column
        prop="createTime"
        label="创建时间"
        align="center"
        sortable="custom"
      />
      <el-table-column prop="remark" label="备注" align="center" />
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="primary" icon="Edit" link @click="handleUpdate(row)"
            >编辑</el-button
          >
          <el-button type="danger" link @click="handleDelete(row)"
            >删除</el-button
          >
          <el-button type="primary" link @click="handleView(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"
    />
    <!-- 新增编辑 -->
    <FormFields :open="materialOpen" @close="formFieldsClose" />
    <!-- 视频预览 -->
    <el-dialog
      title="视频预览"
      v-model="videoViewOpen"
      width="1200"
      destroy-on-close
    >
      <XGVideoViewer
        :question="videoDetail.data.question"
        :options="videoDetail.data.optionsList"
        :videoUrl="videoDetail.data.uri"
        :autoplay="true"
        :autoplayMuted="false"
        :poster="videoDetail.data.image"
        @onClickOption="onClickOption"
      />
    </el-dialog>
  </div>
</template>

<script setup name="VideoManage">
import {
  getInteractionVideoList,
  interactionVideoDel,
  getInteractionVideoDetail,
} from "@/api/interactVideo/videoManage";
import { transfromReturnGraphdata } from "@/hooks/transformGraphdata";
import FormFields from "./formFields.vue";
import XGVideoViewer from "@/components/XGVideoViewer";

const { proxy } = getCurrentInstance();
const router = useRouter();
const loading = ref(false);
const videoList = ref([]);
const total = ref(0);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const materialOpen = ref(false);
const data = reactive({
  queryParams: {
    orderByColumn: "create_time",
    isAsc: "desc",
    pageNum: 1,
    pageSize: 10,
    interactionName: undefined,
  },
});
const { queryParams } = toRefs(data);
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.interactionId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
getList();
/** 查询视频列表 */
function getList() {
  loading.value = true;
  getInteractionVideoList(queryParams.value)
    .then((response) => {
      videoList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    })
    .finally(() => {
      loading.value = false;
    });
}

const sortableKeyMap = {
  descending: "desc",
  ascending: "asc",
};

// 排序
function sortChange(sortData) {
  // console.log(sortData);
  queryParams.value.isAsc = sortableKeyMap[sortData.order];
  getList();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const interactionIds = row.interactionId || ids.value;
  proxy.$modal
    .confirm('是否确认删除互动视频名称为"' + row.interactionName + '"的数据项?')
    .then(function () {
      return interactionVideoDel(interactionIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

// 视频预览
const videoViewOpen = ref(false);
const videoDetail = ref({});
const graphData = ref({
  id: "0",
  type: "root-node",
  preNodeId: null,
  children: [],
  data: {
    name: "默认节点",
    image: "",
    url: "",
    imageId: "",
    videoId: "",
    question: "",
    optionsList: [],
  },
});

/** 预览按钮操作 */
function handleView(row) {
  console.log(row);
  // 获取互动视频基本信息以及节点
  getInteractionVideoDetail(row.interactionId).then((res) => {
    if (res.code == 200) {
      if (!res.data.rootNode)
        return proxy.$modal.msgError("暂无可播放视频，可添加视频素材后预览");
      graphData.value = transfromReturnGraphdata(res.data.rootNode);
      videoDetail.value = graphData.value;
      if (!videoDetail.value.data.uri) {
        proxy.$modal.msgError("暂无可播放视频，可添加视频素材后预览");
        return;
      }
      videoViewOpen.value = true;
    }
  });
}

// 点击选中选项
const onClickOption = (option) => {
  videoDetail.value = graphData.value.children.filter(
    (i) => i.id == option.nodeId
  )[0];
};

function handleAdd() {
  // router.push("/interactVideo/videoOperation");
  materialOpen.value = true;
}
function handleUpdate(row) {
  const interactionId = row.interactionId || ids.value[0];
  router.push({
    path: "/interactVideo/videoOperation",
    query: { interactionId },
  });
}

function formFieldsClose() {
  materialOpen.value = false;
  getList();
}
</script>
