<template>
  <div class="print-container" id="print-container" ref="printContainerRef">
    <div class="print-content" v-loading="loading" ref="printContentRef">
      <!-- 上半部分：存根 -->
      <div class="stub-section">
        <!-- 大标题 -->
        <div class="main-title">江苏省常州技师学院调令</div>
        
        <!-- 副标题和编号 -->
        <div class="subtitle-row">
          <div class="subtitle">
            <div class="subtitle-text">校内调配通知单</div>
            <div class="stub-label">(存根)</div>
          </div>
          <div class="doc-number">编号：</div>
        </div>
        
        <!-- 存根内容 -->
        <div class="stub-content">
          <div class="content-line">
            <span class="underline-field">{{ detailData.fromDeptName || '________' }}</span>
            <span class="text">(院、部、中心、处室):</span>
          </div>
          
          <div class="content-line indent">
            <span class="text">经学校研究决定,调配</span>
            <span class="underline-field">{{ detailData.realName || '________' }}</span>
            <span class="text">同志到</span>
            <span class="underline-field">{{ detailData.toDeptName1 || '________' }}</span>
            <span class="text">(院、部、中心、处室)</span>
            <span class="underline-field">{{ detailData.pos || '________' }}</span>
            <span class="text">(专技、管理、工勤)岗位工作,请做好移交或安排。</span>
          </div>
          
          <div class="content-line">
            <span class="text">限</span>
            <span class="underline-field">{{ detailData.year1 || '____' }}</span>
            <span class="text">年</span>
            <span class="underline-field">{{ detailData.month1 || '____' }}</span>
            <span class="text">月</span>
            <span class="underline-field">{{ detailData.day1 || '____' }}</span>
            <span class="text">日前离岗。</span>
          </div>
        </div>
        
        <!-- 存根底部 -->
        <div class="stub-footer">
          <div class="footer-right">
            <div class="dept-name">组织人事处</div>
            <div class="date-line">
              <span class="underline-field">{{ detailData.year || '____' }}</span>
              <span class="text">年</span>
              <span class="underline-field">{{ detailData.month || '____' }}</span>
              <span class="text">月</span>
              <span class="underline-field">{{ detailData.day || '____' }}</span>
              <span class="text">日</span>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 分隔线 -->
      <div class="divider-line"></div>
      
      <!-- 下半部分：正式通知单 -->
      <div class="notice-section">
        <!-- 顶部横线 -->
        <div class="top-line"></div>
        
        <!-- 标题和编号 -->
        <div class="notice-title-row">
          <div class="notice-title">校内调配通知单</div>
          <div class="doc-number">编号：</div>
        </div>
        
        <!-- 通知单内容 -->
        <div class="notice-content">
          <div class="content-line">
            <span class="underline-field">{{ detailData.fromDeptName || '________' }}</span>
            <span class="text">(院、部、中心、处室):</span>
          </div>
          
          <div class="content-line indent">
            <span class="text">经学校研究决定,调配</span>
            <span class="underline-field">{{ detailData.realName || '________' }}</span>
            <span class="text">同志到</span>
            <span class="underline-field">{{ detailData.toDeptName1 || '________' }}</span>
            <span class="text">(院、部、中心、处室)</span>
            <span class="underline-field">{{ detailData.pos || '________' }}</span>
            <span class="text">(专技、管理、工勤)岗位工作,请做好工作安排。</span>
          </div>
          
          <div class="content-line">
            <span class="text">限</span>
            <span class="underline-field">{{ detailData.year1 || '____' }}</span>
            <span class="text">年</span>
            <span class="underline-field">{{ detailData.month1 || '____' }}</span>
            <span class="text">月</span>
            <span class="underline-field">{{ detailData.day1 || '____' }}</span>
            <span class="text">日前报到,并将该单交财务处。</span>
          </div>
        </div>
        
        <!-- 通知单底部 -->
        <div class="notice-footer">
          <div class="footer-center">
            <div class="dept-name-bold">组织人事处</div>
            <div class="date-line">
              <span class="underline-field">{{ detailData.year || '____' }}</span>
              <span class="text">年</span>
              <span class="underline-field">{{ detailData.month || '____' }}</span>
              <span class="text">月</span>
              <span class="underline-field">{{ detailData.day || '____' }}</span>
              <span class="text">日</span>
            </div>
          </div>
          
          <!-- 签字栏 -->
          <div class="signature-section">
            <div class="signature-left">
              <div class="signature-label">调配(出)部门领导签字/日期:</div>
            </div>
            <div class="signature-right">
              <div class="signature-label">接收部门领导签字/日期:</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 打印按钮 -->
    <div class="print-actions" v-if="!loading">
      <el-button type="primary" icon="Printer" @click="handlePrint">打印</el-button>
      <el-button @click="handleClose">关闭</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { print } from '/@/api/professional/professionaluser/professionalteacherstationchange'
