<template>
  <div class="repair-order-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h2 class="page-title">
        <el-icon><Tools /></el-icon>
        维修工单管理
      </h2>
      <div class="header-actions">
<!--        <el-button type="primary" :icon="Plus" @click="handleAdd" v-hasPermi="['repairOrder:repairOrder:add']">
          新建工单
        </el-button>-->
        <el-button :icon="Download" @click="handleExport" v-hasPermi="['repairOrder:repairOrder:export']">
          导出报表
        </el-button>
      </div>
    </div>

    <!-- 统计卡片 -->
    <el-row :gutter="20" class="statistics-cards">
      <el-col :xs="24" :sm="12" :md="6">
        <div class="stat-card">
          <div class="stat-icon blue">
            <el-icon><DocumentCopy /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.total }}</div>
            <div class="stat-label">工单总数</div>
          </div>
        </div>
      </el-col>
      <el-col :xs="24" :sm="12" :md="6">
        <div class="stat-card">
          <div class="stat-icon orange">
            <el-icon><Clock /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.pending }}</div>
            <div class="stat-label">待处理</div>
          </div>
        </div>
      </el-col>
      <el-col :xs="24" :sm="12" :md="6">
        <div class="stat-card">
          <div class="stat-icon purple">
            <el-icon><Loading /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.processing }}</div>
            <div class="stat-label">处理中</div>
          </div>
        </div>
      </el-col>
      <el-col :xs="24" :sm="12" :md="6">
        <div class="stat-card">
          <div class="stat-icon green">
            <el-icon><CircleCheck /></el-icon>
          </div>
          <div class="stat-content">
            <div class="stat-value">{{ statistics.completed }}</div>
            <div class="stat-label">已完成</div>
          </div>
        </div>
      </el-col>
    </el-row>

    <!-- 搜索栏 -->
    <el-card class="search-card" shadow="never">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :md="6">
            <el-form-item label="工单编号" prop="orderNo">
              <el-input
                  v-model="queryParams.orderNo"
                  placeholder="请输入工单编号"
                  clearable
                  :prefix-icon="Search"
                  @keyup.enter="handleQuery"
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6">
            <el-form-item label="项目名称" prop="projectName">
              <el-input
                  v-model="queryParams.projectName"
                  placeholder="请输入项目名称"
                  clearable
                  :prefix-icon="Search"
                  @keyup.enter="handleQuery"
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6">
            <el-form-item label="工单状态" prop="orderStatus">
              <el-select
                  v-model="queryParams.orderStatus"
                  placeholder="请选择工单状态"
                  clearable
                  style="width: 100%;min-width: 200px;"
              >
                <el-option
                    v-for="dict in repair_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                >
                  <div style="display: flex; align-items: center; justify-content: space-between; width: 100%;">
                    <span>{{ dict.label }}</span>
                    <el-tag :type="getStatusTagType(dict.value)" size="small">
                      {{ dict.label }}
                    </el-tag>
                  </div>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="6">
            <el-form-item label="派单时间" prop="dateRange">
              <el-date-picker
                  v-model="queryParams.dateRange"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24" style="text-align: right;">
            <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-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card" shadow="never">
      <div class="table-header">
        <span class="table-title">工单列表</span>
        <div class="table-tools">
          <el-tooltip content="刷新" placement="top">
            <el-button :icon="Refresh" circle size="small" @click="getList" />
          </el-tooltip>
          <el-tooltip content="显示/隐藏搜索" placement="top">
            <el-button
                :icon="showSearch ? View : Hide"
                circle
                size="small"
                @click="showSearch = !showSearch"
            />
          </el-tooltip>
        </div>
      </div>

      <el-table
          v-loading="loading"
          :data="repairOrderList"
          @selection-change="handleSelectionChange"
          stripe
          border
          :row-class-name="tableRowClassName"
      >
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="序号" type="index" width="60" align="center">
          <template #default="scope">
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="工单编号" align="center" prop="orderNo" min-width="120">
          <template #default="scope">
            <el-link type="primary" @click="showDetail(scope.row)">
              {{ scope.row.orderNo }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="工单内容" align="center" prop="orderContent" min-width="200" show-overflow-tooltip />
        <el-table-column label="项目名称" align="center" prop="projectName" min-width="150" show-overflow-tooltip />
        <el-table-column label="派单时间" align="center" prop="assignTime" width="180" sortable>
          <template #default="scope">
            <div class="time-info">
              <el-icon><Clock /></el-icon>
              <span>{{ parseTime(scope.row.assignTime, '{y}-{m}-{d} {h}:{i}') }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="责任人" align="center" prop="finishBy" min-width="100">
          <template #default="scope">
            <div class="user-info">
              <el-avatar size="small" :src="getUserAvatar(scope.row.finishBy)">
                {{ scope.row.finishBy?.charAt(0) || '无' }}
              </el-avatar>
              <span>{{ scope.row.finishBy || '未分配' }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="工单状态" align="center" prop="orderStatus" width="100">
          <template #default="scope">
            <el-tag
                :type="getStatusTagType(scope.row.orderStatus)"
                effect="dark"
                size="small"
            >
              <el-icon v-if="scope.row.orderStatus === '0'"><Clock /></el-icon>
              <el-icon v-else-if="scope.row.orderStatus === '1'"><Loading /></el-icon>
              <el-icon v-else-if="scope.row.orderStatus === '2'"><CircleCheck /></el-icon>
              {{ getStatusLabel(scope.row.orderStatus) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="完成时间" align="center" prop="finishTime" width="180">
          <template #default="scope">
            <span v-if="scope.row.finishTime">
              {{ parseTime(scope.row.finishTime, '{y}-{m}-{d} {h}:{i}') }}
            </span>
            <span v-else class="text-muted">-</span>
          </template>
        </el-table-column>
        <el-table-column label="附件" align="center" prop="annex" width="80">
          <template #default="scope">
            <el-button
                v-if="scope.row.annex"
                type="primary"
                link
                :icon="Paperclip"
                @click="downloadFile(scope.row.annex)"
            >
              下载
            </el-button>
            <span v-else class="text-muted">-</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
          <template #default="scope">
            <el-button
                link
                type="primary"
                :icon="View"
                @click="showDetail(scope.row)"
                v-hasPermi="['repairOrder:repairOrder:edit']"
            >
              详情
            </el-button>
            <el-button
                v-if="scope.row.orderStatus === '0' || scope.row.orderStatus === '1'"
                link
                type="warning"
                :icon="Edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['repairOrder:repairOrder:edit']"
            >
              处理
            </el-button>
<!--            <el-button
                link
                type="danger"
                :icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['repairOrder:repairOrder:remove']"
            >
              删除
            </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-card>

    <!-- 工单详情抽屉 -->
    <el-drawer
        v-model="detailVisible"
        :title="`工单详情 - ${detailData.orderNo}`"
        direction="rtl"
        size="50%"
        class="detail-drawer"
    >
      <div class="detail-header">
        <el-tag :type="getStatusType(detailData.orderStatus)" size="large">
          {{ getStatusText(detailData.orderStatus) }}
        </el-tag>
        <el-button-group>
          <el-button :icon="Edit" @click="handleUpdateFromDetail" v-if="canDispatch(detailData)">编辑</el-button>
          <el-button :icon="Printer" @click="handlePrint">打印</el-button>
        </el-button-group>
      </div>

      <el-descriptions :column="2" border class="detail-descriptions">
        <el-descriptions-item label="工单编号">
          <span class="detail-value">{{ detailData.orderNo }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="项目名称">
          {{ detailData.projectName }}
        </el-descriptions-item>
        <el-descriptions-item label="工单内容" :span="2">
          <div class="content-box">{{ detailData.orderContent }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="派单时间">
          <div class="time-info">
            <el-icon><Clock /></el-icon>
            {{ parseTime(detailData.assignTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item label="完成时间">
          <div class="time-info" v-if="detailData.finishTime">
            <el-icon><CircleCheck /></el-icon>
            {{ parseTime(detailData.finishTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
          </div>
          <el-tag v-else type="info" size="small">未完成</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="责任人">
          <div class="user-detail" v-if="detailData.finishBy">
            <el-avatar>{{ detailData.finishBy.charAt(0) }}</el-avatar>
            <span>{{ detailData.finishBy }}</span>
          </div>
          <el-tag v-else type="warning" size="small">待分配</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="工单状态">
          <el-tag :type="getStatusType(detailData.orderStatus)" size="small">
            {{ getStatusText(detailData.orderStatus) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="工单备注" :span="2">
          <div class="remark-box">{{ detailData.orderRemark || '暂无备注' }}</div>
        </el-descriptions-item>
        <el-descriptions-item label="创建人">{{ detailData.createBy || '-' }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(detailData.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="更新人">{{ detailData.updateBy || '-' }}</el-descriptions-item>
        <el-descriptions-item label="更新时间">{{ parseTime(detailData.updateTime) }}</el-descriptions-item>
      </el-descriptions>

      <div v-if="detailData.annex" class="attachment-section">
        <h4>附件信息</h4>
        <div class="attachment-list">
          <div
              v-for="(filePath, index) in getFileList(detailData.annex)"
              :key="index"
              class="attachment-item"
          >
            <el-icon><Document /></el-icon>
            <span>{{ getFileName(filePath) }}</span>
            <el-button type="primary" link @click="downloadFile(filePath)">
              下载
            </el-button>
          </div>
        </div>
      </div>

      <!-- 操作日志 -->
      <div class="operation-log">
        <h4>操作记录</h4>
        <el-timeline v-if="operationLogs.length > 0">
          <el-timeline-item
              v-for="(log, index) in operationLogs"
              :key="index"
              :timestamp="log.operateTime"
              :type="getLogType(log.operateType)"
              placement="top"
          >
            <div class="log-content">
              <span class="log-user">{{ log.operateUser }}</span>
              <span class="log-action">{{ log.operateDesc }}</span>
            </div>
          </el-timeline-item>
        </el-timeline>
        <el-empty v-else description="暂无操作记录" :image-size="80" />
      </div>
    </el-drawer>

    <!-- 添加或修改维修工单对话框 -->
    <el-dialog
        :title="title"
        v-model="open"
        width="700px"
        append-to-body
        :close-on-click-modal="false"
    >
      <el-form ref="repairOrderRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="工单编号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入工单编号" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入项目名称" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="工单内容" prop="orderContent">
          <el-input
              v-model="form.orderContent"
              type="textarea"
              :rows="3"
              placeholder="请输入内容"
              disabled
          />
        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="派单时间" prop="assignTime">
              <el-date-picker
                  v-model="form.assignTime"
                  type="datetime"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  placeholder="请选择派单时间"
                  disabled
                  style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="完成时间" prop="finishTime" :rules="rules.finishTime">
              <el-date-picker
                  v-model="form.finishTime"
                  type="datetime"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  placeholder="请选择完成时间"
                  style="width: 100%"
                  :disabled-date="disabledDate"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="责任人" prop="finishBy">
          <el-select
              v-model="form.userId"
              placeholder="请选择责任人"
              clearable
              filterable
              disabled
              style="width: 100%"
          >
            <el-option
                v-for="user in userOptions"
                :key="user.userId"
                :label="user.nickName || user.userName"
                :value="user.userId"
            >
              <div style="display: flex; align-items: center;">
                <el-avatar size="small" style="margin-right: 8px">
                  {{ (user.nickName || user.userName).charAt(0) }}
                </el-avatar>
                <span>{{ user.nickName || user.userName }}</span>
              </div>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="附件上传" prop="annex">
          <file-upload
              v-model="form.annex"
              :on-remove="handleRemoveFile"
              :limit="5"
              :file-size="10"
          />
        </el-form-item>

        <el-form-item label="工单备注" prop="orderRemark">
          <el-input
              v-model="form.orderRemark"
              type="textarea"
              :rows="3"
              placeholder="请输入工单处理备注"
              maxlength="500"
              show-word-limit
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm" :loading="submitLoading">
            确 定
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RepairOrder2">
import { ref, reactive, toRefs, onMounted, computed } from 'vue';
import {
  listRepairOrder,
  getRepairOrder,
  delRepairOrder,
  addRepairOrder,
  updateRepairOrder,
  deleteFile, getRepairOrderStatistics,
  getRepairOrderLogs
} from "@/api/repairOrder/repairOrder";
import { listUser } from "@/api/system/user";
import { ElMessage, ElMessageBox, ElLoading } from 'element-plus';
// 导入图标
import {
  Tools,
  Plus,
  Download,
  Edit,
  Delete,
  Search,
  Refresh,
  View,
  Hide,
  Setting,
  Clock,
  CircleCheck,
  Paperclip,
  ArrowDown,
  ArrowUp,
  DocumentCopy,
  Printer,
  Document,
  Tickets,
  OfficeBuilding,
  InfoFilled,
  Warning,
  Loading
} from '@element-plus/icons-vue';

const { proxy } = getCurrentInstance();
const { repair_status } = proxy.useDict('repair_status');

// 响应式变量
const repairOrderList = ref([]);
const open = ref(false);
const loading = ref(true);
const submitLoading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const userOptions = ref([]);
const detailVisible = ref(false);
const detailData = ref({});
const statisticsData = ref({
  total: 0,
  pending: 0,
  processing: 0,
  completed: 0
});

// 统计数据
const statistics = computed(() => statisticsData.value);
// 详情相关
const operationLogs = ref([]); // 操作日志数据

const data = reactive({
  form: {},
  queryParams: {
    isMe: true,
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    orderContent: null,
    projectName: null,
    orderStatus: null,
    dateRange: [],
    state: Date.now(),
  },
  rules: {
    finishTime: [
      { required: true, message: "完成时间不能为空", trigger: "blur" }
    ],
    orderRemark: [
      { required: true, message: "请填写处理备注", trigger: "blur" }
    ]
  }
});

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

// 获取状态标签类型
function getStatusTagType(status) {
  const statusMap = {
    '0': 'warning',
    '1': 'primary',
    '2': 'success'
  };
  return statusMap[status] || 'info';
}

// 将逗号分隔的文件路径字符串转换为数组
function getFileList(annexString) {
  if (!annexString) return [];
  // 分割字符串并去除每个路径的前后空格
  return annexString.split(',').map(path => path.trim()).filter(path => path);
}

// 获取文件数量
function getFileCount(annexString) {
  if (!annexString) return 0;
  return getFileList(annexString).length;
}

// 修改原有的 getFileName 方法（如果需要的话）
function getFileName(url) {
  if (!url) return '';
  const parts = url.trim().split('/');
  return parts[parts.length - 1] || '附件';
}
// 获取日志类型
function getLogType(type) {
  const typeMap = {
    'create': 'primary',
    'assign': 'warning',
    'update': 'info',
    'complete': 'success'
  };
  return typeMap[type] || 'info';
}
// 获取状态类型（用于 el-tag 的 type）
function getStatusType(status) {
  const statusMap = {
    '0': 'warning',  // 待处理
    '1': 'primary',  // 处理中
    '2': 'success'   // 已完成
  };
  return statusMap[status] || 'info';
}

// 获取状态文本
function getStatusText(status) {
  const dict = repair_status.value.find(item => item.value === status);
  return dict ? dict.label : '未知状态';
}

// 判断是否可以派单/编辑
function canDispatch(row) {
  // admin 可以编辑所有工单
  const isAdmin = proxy.$auth.hasRole('admin');
  if (isAdmin) return true;

  // 其他用户只能编辑未完成的工单
  return row.orderStatus !== '2';
}

// 从详情页编辑
function handleUpdateFromDetail() {
  if (!detailData.value.id) {
    ElMessage.error('工单信息不完整');
    return;
  }

  // 关闭详情抽屉
  detailVisible.value = false;

  // 调用编辑方法
  handleUpdate(detailData.value);
}

// 打印功能
function handlePrint() {
  // 方法1：使用浏览器打印
  window.print();

  // 方法2：打印特定内容
  // const printContent = document.querySelector('.detail-drawer .el-drawer__body').innerHTML;
  // const printWindow = window.open('', '_blank');
  // printWindow.document.write(`
  //   <html>
  //     <head>
  //       <title>工单详情 - ${detailData.value.orderNo}</title>
  //       <style>
  //         /* 添加打印样式 */
  //         body { font-family: Arial, sans-serif; }
  //         .detail-header { margin-bottom: 20px; }
  //         /* ... 其他样式 */
  //       </style>
  //     </head>
  //     <body>
  //       ${printContent}
  //     </body>
  //   </html>
  // `);
  // printWindow.document.close();
  // printWindow.print();
}


// 显示详情（修改原有的 showDetail 方法）
async function showDetail(row) {
  detailData.value = { ...row };
  detailVisible.value = true;

  // 获取操作日志（如果有接口的话）
  await getOperationLogs(row.id);
}

// 获取操作日志
async function getOperationLogs(orderId) {
  try {
    // 模拟操作日志数据，实际应该从后端获取
    const response = await getRepairOrderLogs(orderId);
    operationLogs.value = response.data.map(log => ({
      logId: log.log_id,
      operateUser: log.operate_user,
      operateType: log.operate_type,
      operateDesc: log.operate_desc,
      orderId: log.order_id,
      operateTime: log.operate_time
    }));

    // 按时间倒序排序
    operationLogs.value.sort((a, b) => new Date(b.operateTime) - new Date(a.operateTime));
  } catch (error) {
    console.error('获取操作日志失败:', error);
    operationLogs.value = [];
  }
}


// 获取状态标签文本
function getStatusLabel(status) {
  const dict = repair_status.value.find(item => item.value === status);
  return dict ? dict.label : '未知';
}

// 获取用户头像
function getUserAvatar(userName) {
  // 这里可以根据实际情况返回用户头像URL
  return '';
}

// 表格行样式
function tableRowClassName({ row }) {
  if (row.orderStatus === '0') {
    return 'warning-row';
  } else if (row.orderStatus === '2') {
    return 'success-row';
  }
  return '';
}

// 禁用日期（不能选择派单时间之前的日期）
function disabledDate(time) {
  if (form.value.assignTime) {
    return time.getTime() < new Date(form.value.assignTime).getTime();
  }
  return false;
}

// 下载文件
async function downloadFile(url) {
  if (!url) {
    ElMessage.warning('暂无附件');
    return;
  }

  // 将逗号分隔的URL字符串转换为数组
  const urls = url.split(',').map(u => u.trim()).filter(u => u);

  if (urls.length === 0) {
    ElMessage.warning('暂无附件');
    return;
  }

  const loading = ElLoading.service({
    lock: true,
    text: `正在下载文件 (0/${urls.length})...`,
    background: 'rgba(0, 0, 0, 0.7)',
  });

  let successCount = 0;
  let failCount = 0;

  try {
    for (let i = 0; i < urls.length; i++) {
      const currentUrl = urls[i];

      // 更新加载提示
      loading.setText(`正在下载文件 (${i + 1}/${urls.length})...`);

      try {
        const response = await fetch(import.meta.env.VITE_APP_BASE_API + currentUrl, {
          method: 'GET',
          headers: {
            'Authorization': 'Bearer ' ,// + getToken(),
          }
        });

        if (!response.ok) {
          throw new Error('下载失败');
        }

        const blob = await response.blob();
        const fileName = currentUrl.split('/').pop() || `download_${i + 1}`;
        const fileExt = fileName.split('.').pop().toLowerCase();

        const mimeTypes = {
          'txt': 'text/plain;charset=utf-8',
          'pdf': 'application/pdf',
          'doc': 'application/msword',
          'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
          'xls': 'application/vnd.ms-excel',
          'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
          'ppt': 'application/vnd.ms-powerpoint',
          'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation'
        };

        const typedBlob = new Blob([blob], {
          type: mimeTypes[fileExt] || 'application/octet-stream'
        });

        const downloadUrl = window.URL.createObjectURL(typedBlob);
        const link = document.createElement('a');
        link.href = downloadUrl;
        link.download = fileName;
        link.style.display = 'none';

        document.body.appendChild(link);
        link.click();

        setTimeout(() => {
          window.URL.revokeObjectURL(downloadUrl);
          document.body.removeChild(link);
        }, 100);

        successCount++;

        // 添加延迟，避免同时触发太多下载
        if (i < urls.length - 1) {
          await new Promise(resolve => setTimeout(resolve, 500));
        }
      } catch (error) {
        console.error(`下载文件失败: ${currentUrl}`, error);
        failCount++;
      }
    }

    if (successCount > 0 && failCount === 0) {
      ElMessage.success(`成功下载 ${successCount} 个文件`);
    } else if (successCount > 0 && failCount > 0) {
      ElMessage.warning(`成功下载 ${successCount} 个文件，失败 ${failCount} 个`);
    } else {
      ElMessage.error('所有文件下载失败');
    }
  } catch (error) {
    console.error('下载过程出错:', error);
    ElMessage.error('下载失败，请重试');
  } finally {
    loading.close();
  }
}


/** 查询维修工单列表 */
async function getList() {
  loading.value = true;
  try {
    const params = { ...queryParams.value };

    // 处理日期范围
    /*if (dateRange.value && dateRange.value.length === 2) {
      params.startDate = dateRange.value[0];
      params.endDate = dateRange.value[1];
    }*/

    const response = await listRepairOrder(params);
    repairOrderList.value = response.rows;
    total.value = response.total;

    // 获取统计数据
    await getStatistics();
  } catch (error) {
    console.error('获取数据失败:', error);
    ElMessage.error('获取工单列表失败');
  } finally {
    loading.value = false;
  }
}

async function getStatistics() {
  try {
    // 如果后端没有统计接口，可以通过查询所有数据来统计
    const response = await getRepairOrderStatistics({isMe: true});

    // 直接更新 statisticsData
    statisticsData.value = {
      total: response.data.total || 0,
      pending: response.data.pending || 0,
      processing: response.data.processing || 0,
      completed: response.data.completed || 0
    };

    // 计算趋势（这里模拟数据，实际应该对比上期数据）
    /*statisticsData.value.forEach(stat => {
      stat.trend = Math.floor(Math.random() * 30) - 15; // 随机生成-15到15的趋势
    });*/
  } catch (error) {
    console.error('获取统计数据失败:', error);
  }
}

// 删除附件
function handleRemoveFile(file) {
  ElMessageBox.confirm('确认删除该附件吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteFile(file.url).then(() => {
      form.value.annex = null;
      ElMessage.success("删除成功");
    }).catch(() => {
      ElMessage.error("删除失败");
    });
  }).catch(() => {});
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderNo: null,
    orderContent: null,
    projectName: null,
    assignTime: null,
    finishTime: null,
    finishBy: null,
    annex: null,
    userId: null,
    orderStatus: null,
    orderRemark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("repairOrderRef");
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.dateRange = [];
  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 getUserList() {
  listUser().then(response => {
    userOptions.value = response.rows || [];
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getRepairOrder(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "处理维修工单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["repairOrderRef"].validate(valid => {
    if (valid) {
      submitLoading.value = true;

      // 设置完成人信息
      if (form.value.userId) {
        const user = userOptions.value.find(u => u.userId === form.value.userId);
        if (user) {
          form.value.finishBy = user.nickName || user.userName;
        }
      }

      // 设置工单状态为已完成
      form.value.orderStatus = '2';

      if (form.value.id != null) {
        updateRepairOrder(form.value).then(response => {
          ElMessage.success("处理成功");
          open.value = false;
          getList();
        }).finally(() => {
          submitLoading.value = false;
        });
      } else {
        addRepairOrder(form.value).then(response => {
          ElMessage.success("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          submitLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  const deletePromises = [];

  ElMessageBox.confirm('是否确认删除选中的工单？删除后不可恢复！', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 如果是批量删除，检查每个选中项是否有附件
    if (Array.isArray(ids.value) && ids.value.length > 0) {
      repairOrderList.value.forEach(item => {
        if (ids.value.includes(item.id) && item.annex) {
          deletePromises.push(deleteFile(item.annex));
        }
      });
    }
    // 如果是单个删除，检查是否有附件
    else if (row.annex) {
      deletePromises.push(deleteFile(row.annex));
    }

    // 先删除所有附件
    return Promise.all(deletePromises).then(() => {
      // 附件删除成功后删除数据
      return delRepairOrder(_ids);
    });
  }).then(() => {
    getList();
    ElMessage.success("删除成功");
  }).catch((error) => {
    if (error !== 'cancel') {
      ElMessage.error("删除失败");
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  ElMessageBox.confirm('确定要导出所有维修工单数据吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info'
  }).then(() => {
    proxy.download('repairOrder/repairOrder/export', {
      ...queryParams.value
    }, `维修工单_${new Date().getTime()}.xlsx`);
  });
}

// 初始化
onMounted(() => {
  getList();
  getUserList();
});
</script>

<style scoped lang="scss">
.repair-order-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 84px);

  .page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    .page-title {
      font-size: 24px;
      font-weight: 600;
      color: #303133;
      margin: 0;
      display: flex;
      align-items: center;

      .el-icon {
        margin-right: 8px;
        color: #409eff;
      }
    }

    .header-actions {
      display: flex;
      gap: 10px;
    }
  }

  // 统计卡片
  .statistics-cards {
    margin-bottom: 20px;

    .stat-card {
      background: #fff;
      border-radius: 8px;
      padding: 20px;
      display: flex;
      align-items: center;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
      transition: all 0.3s;
      margin-bottom: 10px;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.08);
      }

      .stat-icon {
        width: 60px;
        height: 60px;
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 16px;

        .el-icon {
          font-size: 28px;
        }

        &.blue {
          background: #e6f2ff;
          color: #409eff;
        }

        &.orange {
          background: #fdf6ec;
          color: #e6a23c;
        }

        &.purple {
          background: #f3e8ff;
          color: #9c27b0;
        }

        &.green {
          background: #e8f5e9;
          color: #67c23a;
        }
      }

      .stat-content {
        flex: 1;

        .stat-value {
          font-size: 28px;
          font-weight: 600;
          color: #303133;
          line-height: 1;
          margin-bottom: 8px;
        }

        .stat-label {
          font-size: 14px;
          color: #909399;
        }
      }
    }
  }

  // 搜索卡片
  .search-card {
    margin-bottom: 20px;

    :deep(.el-card__body) {
      padding: 20px 20px 0;
    }

    .el-form-item {
      margin-bottom: 20px;
    }
  }

  // 表格卡片
  .table-card {
    :deep(.el-card__body) {
      padding: 0;
    }

    .table-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20px;
      border-bottom: 1px solid #ebeef5;

      .table-title {
        font-size: 16px;
        font-weight: 600;
        color: #303133;
      }

      .table-tools {
        display: flex;
        gap: 8px;
      }
    }

    .el-table {
      :deep(.el-table__header) {
        th {
          background-color: #f5f7fa;
          color: #303133;
          font-weight: 600;
        }
      }

      :deep(.warning-row) {
        background-color: #fef0f0;
      }

      :deep(.success-row) {
        background-color: #f0f9ff;
      }

      .time-info {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 4px;
        color: #606266;

        .el-icon {
          color: #909399;
        }
      }

      .user-info {
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;

        .el-avatar {
          background-color: #409eff;
          color: #fff;
          font-size: 12px;
        }
      }

      .text-muted {
        color: #c0c4cc;
      }
    }
  }

  // 详情抽屉
  .detail-drawer {
    :deep(.el-drawer__header) {
      margin-bottom: 0;
      padding-bottom: 0;
    }

    .detail-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 20px 20px;
      border-bottom: 1px solid #ebeef5;
    }

    .detail-descriptions {
      margin: 20px;

      .detail-value {
        font-weight: 600;
        color: #409eff;
      }

      .content-box {
        background: #f5f7fa;
        padding: 12px;
        border-radius: 4px;
        line-height: 1.6;
      }

      .time-info {
        display: flex;
        align-items: center;
        gap: 4px;

        .el-icon {
          color: #909399;
        }
      }

      .user-detail {
        display: flex;
        align-items: center;
        gap: 8px;

        .el-avatar {
          background-color: #409eff;
          color: #fff;
        }
      }

      .remark-box {
        background: #fef0f0;
        padding: 12px;
        border-radius: 4px;
        color: #e6a23c;
      }
    }

    .attachment-section {
      margin: 20px;

      h4 {
        margin: 0 0 12px 0;
        font-size: 16px;
        color: #303133;
      }

      .attachment-list {
        background: #f5f7fa;
        padding: 16px;
        border-radius: 4px;

        .attachment-item {
          display: flex;
          align-items: center;
          gap: 8px;

          .el-icon {
            color: #909399;
            font-size: 20px;
          }

          span {
            flex: 1;
          }
        }
      }
    }

    .operation-log {
      margin: 20px;

      h4 {
        margin: 0 0 16px 0;
        font-size: 16px;
        color: #303133;
      }

      .log-content {
        .log-user {
          font-weight: 500;
          color: #409eff;
          margin-right: 8px;
        }

        .log-action {
          color: #606266;
        }
      }
    }
  }


  // 对话框样式
  :deep(.el-dialog) {
    .el-dialog__header {
      background-color: #f5f7fa;
      padding: 20px;

      .el-dialog__title {
        font-size: 18px;
        color: #303133;
      }
    }

    .el-dialog__body {
      padding: 20px;
    }

    .el-form {
      .el-form-item__label {
        font-weight: 500;
      }
    }
  }
}

// 响应式设计
@media (max-width: 768px) {
  .repair-order-container {
    padding: 10px;

    .page-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 10px;

      .header-actions {
        width: 100%;
        display: flex;
        justify-content: flex-end;
      }
    }

    .statistics-cards {
      .el-col {
        margin-bottom: 10px;
      }
    }

    .search-card {
      :deep(.el-form) {
        .el-form-item {
          display: block;
          margin-bottom: 16px;
        }
      }
    }

    .detail-drawer {
      :deep(.el-drawer) {
        width: 90% !important;
      }
    }
  }
}

// 动画效果
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.stat-card {
  animation: fadeIn 0.5s ease-out;

  &:nth-child(1) { animation-delay: 0.1s; }
  &:nth-child(2) { animation-delay: 0.2s; }
  &:nth-child(3) { animation-delay: 0.3s; }
  &:nth-child(4) { animation-delay: 0.4s; }
}

// 标签样式优化
:deep(.el-tag) {
  .el-icon {
    margin-right: 4px;
  }
}

// 链接样式
.el-link {
  font-weight: 500;
}

// 分页器样式
:deep(.pagination-container) {
  padding: 20px;
  background: #fff;
}
.attachment-section {
  margin: 20px;

  h4 {
    margin: 0 0 12px 0;
    font-size: 16px;
    color: #303133;
  }

  .attachment-list {
    background: #f5f7fa;
    padding: 16px;
    border-radius: 4px;

    .attachment-item {
      display: flex;
      align-items: center;
      gap: 8px;
      padding: 8px 0;

      &:not(:last-child) {
        border-bottom: 1px solid #e4e7ed;
      }

      .el-icon {
        color: #909399;
        font-size: 20px;
      }

      span {
        flex: 1;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}
</style>
