<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryFormRef" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <!--                        <el-form-item label="日期" prop="date">-->
      <!--                            <el-input-->
      <!--                                    v-model="queryParams.date"-->
      <!--                                    placeholder="请输入日期"-->
      <!--                                    clearable-->
      <!--                                    @keyup.enter="handleQuery"-->
      <!--                            />-->
      <!--                        </el-form-item>-->
      <el-form-item label="记录时间">
        <el-date-picker
          v-model="daterange"
          style="width: 340px"
          value-format="yyyy/MM/dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </el-form-item>
      <!--     <el-form-item label="位置" prop="location">
                  <el-input
                          v-model="queryParams.location"
                          placeholder="请输入位置"
                          clearable
                          @keyup.enter="handleQuery"
                  />
              </el-form-item>
              <el-form-item label="科室值班" prop="dailyDutySupervisor">
                  <el-input
                          v-model="queryParams.dailyDutySupervisor"
                          placeholder="请输入当日值班长"
                          clearable
                          @keyup.enter="handleQuery"
                  />
              </el-form-item>
              <el-form-item label="外包值班" prop="tendaDailyDutySupervisor">
                  <el-input
                          v-model="queryParams.tendaDailyDutySupervisor"
                          placeholder="请输入天达当日值班长"
                          clearable
                          @keyup.enter="handleQuery"
                  />
              </el-form-item>
              <el-form-item label="故障情况" prop="faultSituation">
                  <el-input
                          v-model="queryParams.faultSituation"
                          placeholder="请输入故障情况"
                          clearable
                          @keyup.enter="handleQuery"
                  />
              </el-form-item>
              <el-form-item label="处置措施" prop="handlingMeasures">
                  <el-input
                          v-model="queryParams.handlingMeasures"
                          placeholder="请输入处置措施"
                          clearable
                          @keyup.enter="handleQuery"
                  />
              </el-form-item>
              <el-form-item label="故障类型" prop="faultType">
                  <el-select v-model="queryParams.faultType" placeholder="请选择故障类型" clearable>
                      <el-option
                              v-for="dict in dict.type.${dictType}"
                              :key="dict.value"
                              :label="dict.label"
                              :value="dict.value"
                      />
                  </el-select>
              </el-form-item>
              <el-form-item label="故障发生时间" prop="faultOccurrenceTime">
                  <el-input
                          v-model="queryParams.faultOccurrenceTime"
                          placeholder="请输入故障发生时间"
                          clearable
                          @keyup.enter="handleQuery"
                  />
              </el-form-item>
              <el-form-item label="处置完毕时间" prop="handlingCompletionTime">
                  <el-input
                          v-model="queryParams.handlingCompletionTime"
                          placeholder="请输入处置完毕时间"
                          clearable
                          @keyup.enter="handleQuery"
                  />
              </el-form-item>
              <el-form-item label="处置时长" prop="handlingDuration">
                  <el-input
                          v-model="queryParams.handlingDuration"
                          placeholder="请输入处置时长"
                          clearable
                          @keyup.enter="handleQuery"
                  />
              </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作提示 -->
    <!-- <el-alert
            title="提示：可以在表格行上右击查看详细信息，或点击操作列的详情按钮"
            type="info"
            :closable="false"
            show-icon
            style="margin-bottom: 10px;"
        /> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['ems/info:dailyFaultRecord:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ems/info:dailyFaultRecord:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ems/info:dailyFaultRecord:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['ems/info:dailyFaultRecord:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport" v-hasPermi="['ems/info:dailyFaultRecord:add']"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" @click="handleClearAll" v-hasPermi="['ems/info:dailyFaultRecord:remove']"
          >一键全部清空</el-button
        >
      </el-col>
      <right-toolbar v-model:show-search="showSearch" @query-table="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dailyFaultRecordList"
      @selection-change="handleSelectionChange"
      @row-contextmenu="handleRowContextMenu"
      class="el-table--enable-row-hover fault-record-table"
      tooltip-effect="dark"
      :header-cell-style="{ backgroundColor: '#f5f7fa', color: '#606266' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="60" v-if="columns[0].visible">
        <template #default="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="日期" align="center" prop="date" v-if="columns[1].visible" />
      <el-table-column label="位置" align="center" prop="location" v-if="columns[2].visible" />
      <el-table-column label="科室值班" align="center" prop="dailyDutySupervisor" v-if="columns[3].visible" />
      <el-table-column label="外包值班" align="center" prop="tendaDailyDutySupervisor" v-if="columns[4].visible" />
      <el-table-column label="故障情况" align="center" prop="faultSituation" v-if="columns[5].visible" show-overflow-tooltip />
      <el-table-column label="处置措施" align="center" prop="handlingMeasures" v-if="columns[6].visible" show-overflow-tooltip />
      <el-table-column label="故障类型" align="center" prop="faultType" v-if="columns[7].visible" />
      <el-table-column label="故障发生时间" align="center" prop="faultOccurrenceTime" v-if="columns[8].visible" />
      <el-table-column label="处置完毕时间" align="center" prop="handlingCompletionTime" v-if="columns[9].visible" />
      <el-table-column label="处置时长" align="center" prop="handlingDurationFormatted" v-if="columns[10].visible" />
      <el-table-column label="备注" align="center" prop="remarks" v-if="columns[11].visible" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleViewDetail(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['ems/info:dailyFaultRecord:edit']"
            >修改</el-button
          >
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['ems/info:dailyFaultRecord: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-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="日期" prop="date">
          <el-date-picker v-model="form.date" type="date" placeholder="请选择日期" value-format="yyyy/MM/dd" format="yyyy/MM/dd" />
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="科室值班" prop="dailyDutySupervisor">
          <el-input v-model="form.dailyDutySupervisor" placeholder="请输入当日值班长" />
        </el-form-item>
        <el-form-item label="外包值班" prop="tendaDailyDutySupervisor">
          <el-input v-model="form.tendaDailyDutySupervisor" placeholder="请输入天达当日值班长" />
        </el-form-item>
        <el-form-item label="故障情况" prop="faultSituation">
          <el-input v-model="form.faultSituation" placeholder="请输入故障情况" />
        </el-form-item>
        <el-form-item label="处置措施" prop="handlingMeasures">
          <el-input v-model="form.handlingMeasures" placeholder="请输入处置措施" />
        </el-form-item>
        <el-form-item label="故障类型" prop="faultType">
          <el-input v-model="form.faultType" placeholder="请输入故障类型" />
        </el-form-item>
        <el-form-item label="故障发生时间" prop="faultOccurrenceTime">
          <el-time-picker v-model="form.faultOccurrenceTime" placeholder="请选择故障发生时间" value-format="HH:mm:ss" format="HH:mm:ss" />
        </el-form-item>
        <el-form-item label="处置完毕时间" prop="handlingCompletionTime">
          <el-time-picker v-model="form.handlingCompletionTime" placeholder="请选择处置完毕时间" value-format="HH:mm:ss" format="HH:mm:ss" />
        </el-form-item>
        <el-form-item label="处置时长" prop="handlingDurationFormatted">
          <el-input v-model="form.handlingDurationFormatted" placeholder="由系统自动计算" disabled />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" :rows="3" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" v-model="upload.open" width="400px" append-to-body>
      <el-upload
        ref="uploadRef"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <template #tip>
          <div class="el-upload__tip text-center">
            <!--                    <div class="el-upload__tip" slot="tip">-->
            <!--                        <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的数据-->
            <!--                    </div>-->
            <span>仅允许导入xls、xlsx格式文件。</span>
            <!--                    <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>-->
          </div>
        </template>
      </el-upload>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="upload.open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 详情抽屉 -->
    <el-drawer title="故障记录详情" v-model="detailDrawer" direction="rtl" size="500px" :before-close="handleDetailClose">
      <div class="detail-content" v-if="currentRecord">
        <el-descriptions :column="1" border>
          <el-descriptions-item label="记录ID">
            <span>{{ currentRecord.id }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="日期">
            <span>{{ currentRecord.date }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="位置">
            <span>{{ currentRecord.location || '暂无' }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="科室值班">
            <span>{{ currentRecord.dailyDutySupervisor || '暂无' }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="外包值班">
            <span>{{ currentRecord.tendaDailyDutySupervisor || '暂无' }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="故障类型">
            <span>{{ currentRecord.faultType || '暂无' }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="故障发生时间">
            <span>{{ currentRecord.faultOccurrenceTime || '暂无' }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="处置完毕时间">
            <span>{{ currentRecord.handlingCompletionTime || '暂无' }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="处置时长">
            <span>{{ currentRecord.handlingDurationFormatted || '暂无' }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="故障情况">
            <div class="detail-text-content">
              {{ currentRecord.faultSituation || '暂无' }}
            </div>
          </el-descriptions-item>
          <el-descriptions-item label="处置措施">
            <div class="detail-text-content">
              {{ currentRecord.handlingMeasures || '暂无' }}
            </div>
          </el-descriptions-item>
          <el-descriptions-item label="备注">
            <div class="detail-text-content">
              {{ currentRecord.remarks || '暂无' }}
            </div>
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-drawer>

    <!-- 右击菜单 -->
    <ul v-show="contextMenuVisible" :style="contextMenuStyle" class="context-menu" @click.stop>
      <li @click="handleViewDetail(contextMenuRow)">
        <i class="el-icon-view"></i>
        查看详情
      </li>
      <!--            <li @click="handleUpdate(contextMenuRow)" v-if="checkPermission(['ems/info:dailyFaultRecord:edit'])">-->
      <!--                <i class="el-icon-edit"></i>-->
      <!--                修改记录-->
      <!--            </li>-->
      <!--            <li @click="handleDelete(contextMenuRow)" v-if="checkPermission(['ems/info:dailyFaultRecord:remove'])">-->
      <!--                <i class="el-icon-delete"></i>-->
      <!--                删除记录-->
      <!--            </li>-->
    </ul>
  </div>
</template>

<script setup lang="ts">
import { useEmsClearAll } from '@/composables/useEmsClearAll';
import { getToken } from '@/utils/auth';
import {
  listDailyFaultRecord,
  getDailyFaultRecord,
  delDailyFaultRecord,
  addDailyFaultRecord,
  updateDailyFaultRecord
} from '@/api/ems/info/dailyFaultRecord';
import { checkPermi } from '@/utils/permission';

defineOptions({
  name: 'DailyFaultRecord'
} as any);

const { proxy } = getCurrentInstance() as any;

const formRef = ref<any>();
const queryFormRef = ref<any>();
const uploadRef = ref<any>();

// 这里统一通过工厂函数重建表单，避免弹窗在新增/编辑切换时残留上一条业务数据。
const createFormData = () => ({
  id: null,
  date: null,
  location: null,
  dailyDutySupervisor: null,
  tendaDailyDutySupervisor: null,
  faultSituation: null,
  handlingMeasures: null,
  faultType: null,
  faultOccurrenceTime: null,
  handlingCompletionTime: null,
  handlingDuration: null,
  handlingDurationFormatted: null,
  remarks: null
});

// 这里保留原页面字段结构，优先保证 EMS 业务行为与接口入参不发生漂移。
const state = reactive({
  daterange: [],
  // 遮罩层
  loading: true,
  // 选中数组
  ids: [],
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  // 总条数
  total: 0,
  // 日常故障记录表格数据
  dailyFaultRecordList: [],
  // 弹出层标题
  title: '',
  // 是否显示弹出层
  open: false,
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    date: null,
    location: null,
    dailyDutySupervisor: null,
    tendaDailyDutySupervisor: null,
    faultSituation: null,
    handlingMeasures: null,
    faultType: null,
    faultOccurrenceTime: null,
    handlingCompletionTime: null,
    handlingDuration: null,
    remarks: null,
    params: {}
  },
  // 表单参数
  form: createFormData(),
  // 表单校验
  rules: {
    date: [
      {
        required: true,
        message: '日期不能为空',
        trigger: 'blur'
      }
    ]
  },
  columns: [
    {
      key: 0,
      label: `序号`,
      visible: true
    },
    {
      key: 1,
      label: `日期`,
      visible: true
    },
    {
      key: 2,
      label: `位置`,
      visible: true
    },
    {
      key: 3,
      label: `科室值班`,
      visible: true
    },
    {
      key: 4,
      label: `外包值班`,
      visible: true
    },
    {
      key: 5,
      label: `故障情况`,
      visible: true
    },
    {
      key: 6,
      label: `处置措施`,
      visible: true
    },
    {
      key: 7,
      label: `故障类型`,
      visible: true
    },
    {
      key: 8,
      label: `故障发生时间`,
      visible: true
    },
    {
      key: 9,
      label: `处置完毕时间`,
      visible: true
    },
    {
      key: 10,
      label: `处置时长`,
      visible: true
    },
    {
      key: 11,
      label: `备注`,
      visible: true
    }
  ],
  upload: {
    title: '导入日常故障记录',
    open: false,
    url: '',
    updateSupport: false,
    isUploading: false,
    headers: {}
  },
  // 详情抽屉相关
  detailDrawer: false,
  currentRecord: null,
  // 右击菜单相关
  contextMenuVisible: false,
  contextMenuStyle: {
    position: 'fixed',
    zIndex: 1000,
    left: '0px',
    top: '0px'
  },
  contextMenuRow: null,
  clearAllConfig: {
    moduleKey: 'dailyFaultRecord',
    entityName: '日常故障记录',
    permission: 'ems/info:dailyFaultRecord:remove'
  }
} as any);

const {
  clearAllConfig,
  columns,
  contextMenuRow,
  contextMenuStyle,
  contextMenuVisible,
  currentRecord,
  dailyFaultRecordList,
  daterange,
  detailDrawer,
  form,
  ids,
  loading,
  multiple,
  open,
  queryParams,
  rules,
  showSearch,
  single,
  title,
  total,
  upload
} = toRefs(state);

const getList = () => {
  loading.value = true;

  // 处理日期范围参数
  if (!queryParams.value.params) {
    queryParams.value.params = {};
  }
  if (daterange.value && daterange.value.length === 2) {
    queryParams.value.params.beginDate = daterange.value[0];
    queryParams.value.params.endDate = daterange.value[1];
  } else {
    // 清空日期范围参数
    delete queryParams.value.params.beginDate;
    delete queryParams.value.params.endDate;
  }
  listDailyFaultRecord(queryParams.value).then((response) => {
    dailyFaultRecordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
};

const cancel = () => {
  open.value = false;
  reset();
};

const reset = () => {
  form.value = {
    id: null,
    date: null,
    location: null,
    dailyDutySupervisor: null,
    tendaDailyDutySupervisor: null,
    faultSituation: null,
    handlingMeasures: null,
    faultType: null,
    faultOccurrenceTime: null,
    handlingCompletionTime: null,
    handlingDuration: null,
    handlingDurationFormatted: null,
    remarks: null
  };
  formRef.value?.resetFields();
};

const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

const resetQuery = () => {
  daterange.value = [];
  queryFormRef.value?.resetFields();
  handleQuery();
};

const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};

const handleAdd = () => {
  reset();
  open.value = true;
  title.value = '添加  日常故障记录';
};

const handleUpdate = (row) => {
  reset();
  const id = row.id || ids.value;
  getDailyFaultRecord(id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = '修改  日常故障记录';
  });
};

const submitForm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateDailyFaultRecord(form.value).then((response) => {
          proxy?.$modal.msgSuccess('修改成功');
          open.value = false;
          getList();
        });
      } else {
        addDailyFaultRecord(form.value).then((response) => {
          proxy?.$modal.msgSuccess('新增成功');
          open.value = false;
          getList();
        });
      }
    }
  });
};

const handleDelete = (row) => {
  const ids = row.id || ids.value;
  proxy?.$modal
    .confirm('是否确认删除日常故障记录编号为"' + ids + '"的数据项？')
    .then(function () {
      return delDailyFaultRecord(ids);
    })
    .then(() => {
      getList();
      proxy?.$modal.msgSuccess('删除成功');
    })
    .catch(() => {});
};

const handleExport = () => {
  proxy?.download(
    'ems/info/dailyFaultRecord/export',
    {
      ...queryParams.value
    },
    `dailyFaultRecord_${new Date().getTime()}.xlsx`
  );
};

const handleImport = () => {
  upload.value.title = '导入日常故障记录';
  upload.value.open = true;
};

const importTemplate = () => {
  proxy?.download('ems/info/dailyFaultRecord/importTemplate', {}, `日常故障记录数据模板_${new Date().getTime()}.xlsx`);
};

const handleFileUploadProgress = (event, file, fileList) => {
  upload.value.isUploading = true;
};

const handleFileSuccess = (response, file, fileList) => {
  upload.value.open = false;
  upload.value.isUploading = false;
  uploadRef.value?.clearFiles();
  proxy?.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + '</div>', '导入结果', {
    dangerouslyUseHTMLString: true
  });
  getList();
};

const submitFileForm = () => {
  uploadRef.value?.submit();
};

const handleViewDetail = (row) => {
  currentRecord.value = row;
  detailDrawer.value = true;
  hideContextMenu();
};

const handleDetailClose = () => {
  currentRecord.value = null;
  detailDrawer.value = false;
};

const handleRowContextMenu = (row, column, event) => {
  event.preventDefault();
  contextMenuRow.value = row;

  // 设置右击菜单位置，考虑边界情况
  nextTick(() => {
    const menuWidth = 120; // 菜单宽度
    const menuHeight = 120; // 菜单高度
    const windowWidth = window.innerWidth;
    const windowHeight = window.innerHeight;
    let left = event.clientX;
    let top = event.clientY;

    // 防止菜单超出右边界
    if (left + menuWidth > windowWidth) {
      left = windowWidth - menuWidth - 10;
    }

    // 防止菜单超出下边界
    if (top + menuHeight > windowHeight) {
      top = windowHeight - menuHeight - 10;
    }
    contextMenuStyle.value.left = left + 'px';
    contextMenuStyle.value.top = top + 'px';
    contextMenuVisible.value = true;
  });
};

const hideContextMenu = () => {
  contextMenuVisible.value = false;
  contextMenuRow.value = null;
};

const checkPermission = (permissions) => {
  return checkPermi(permissions);
};

const { handleClearAll } = useEmsClearAll(clearAllConfig, getList);

onMounted(() => {
  getList();
  upload.value.url = import.meta.env.VITE_APP_BASE_API + '/ems/info/dailyFaultRecord/importData';
  upload.value.headers = {
    Authorization: 'Bearer ' + getToken()
  };

  // 配置清空功能
  clearAllConfig.value = {
    moduleKey: 'dailyFaultRecord',
    entityName: '日常故障记录',
    permission: 'ems/info:dailyFaultRecord:remove'
  };
});

onMounted(() => {
  // script setup 下没有 this，上下文菜单回收必须绑定稳定函数引用，否则解绑会失效。
  document.addEventListener('click', hideContextMenu);
});

onBeforeUnmount(() => {
  // 这里复用同一个函数引用，避免页面切换后遗留全局监听器。
  document.removeEventListener('click', hideContextMenu);
});
</script>

<style scoped>
/* 右击菜单样式 */
.context-menu {
  position: fixed;
  z-index: 1000;
  background-color: #fff;
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 5px 0;
  margin: 0;
  list-style: none;
  min-width: 120px;
}

.context-menu li {
  padding: 8px 16px;
  cursor: pointer;
  font-size: 14px;
  color: #606266;
  display: flex;
  align-items: center;
  transition: background-color 0.3s;
}

.context-menu li:hover {
  background-color: #f5f7fa;
  color: #409eff;
}

.context-menu li i {
  margin-right: 8px;
  font-size: 14px;
}

/* 详情内容样式 */
.detail-content {
  padding: 20px;
}

.detail-text-content {
  max-height: 200px;
  overflow-y: auto;
  word-wrap: break-word;
  word-break: break-all;
  line-height: 1.6;
  padding: 8px;
  background-color: #f8f9fa;
  border-radius: 4px;
  border: 1px solid #e9ecef;
}

/* 表格行悬停效果 */
.fault-record-table .el-table__row {
  transition: background-color 0.3s;
}

.fault-record-table .el-table__row:hover {
  cursor: context-menu;
  background-color: #f0f9ff !important;
}

/* 表格样式优化 */
.fault-record-table {
  border-radius: 4px;
  overflow: hidden;
}

.fault-record-table .el-table__header-wrapper {
  border-radius: 4px 4px 0 0;
}

/* 描述列表样式优化 */
.detail-content .el-descriptions {
  margin-top: 0;
}

.detail-content .el-descriptions-item__label {
  font-weight: 600;
  color: #303133;
  width: 90px;
}

.detail-content .el-descriptions-item__content {
  color: #606266;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .context-menu {
    min-width: 100px;
    font-size: 12px;
  }

  .context-menu li {
    padding: 6px 12px;
  }

  .detail-content {
    padding: 15px;
  }

  .detail-content .el-descriptions-item__label {
    width: 80px;
    font-size: 13px;
  }
}

/* 打印样式 */
@media print {
  .context-menu {
    display: none !important;
  }
}

.el-table--enable-row-hover .el-table__body tr:hover > td.el-table__cell {
  background-color: #0264f7;
  color: #fff;
}
</style>