import { useMessage } from '/@/hooks/message'

const route = useRoute()
const router = useRouter()
const message = useMessage()

const loading = ref(false)
const detailData = ref<any>({})
const printContainerRef = ref<HTMLElement>()
const printContentRef = ref<HTMLElement>()

// 解析日期
const parseDate = (dateStr: string) => {
  if (!dateStr) return { year: '', month: '', day: '' }
  const date = new Date(dateStr)
  return {
    year: date.getFullYear().toString(),
    month: (date.getMonth() + 1).toString().padStart(2, '0'),
    day: date.getDate().toString().padStart(2, '0')
  }
}

// 签发日期
const issueDate = computed(() => {
  return parseDate(detailData.value.changeDate || '')
})

const issueYear = computed(() => issueDate.value.year)
const issueMonth = computed(() => issueDate.value.month)
const issueDay = computed(() => issueDate.value.day)

// 报到日期（假设是调令日期，可根据实际需求调整）
const reportDate = computed(() => {
  return parseDate(detailData.value.changeDate || '')
})

const reportYear = computed(() => reportDate.value.year)
const reportMonth = computed(() => reportDate.value.month)
const reportDay = computed(() => reportDate.value.day)

// 获取详情数据
const fetchDetail = async () => {
  // 优先从 params 获取，如果没有则从 query 获取
  const id = (route.params.id || route.query.id) as string
  if (!id) {
    message.error('缺少调令ID')
    return
  }
  
  loading.value = true
  try {
    const response = await print(id)
    if (response && response.data) {
      detailData.value = response.data
    } else {
      message.error('获取调令信息失败')
    }
  } catch (error) {
    console.error('获取调令详情失败:', error)
    message.error('获取调令信息失败')
  } finally {
    loading.value = false
  }
}

// 打印
const handlePrint = () => {
  // 使用 ref 直接操作元素
  if (printContainerRef.value) {
    printContainerRef.value.style.display = 'flex'
    printContainerRef.value.style.visibility = 'visible'
    printContainerRef.value.style.opacity = '1'
  }
  if (printContentRef.value) {
    printContentRef.value.style.display = 'block'
    printContentRef.value.style.visibility = 'visible'
    printContentRef.value.style.opacity = '1'
  }
  
  // 隐藏左侧菜单等元素
  const asideElements = document.querySelectorAll('.layout-aside, .layout-header, .layout-navbars, .layout-tags-view, .layout-breadcrumb')
  asideElements.forEach((el: any) => {
    if (el) {
      el.style.display = 'none'
    }
  })
  
  // 调整主内容区
  const mainElements = document.querySelectorAll('.layout-main')
  mainElements.forEach((el: any) => {
    if (el) {
      el.style.marginLeft = '0'
      el.style.width = '100%'
    }
  })
  
  // 延迟打印，确保样式已应用
  setTimeout(() => {
    window.print()
  }, 200)
}

// 关闭
const handleClose = () => {
  router.back()
}

// 初始化
onMounted(() => {
  fetchDetail()
})
</script>

