<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="mb-[10px]">
        <el-card shadow="hover" :style="{ marginTop: '10px', height: '60px' }">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="50px">
            <el-form-item label="名称" prop="name">
              <el-input v-model="queryParams.name" style="width: 150px" placeholder="请输入名称" clearable
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="类型" prop="itemType">
              <el-select v-model="queryParams.itemType" style="width: 150px" placeholder="请选择类型" clearable>
                <el-option v-for="dict in smsb_push_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="时间" style="width: 250px">
              <el-date-picker v-model="dateRangeCreateTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
                range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]" />
            </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-card>
      </div>
    </transition>
    <el-tabs type="border-card" v-model="activeName" @tab-click="handleClickTab">
      <el-tab-pane label="内容审核" name="need">
        <div class="table-content">
          <el-table v-loading="loading" :data="itemPushList">
            <el-table-column label="ID" align="left" prop="id" width="200" />
            <el-table-column label="名称" align="left" prop="name" />
            <el-table-column label="优先级" align="center" width="100" prop="level">
              <template #default="scope">
                <dict-tag :options="smsb_push_level" :value="scope.row.level" />
              </template>
            </el-table-column>
            <el-table-column label="类型" align="center" width="100" prop="itemType">
              <template #default="scope">
                <dict-tag :options="smsb_push_type" :value="scope.row.itemType" />
              </template>
            </el-table-column>
            <el-table-column label="发布状态" align="center" width="100" prop="status">
              <template #default="scope">
                <dict-tag :options="smsb_push_state" :value="scope.row.status" />
              </template>
            </el-table-column>
            <el-table-column label="创建时间" align="left" prop="createTime" width="180" />
            <el-table-column label="创建人" align="left" prop="createUser" width="180" :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
              <template #default="scope">
                <el-tooltip content="内容审核" placement="top">
                  <el-button link type="primary" icon="Notification" @click="handleApproval(scope.row)"
                    v-hasPermi="['source:itemPush:edit']"></el-button>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-tab-pane>
      <el-tab-pane label="我的已办" name="finish">
        <el-table v-loading="loading" :data="itemPushList">
          <el-table-column label="ID" align="left" prop="id" width="200" />
          <el-table-column label="名称" align="left" prop="name" />
          <el-table-column label="优先级" align="center" width="100" prop="level">
            <template #default="scope">
              <dict-tag :options="smsb_push_level" :value="scope.row.level" />
            </template>
          </el-table-column>
          <el-table-column label="类型" align="center" width="100" prop="itemType">
            <template #default="scope">
              <dict-tag :options="smsb_push_type" :value="scope.row.itemType" />
            </template>
          </el-table-column>
          <el-table-column label="发布状态" align="center" width="100" prop="status">
            <template #default="scope">
              <dict-tag :options="smsb_push_state" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="left" prop="createTime" width="180" />
          <el-table-column label="创建人" align="left" prop="createUser" width="180" :show-overflow-tooltip="true" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
            <template #default="scope">
              <el-tooltip content="审核详情" placement="top">
                <el-button link type="primary" icon="View" @click="handleReview(scope.row)"
                  v-hasPermi="['source:itemPush:edit']"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize" @pagination="getFinishList" />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script setup lang="ts">
import { ItemPushForm, ItemPushQuery, ItemPushVO } from '@/api/smsb/source/item_push_type';
import { listNeedReview } from '@/api/smsb/source/item_push';
import type { TabsPaneContext } from 'element-plus';

const queryFormRef = ref<ElFormInstance>();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { smsb_push_state, smsb_push_type, smsb_push_level } = toRefs<any>(proxy?.useDict('smsb_push_state', 'smsb_push_type', 'smsb_push_level'));
const showSearch = ref(true);
const itemPushList = ref<ItemPushVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const activeName = ref('need');
const total = ref(0);
const dateRangeCreateTime = ref<[DateModelType, DateModelType]>(['', '']);
const initFormData: ItemPushForm = {
  name: undefined,
  level: undefined,
  itemType: undefined,
  deviceIds: undefined,
  itemIds: undefined,
  dateRange: undefined,
  weekDays: ['1', '2', '3', '4', '5', '6', '7'],
  timeRange: []
};
const data = reactive<PageData<ItemPushForm, ItemPushQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    level: undefined,
    itemType: undefined,
    status: undefined,
    params: {
      createTime: undefined
    }
  },
  rules: {}
});
const { queryParams, form, rules } = toRefs(data);

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 查询内容发布列表 */
const getList = async () => {
  loading.value = true;
  queryParams.value.params = {};
  queryParams.value.status = 'waiting';
  proxy?.addDateRange(queryParams.value, dateRangeCreateTime.value, 'CreateTime');
  const res = await listNeedReview(queryParams.value);
  itemPushList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
const getFinishList = async () => {
  loading.value = true;
  queryParams.value.params = {};
  queryParams.value.status = 'finished';
  proxy?.addDateRange(queryParams.value, dateRangeCreateTime.value, 'CreateTime');
  const res = await listNeedReview(queryParams.value);
  itemPushList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
const resetQuery = () => {
  dateRangeCreateTime.value = ['', ''];
  queryFormRef.value?.resetFields();
  handleQuery();
};
const handleReview = (row?: ItemPushVO) => {
  const id = row.id;
  const taskId = row.taskId;
  const status = row.status;
  // 跳转页面 /source/push/review?id=1898991996092481537&type=approval&taskId=1898992031169445891
  proxy.$router.push('/source/push/review?id=' + id + '&type=view&taskId=' + taskId);
};
const handleApproval = (row?: ItemPushVO) => {
  const id = row.id;
  const taskId = row.taskId;
  const status = row.status;
  // 跳转页面 /source/push/review?id=1898991996092481537&type=approval&taskId=1898992031169445891
  proxy.$router.push('/source/push/review?id=' + id + '&type=approval&taskId=' + taskId);
};
const handleClickTab = (tab: TabsPaneContext, event: Event) => {
  if (tab.props.name === 'need') {
    getList();
  } else if (tab.props.name === 'finish') {
    getFinishList();
  }
};

onMounted(() => {
  getList();
});
</script>
