<template>
  <div>
    <AppHeader />
    
    <div class="container-fluid py-4">
      <div class="mb-4">
        <NuxtLink to="/report/weekly" class="text-decoration-none">
          <i class="bi bi-arrow-left me-1"></i> 목록으로
        </NuxtLink>
      </div>

      <div class="row">
        <div class="col-lg-8">
          <div class="card">
            <div class="card-header">
              <h5 class="mb-0">
                <i class="bi bi-pencil-square me-2"></i>
                {{ isEdit ? '주간보고 수정' : '주간보고 작성' }}
              </h5>
            </div>
            <div class="card-body">
              <form @submit.prevent="handleSubmit">
                <!-- 프로젝트 선택 -->
                <div class="mb-3">
                  <label class="form-label">프로젝트 <span class="text-danger">*</span></label>
                  <select class="form-select" v-model="form.projectId" required :disabled="isEdit">
                    <option value="">프로젝트를 선택하세요</option>
                    <option v-for="p in projects" :key="p.projectId" :value="p.projectId">
                      {{ p.projectName }}
                    </option>
                  </select>
                </div>

                <!-- 주차 선택 -->
                <div class="row mb-3">
                  <div class="col-md-4">
                    <label class="form-label">연도</label>
                    <select class="form-select" v-model="form.reportYear" :disabled="isEdit">
                      <option v-for="y in years" :key="y" :value="y">{{ y }}년</option>
                    </select>
                  </div>
                  <div class="col-md-4">
                    <label class="form-label">주차</label>
                    <select class="form-select" v-model="form.reportWeek" :disabled="isEdit">
                      <option v-for="w in 53" :key="w" :value="w">
                        W{{ String(w).padStart(2, '0') }}
                      </option>
                    </select>
                  </div>
                  <div class="col-md-4">
                    <label class="form-label">기간</label>
                    <input type="text" class="form-control" :value="weekRangeText" readonly />
                  </div>
                </div>

                <!-- 금주 실적 -->
                <div class="mb-3">
                  <label class="form-label">금주 실적 <span class="text-danger">*</span></label>
                  <textarea 
                    class="form-control" 
                    v-model="form.workDescription" 
                    rows="5"
                    placeholder="이번 주에 수행한 업무 내용을 작성해주세요."
                    required
                  ></textarea>
                </div>

                <!-- 차주 계획 -->
                <div class="mb-3">
                  <label class="form-label">차주 계획</label>
                  <textarea 
                    class="form-control" 
                    v-model="form.planDescription" 
                    rows="4"
                    placeholder="다음 주에 수행할 업무 계획을 작성해주세요."
                  ></textarea>
                </div>

                <!-- 이슈사항 -->
                <div class="mb-3">
                  <label class="form-label">이슈/리스크</label>
                  <textarea 
                    class="form-control" 
                    v-model="form.issueDescription" 
                    rows="3"
                    placeholder="업무 진행 중 발생한 이슈나 리스크를 작성해주세요."
                  ></textarea>
                </div>

                <!-- 비고 -->
                <div class="mb-3">
                  <label class="form-label">비고</label>
                  <textarea 
                    class="form-control" 
                    v-model="form.remarkDescription" 
                    rows="2"
                    placeholder="기타 참고사항"
                  ></textarea>
                </div>

                <!-- 투입시간 -->
                <div class="mb-4">
                  <label class="form-label">투입 시간 (선택)</label>
                  <div class="input-group" style="max-width: 200px;">
                    <input 
                      type="number" 
                      class="form-control" 
                      v-model="form.workHours"
                      min="0" 
                      max="100" 
                      step="0.5"
                    />
                    <span class="input-group-text">시간</span>
                  </div>
                </div>

                <!-- 버튼 -->
                <div class="d-flex gap-2">
                  <button type="submit" class="btn btn-primary" :disabled="isSubmitting">
                    <span v-if="isSubmitting" class="spinner-border spinner-border-sm me-1"></span>
                    <i class="bi bi-save me-1" v-else></i>
                    {{ isEdit ? '수정' : '저장' }}
                  </button>
                  <button 
                    type="button" 
                    class="btn btn-success" 
                    @click="handleSaveAndSubmit"
                    :disabled="isSubmitting"
                  >
                    <i class="bi bi-send me-1"></i> 저장 후 제출
                  </button>
                  <NuxtLink to="/report/weekly" class="btn btn-outline-secondary">
                    취소
                  </NuxtLink>
                </div>
              </form>
            </div>
          </div>
        </div>

        <!-- 사이드 안내 -->
        <div class="col-lg-4">
          <div class="card bg-light">
            <div class="card-body">
              <h6><i class="bi bi-info-circle me-1"></i> 작성 안내</h6>
              <ul class="mb-0 ps-3">
                <li>금주 실적은 필수 항목입니다.</li>
                <li>같은 프로젝트, 같은 주차에 하나의 보고서만 작성 가능합니다.</li>
                <li>제출 후에는 수정이 제한됩니다.</li>
                <li>취합은 매주 자동으로 진행됩니다.</li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