<!-- 全局打印样式，用于隐藏 layout 元素 -->
<style lang="scss">
/* 打印时隐藏左侧菜单和其他 layout 元素 */
@media print {
  /* 只隐藏左侧菜单和头部，不影响其他内容 */
  .layout-aside,
  .layout-columns-aside,
  .layout-header,
  .layout-navbars,
  .layout-tags-view,
  .layout-breadcrumb,
  .el-backtop {
    display: none !important;
  }
  
  /* 确保主内容区全宽 */
  .layout-main {
    margin-left: 0 !important;
    width: 100% !important;
  }
  
  /* 隐藏打印按钮 */
  .print-actions {
    display: none !important;
  }
}
</style>

<style lang="scss">
.print-container {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.print-content {
  background: #fff;
  width: 100%;
  max-width: 800px;
  padding: 40px 60px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
  font-size: 16px;
  line-height: 1.8;
}

/* 上半部分：存根 */
.stub-section {
  margin-bottom: 30px;
}

.main-title {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
  margin-bottom: 20px;
  letter-spacing: 2px;
}

.subtitle-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 30px;
}

.subtitle {
  flex: 1;
  text-align: center;
}

.subtitle-text {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 5px;
}

.stub-label {
  font-size: 14px;
  color: #666;
}

.doc-number {
  font-size: 16px;
  white-space: nowrap;
}

.stub-content {
  margin-bottom: 30px;
}

.content-line {
  margin-bottom: 15px;
  line-height: 2;
  
  &.indent {
    padding-left: 2em;
  }
}

.underline-field {
  display: inline-block;
  min-width: 80px;
  border-bottom: 1px solid #333;
  text-align: center;
  padding: 0 5px;
  margin: 0 3px;
  font-weight: 500;
}

.text {
  color: #333;
}

.stub-footer {
  margin-top: 40px;
  text-align: right;
}

.footer-right {
  display: inline-block;
}

.footer-center {
  text-align: center;
}

.dept-name {
  font-size: 16px;
  margin-bottom: 10px;
}

.dept-name-bold {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}

.date-line {
  font-size: 16px;
}

/* 分隔线 */
.divider-line {
  width: 100%;
  height: 1px;
  background: #ddd;
  margin: 30px 0;
  border-top: 1px dashed #999;
}

/* 下半部分：正式通知单 */
.notice-section {
  margin-top: 30px;
}

.top-line {
  width: 100%;
  height: 1px;
  background: #333;
  margin-bottom: 20px;
}

.notice-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.notice-title {
  flex: 1;
  text-align: center;
  font-size: 18px;
  font-weight: 500;
}

.notice-content {
  margin-bottom: 40px;
}

.notice-footer {
  margin-top: 50px;
}

.signature-section {
  display: flex;
  justify-content: space-between;
  margin-top: 60px;
  padding-top: 20px;
  border-top: 1px solid #ddd;
}

.signature-left,
.signature-right {
  flex: 1;
}

.signature-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 40px;
}

.print-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

/* 打印样式 */
@media print {
  /* 打印容器样式 - 确保正常显示 */
  .print-container {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    background: #fff !important;
    padding: 20px !important;
    margin: 0 !important;
    width: 100% !important;
    min-height: auto !important;
  }
  
  .print-content {
    display: block !important;
    background: #fff !important;
    box-shadow: none !important;
    padding: 40px 60px !important;
    margin: 0 0 20px 0 !important;
    max-width: 800px !important;
    width: 100% !important;
    font-size: 16px !important;
    line-height: 1.8 !important;
  }
  
  .divider-line {
    margin: 20px 0;
  }
  
  /* 隐藏打印按钮 */
  .print-actions {
    display: none !important;
  }
  
  @page {
    size: A4;
    margin: 1.5cm;
  }
  
  /* 确保上下两部分在同一页 */
  .stub-section,
  .notice-section {
    page-break-inside: avoid;
  }
}
</style>
