<template>
  <el-dialog
    title="配置历史记录"
    :visible.sync="visible"
    width="1080px"
    append-to-body
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <div class="history-head">
      <div class="history-head__title">{{ historyTitle || '未命名配置' }}</div>
      <div class="history-head__desc">保留每次新增、编辑、删除时的快照，可用于追溯考勤规则变更。</div>
    </div>

    <div class="history-layout">
      <div class="history-layout__table">
        <NCC-table
          v-loading="listLoading"
          :data="tableData"
          border
          size="mini"
          height="420"
          highlight-current-row
          @current-change="handleCurrentChange"
        >
          <el-table-column prop="versionNo" label="版本" width="80" align="left" />
          <el-table-column prop="operateTypeText" label="操作" width="90" align="left" />
          <el-table-column prop="title" label="标题" min-width="170" align="left" show-overflow-tooltip />
          <el-table-column prop="operateUserName" label="操作人" width="110" align="left">
            <template slot-scope="scope">
              <span>{{ scope.row.operateUserName || '系统' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="operateTime" label="操作时间" width="160" align="left" />
          <el-table-column prop="changeReason" label="变更原因" min-width="180" align="left" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ scope.row.changeReason || '无' }}</span>
            </template>
          </el-table-column>
        </NCC-table>
        <div v-if="!listLoading && !tableData.length" class="history-empty">
          <el-empty :image-size="56" description="暂无历史记录" />
        </div>
        <pagination
          :hidden="!total"
          :total="total"
          :page.sync="query.currentPage"
          :limit.sync="query.pageSize"
          @pagination="loadData"
        />
      </div>

      <div class="history-layout__preview">
        <div class="preview-card">
          <div class="preview-card__title">快照预览</div>
          <div class="preview-card__meta">
            <span>当前版本：{{ currentRow ? `V${currentRow.versionNo}` : '无' }}</span>
            <span v-if="currentRow">{{ currentRow.operateTypeText }}</span>
          </div>
          <el-scrollbar class="preview-card__scroll">
            <pre class="preview-card__code">{{ previewText }}</pre>
          </el-scrollbar>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { getAttendanceConfigHistory } from '@/api/extend/attendanceSetting'

export default {
  name: 'AttendanceConfigHistoryDialog',
  data() {
    return {
      visible: false,
      moduleType: '',
      bizId: '',
      historyTitle: '',
      listLoading: false,
      tableData: [],
      total: 0,
      currentRow: null,
      query: {
        currentPage: 1,
        pageSize: 10
      }
    }
  },
  computed: {
    previewText() {
      if (!this.currentRow || !this.currentRow.snapshotJson) {
        return '暂无快照数据'
      }
      try {
        return JSON.stringify(JSON.parse(this.currentRow.snapshotJson), null, 2)
      } catch (error) {
        return this.currentRow.snapshotJson
      }
    }
  },
  methods: {
    open({ moduleType, bizId, title }) {
      this.visible = true
      this.moduleType = moduleType
      this.bizId = bizId
      this.historyTitle = title || ''
      this.query.currentPage = 1
      this.query.pageSize = 10
      this.currentRow = null
      this.tableData = []
      this.total = 0
      this.loadData()
    },
    async loadData() {
      if (!this.moduleType || !this.bizId) return
      this.listLoading = true
      try {
        const res = await getAttendanceConfigHistory({
          moduleType: this.moduleType,
          bizId: this.bizId,
          currentPage: this.query.currentPage,
          pageSize: this.query.pageSize
        })
        const data = res.data || {}
        this.tableData = data.list || []
        this.total = data.pagination ? data.pagination.total : 0
        this.currentRow = this.tableData[0] || null
      } finally {
        this.listLoading = false
      }
    },
    handleCurrentChange(row) {
      this.currentRow = row || null
    },
    handleClose() {
      this.visible = false
      this.moduleType = ''
      this.bizId = ''
      this.historyTitle = ''
      this.tableData = []
      this.total = 0
      this.currentRow = null
    }
  }
}
</script>

<style lang="scss" scoped>
.history-head {
  margin-bottom: 12px;
}

.history-head__title {
  color: #303133;
  font-size: 16px;
  font-weight: 600;
}

.history-head__desc {
  margin-top: 4px;
  color: #909399;
  font-size: 12px;
}

.history-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(320px, 0.9fr);
  gap: 14px;
  min-height: 480px;
}

.history-layout__table,
.history-layout__preview {
  min-width: 0;
}

.history-empty {
  padding-top: 12px;
}

.preview-card {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 14px;
  border: 1px solid #ebeef5;
  border-radius: 12px;
  background: #fafbfd;
}

.preview-card__title {
  color: #303133;
  font-size: 14px;
  font-weight: 600;
}

.preview-card__meta {
  display: flex;
  justify-content: space-between;
  margin-top: 6px;
  color: #909399;
  font-size: 12px;
}

.preview-card__scroll {
  flex: 1;
  margin-top: 10px;
}

.preview-card__code {
  margin: 0;
  min-height: 100%;
  padding: 12px;
  border-radius: 10px;
  background: #1f2937;
  color: #f8fafc;
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}
</style>
