<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>我的抄送</span>
        </div>
      </template>

      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="流程名称">
          <el-input v-model="queryParams.processName" placeholder="请输入流程名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="tableData" border>
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="definitionName" label="流程名称" />
        <el-table-column prop="nodeName" label="抄送节点" />
        <el-table-column prop="instanceTitle" label="流程标题" />
        <el-table-column label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="row.taskStatus === 0 ? 'danger' : 'info'">
              {{ row.taskStatus === 0 ? '未读' : '已读' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="抄送时间" />
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleView(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :total="total"
        :page-sizes="[10, 20, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        class="pagination"
        @current-change="loadData"
        @size-change="loadData"
      />
    </el-card>

    <InstanceDetail
      v-model="detailVisible"
      :instance-id="currentInstanceId"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { listCc, readCc } from '@/api/flow/task'
import InstanceDetail from '@/views/flow/execute/InstanceDetail.vue'
import type { FlowTask } from '@/types/flow'

const loading = ref(false)
const tableData = ref<FlowTask[]>([])
const total = ref(0)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  processName: undefined as string | undefined
})

const detailVisible = ref(false)
const currentInstanceId = ref<number>(0)
const currentTaskId = ref<number>(0)
const currentTaskStatus = ref<number>(0)

async function loadData() {
  loading.value = true
  try {
    const res = await listCc(queryParams)
    tableData.value = res.list
    total.value = res.total
  } finally {
    loading.value = false
  }
}

function handleQuery() {
  queryParams.pageNum = 1
  loadData()
}

function resetQuery() {
  queryParams.processName = undefined
  queryParams.pageNum = 1
  loadData()
}

function handleView(row: FlowTask) {
  currentInstanceId.value = row.instanceId
  currentTaskId.value = row.id
  currentTaskStatus.value = row.taskStatus ?? 0
  detailVisible.value = true
}

watch(detailVisible, async (val, oldVal) => {
  // 弹窗关闭且之前是打开状态，且抄送是未读的，则标记已读
  if (!val && oldVal && currentTaskId.value && currentTaskStatus.value === 0) {
    try {
      await readCc(currentTaskId.value)
      ElMessage.success('已标记为已读')
      currentTaskStatus.value = 1
      loadData()
    } catch {
      // 标记已读失败时不修改本地状态
    }
  }
})

onMounted(loadData)
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.pagination {
  margin-top: 20px;
  justify-content: flex-end;
}
.search-form {
  margin-bottom: 20px;
}
</style>
