<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParam" :inline="true">
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="queryParam.mobile" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="视频id" prop="mobile">
            <el-input v-model="queryParam.vlogId" placeholder="请输入视频id" 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>
      </div>
    </transition>

    <el-card shadow="never">
      <el-table
        ref="categoryTableRef"
        v-loading="loading"
        :data="lomglist"
        row-key="categoryId"
        border
        :default-expand-all="isExpandAll"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column label="视频作者" prop="vlogerMobile" align="center" width="100" />
        <el-table-column label="评论id" prop="commentId" align="center" width="180" />
        <el-table-column label="评论状态" prop="status" align="center" width="100">
          <template #default="{ row }">
            {{ row.status === 0 ? '正常' : row.status === 1 ? '删除' : '违规' }}
          </template>
        </el-table-column>
        <el-table-column label="评论内容" prop="content" align="center" width="100" />
        <el-table-column label="评论条数" prop="childCount" align="center" width="100">
          <template #default="{ row }">
            <el-tooltip :content="row.childCount" placement="top">
              <el-button link type="primary" @click="handleAdd(row)" v-hasPermi="['workflow:video:add']">{{ row.childCount }}</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="时间" prop="createTime" align="center" width="100" />
        <!-- <el-table-column label="粉丝数量" prop="fansCounts" align="center" width="100" /> -->
        <el-table-column label="评论者头像" align="center" width="200">
          <template #default="{ row }">
            <img alt="评论者头像" style="max-width: 50%; max-height: 50%" :src="row.commentUserFace" />
          </template>
        </el-table-column>
        <el-table-column label="视频" align="center" width="200">
          <template #default="{ row }">
            <!-- 假设 row 中有一个 videoUrl 属性存储视频地址 -->
            <video controls width="40%">
              <source :src="row.vlogUrl" type="video/mp4" />
              您的浏览器不支持播放该视频。
            </video>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="查看" placement="top">
              <el-button link type="primary" @click="handleAdd(scope.row)" v-hasPermi="['workflow:video:add']">查看</el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['workflow:video:remove']">删除</el-button>
            </el-tooltip>
            <el-tooltip content="下架" placement="top">
              <el-button link type="primary" @click="handledeloffline(scope.row)" v-hasPermi="['workflow:video:remove']" v-if="scope.row.status == 0"
                >下架</el-button
              >
              <el-button link type="primary" @click="handledeloffline(scope.row)" v-hasPermi="['workflow:video:remove']" v-if="scope.row.status == 2"
                >上架</el-button
              >
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <pagination v-if="total > 0" :total="total" v-model:page="queryParam.current" v-model:limit="queryParam.size" @pagination="getList" />
      </div>
    </el-card>
    <!-- 二级评论 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="90%" append-to-body>
      <div class="video-container">
        <el-table
          ref="categoryTableRef"
          :data="lomgChildlist"
          row-key="categoryId"
          border
          :default-expand-all="isExpandAll"
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column label="评论名称" prop="replyUserNickname" align="center" width="100" />
          <el-table-column label="评论内容" prop="content" align="center" width="100" />
          <el-table-column label="点赞数量" prop="likeCounts" align="center" width="100" />
          <el-table-column label="评论状态" prop="status" align="center" width="100">
            <template #default="{ row }">
              {{ row.status === 0 ? '正常' : row.status === 1 ? '删除' : '违规' }}
            </template>
          </el-table-column>
          <el-table-column label="评论者头像" align="center" width="200">
            <template #default="{ row }">
              <img alt="评论者头像" style="max-width: 20%; max-height: 20%" :src="row.replyUserFace" />
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
              <!-- <el-tooltip content="查看" placement="top">
                <el-button link type="primary" @click="handleAdd(scope.row)" v-hasPermi="['workflow:video:add']">查看</el-button>
              </el-tooltip> -->
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['workflow:video:remove']">删除</el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <pagination
            v-if="totlelChild > 0"
            :total="totlelChild"
            v-model:page="queryChild.current"
            v-model:limit="queryChild.size"
            @pagination="getChildList"
          />
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Category" lang="ts">
import { getcommentlist, getchildList, deldetail, deloffline } from '@/api/workflow/category';
import { QueryParamOV, LomglistVO, QueryChildOV, LomgChildOV } from '@/api/contentCenter/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const lomglist = ref<LomglistVO[]>([]);
const showSearch = ref(true);
const isExpandAll = ref(true);
const loading = ref(false);
const queryFormRef = ref<ElFormInstance>();
const categoryTableRef = ref<ElTableInstance>();
const total = ref<number>(0);

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const queryParam = ref<QueryParamOV>({
  current: 1,
  size: 10,
  vlogId: undefined,
  mobile: undefined
});

/** 查询流程分类列表 */
const getList = async () => {
  loading.value = true;
  const res = await getcommentlist(queryParam.value);
  if (res.code == 200) {
    lomglist.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  }
};

const handleCurrentChange = (val: number) => {
  queryParam.value.current = val;
  getList();
};
// 取消按钮
const cancel = () => {
  dialog.visible = false;
};

// 表单重置
// const reset = () => {
//   form.value = { ...initFormData };
//   categoryFormRef.value?.resetFields();
// };

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParam.value.current = 1; // 查询时重置为第一页
  console.log(queryParam.value);
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
const queryChild = ref<QueryChildOV>({
  current: 1,
  size: 10,
  fatherCommentId: undefined
});
const lomgChildlist = ref<LomgChildOV[]>([]);
const totlelChild = ref<number>(0);
/** 查看详情按钮操作 */
const handleAdd = async (row?: LomglistVO) => {
  dialog.title = '查看详情';
  dialog.visible = true;
  queryChild.value.fatherCommentId = row?.id;
  getChildList();
};
const getChildList = async () => {
  const res = await getchildList(queryChild.value);
  console.log(res);
  if (res.code == 200) {
    lomgChildlist.value = res.data.records;
    totlelChild.value = res.data.total;
    // loading.value = false;
  }
};
const handleDelete = async (row?: LomglistVO) => {
  ElMessageBox.confirm('是否确认删除', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const res = await deldetail(row.id);
    if (res.code == 200) {
      getList();
      dialog.visible = false;
      ElMessage({
        message: '删除成功',
        type: 'success'
      });
      getList();
    } else {
      ElMessage({
        message: res.msg,
        type: 'error'
      });
    }
  });
};
const handledeloffline = async (row?: LomglistVO) => {
  ElMessageBox.confirm('是否确认上下架', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const res = await deloffline(row.id);
    if (res.code == 200) {
      getList();
      ElMessage({
        message: res.msg,
        type: 'success'
      });
    }
  });
};
onMounted(() => {
  getList();
});
</script>
<style scoped lang="scss">
.video-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.video-container .video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.video-info {
  width: 100%;
  display: flex;
  justify-content: space-around;
  margin-top: 10px;
  font-size: 20px;
  font-weight: bold;
  & > div {
    cursor: pointer;
  }
}
.comment-list {
  width: 100%;
}
.comment-item {
  display: flex;
  width: 100%;
  .comment-avatar {
    padding: 10px;
  }
  .comment-content {
    padding: 10px;
    width: 100%;
    .comment-header {
      width: 100%;
      display: flex;
      justify-content: space-between;
      .comment-name {
        color: rgb(145, 145, 145);
      }
    }

    .comment-text {
      display: flex;
      width: 100%;
      justify-content: space-between;
      .el-button {
        width: 50px;
        height: 20px;
      }
    }
  }
}
</style>