const { fetchCurrentUser } = useAuth()
const { getCurrentWeekInfo, getWeekRangeText } = useWeekCalc()
const router = useRouter()
const route = useRoute()

const currentWeek = getCurrentWeekInfo()
const currentYear = new Date().getFullYear()
const years = [currentYear, currentYear - 1]

const isEdit = computed(() => !!route.query.id)
const isSubmitting = ref(false)

const form = ref({
  projectId: '',
  reportYear: currentWeek.year,
  reportWeek: currentWeek.week,
  workDescription: '',
  planDescription: '',
  issueDescription: '',
  remarkDescription: '',
  workHours: null as number | null
})

const projects = ref<any[]>([])

const weekRangeText = computed(() => {
  return getWeekRangeText(form.value.reportYear, form.value.reportWeek)
})

onMounted(async () => {
  const user = await fetchCurrentUser()
  if (!user) {
    router.push('/login')
    return
  }
  
  await loadProjects()
  
  if (route.query.id) {
    await loadReport(Number(route.query.id))
  }
})

async function loadProjects() {
  try {
    const res = await $fetch<{ projects: any[] }>('/api/project/list')
    projects.value = res.projects || []
  } catch (e) {
    console.error('Load projects error:', e)
  }
}

async function loadReport(reportId: number) {
  try {
    const res = await $fetch<{ report: any }>(`/api/report/weekly/${reportId}/detail`)
    const r = res.report
    form.value = {
      projectId: r.projectId,
      reportYear: r.reportYear,
      reportWeek: r.reportWeek,
      workDescription: r.workDescription || '',
      planDescription: r.planDescription || '',
      issueDescription: r.issueDescription || '',
      remarkDescription: r.remarkDescription || '',
      workHours: r.workHours
    }
  } catch (e: any) {
    alert('보고서를 불러오는데 실패했습니다.')
    router.push('/report/weekly')
  }
}

async function handleSubmit() {
  if (!form.value.projectId || !form.value.workDescription) {
    alert('프로젝트와 금주 실적은 필수입니다.')
    return
  }
  
  isSubmitting.value = true
  
  try {
    if (isEdit.value) {
      await $fetch(`/api/report/weekly/${route.query.id}/update`, {
        method: 'PUT',
        body: form.value
      })
    } else {
      await $fetch('/api/report/weekly/create', {
        method: 'POST',
        body: form.value
      })
    }
    router.push('/report/weekly')
  } catch (e: any) {
    alert(e.data?.message || e.message || '저장에 실패했습니다.')
  } finally {
    isSubmitting.value = false
  }
}

async function handleSaveAndSubmit() {
  if (!form.value.projectId || !form.value.workDescription) {
    alert('프로젝트와 금주 실적은 필수입니다.')
    return
  }
  
  if (!confirm('저장 후 바로 제출하시겠습니까?\n제출 후에는 수정이 제한됩니다.')) return
  
  isSubmitting.value = true
  
  try {
    let reportId: number
    
    if (isEdit.value) {
      await $fetch(`/api/report/weekly/${route.query.id}/update`, {
        method: 'PUT',
        body: form.value
      })
      reportId = Number(route.query.id)
    } else {
      const res = await $fetch<{ report: any }>('/api/report/weekly/create', {
        method: 'POST',
        body: form.value
      })
      reportId = res.report.reportId
    }
    
    // 제출
    await $fetch(`/api/report/weekly/${reportId}/submit`, { method: 'POST' })
    router.push('/report/weekly')
  } catch (e: any) {
    alert(e.data?.message || e.message || '처리에 실패했습니다.')
  } finally {
    isSubmitting.value = false
  }
}
</script>
