<!--
  -
  - Copyright (c) 2025. Jeffery Hsu - Achievement Report Generator
  -
  - This program is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - This program is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with this program. If not, see <https://www.gnu.org/licenses/>.
  -
  -->

<script setup lang="ts">
import { Edit, Plus } from '@element-plus/icons-vue'
import { IAssessment, IAssessmentMethod } from '@renderer/types'
import { computed, reactive, Ref, ref } from 'vue'
import { Operation } from '@renderer/types/share'
import { useGlobalStore } from '@renderer/store'
import { AssessmentData, AssessmentMethod } from '@renderer/models/Assessment'

type Parent = 'n' | 'p' | 'f'

type MethodStatus = 'ok' | 'weight-fail'

// const props = defineProps<{
//   assessment: IAssessment
//   parent: Parent
//   title: string
// }>()

const emits = defineEmits<{
  (e: 'edit', parent: Parent): void
  (e: 'add', parent: Parent): void
  (e: 'modify', item: IAssessmentMethod, parent: Parent): void
  (e: 'delete', item: IAssessmentMethod, parent: Parent): void
}>()

const store = useGlobalStore()
const stage = ref<{ title: string; assessment: IAssessment }[]>([
  {
    title: '平时考核',
    assessment: reactive(store.doc.assessment.daily)
  },
  {
    title: '过程考核',
    assessment: reactive(store.doc.assessment.progress)
  },
  {
    title: '期末考核',
    assessment: reactive(store.doc.assessment.final)
  }
])

// const isShowStageEditor = ref(false)
// const isShowMethodEditor = ref(false)

// const tempAssessmentMethod = ref<IAssessmentMethod>(AssessmentMethod.factoryToImpl())
// const tempStage = ref<IAssessment>(AssessmentData.factoryToImpl())

const status = computed((): MethodStatus => {
  let total = 0
  props.assessment.method.forEach((v) => (total += v.weight))

  console.log(total)
  if (total !== 100) {
    return 'weight-fail'
  }

  return 'ok'
})

// const addAssessmentMethodSubmit = computed(
//   () => !Object.values(tempAssessmentMethod.value).every((v) => v)
// )

// const checkAllStageWeightSum = computed(() => {
//   return normalStage.value.weight + processStage.value.weight + finalStage.value.weight === 100
// })

// const handleShow
</script>

<template>
  <div class="pb-4">
    <!-- <el-alert v-show="!checkAllStageWeightSum" type="warning" show-icon :closable="false"
      >所有考核占比之和不是100%</el-alert
    > -->
  </div>

  <el-descriptions
    v-for="(item, idx) in stage"
    :key="item.title"
    class="px-8 pb-4"
    border
    :column="3"
  >
    <template #title>
      <p>
        {{ item.title }}
        <span v-if="!item.assessment.stage">（待编辑）</span>
        <span v-else>（通过 {{ item.assessment.stage }}，占比 {{ item.assessment.weight }}%）</span>
      </p>
    </template>
    <template #extra>
      <el-button :icon="Plus" type="primary" @click="emits('add', parent)">添加</el-button>
      <el-button :icon="Edit" @click="emits('edit', parent)">编辑</el-button>
    </template>
    <div v-for="method in item.assessment.method" :key="method.method">
      <el-descriptions-item label="考核方式">{{ method.method }}</el-descriptions-item>
      <el-descriptions-item label="占比">{{ method.weight }}%</el-descriptions-item>
      <el-descriptions-item label="操作">
        <el-button @click="emits('modify', method, parent)">修改</el-button>
        <el-button type="danger" @click="emits('delete', method, parent)">删除</el-button>
      </el-descriptions-item>
    </div>

    <el-descriptions-item v-if="assessment.method.length === 0" label="无数据">
      N/A
    </el-descriptions-item>
    <el-descriptions-item v-else label="备注">
      <span v-if="status === 'ok'" class="text-green-500">准备就绪</span>
      <span v-else-if="status === 'weight-fail'" class="text-red-500">
        需要注意：所有占比不足 <strong>100%</strong>
      </span>
    </el-descriptions-item>
  </el-descriptions>

  <!-- <el-dialog
    v-model="isShowMethodEditor"
    title="添加考核方式"
    :show-close="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form v-model="tempAssessmentMethod" label-width="auto">
      <el-form-item label="考核方式">
        <el-autocomplete v-model="tempAssessmentMethod.method" clearable />
      </el-form-item>
      <el-form-item label="权重">
        <el-input v-model="tempAssessmentMethod.weight" type="number" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button
        :disabled="addAssessmentMethodSubmit"
        type="primary"
        @click="handleAddAssessmentMethod('save')"
      >
        添加
      </el-button>
      <el-button @click="handleAddAssessmentMethod('cancel')">取消</el-button>
    </template>
  </el-dialog>

  <el-dialog v-model="isShowStageEditor" title="编辑考核方式">
    <el-form v-model="tempAssessment" label-width="auto">
      <el-form-item label="考核方式">
        <el-autocomplete v-model="tempAssessment.stage" clearable />
      </el-form-item>
      <el-form-item label="权重">
        <el-input v-model="tempAssessment.weight" type="number" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button
        type="primary"
        @click="handleEditAssessmentSubmit('save', assessmentParent ?? 'n')"
      >
        保存
      </el-button>
      <el-button @click="handleEditAssessmentSubmit('cancel', assessmentParent ?? 'n')">
        取消
      </el-button>
    </template>
  </el-dialog> -->
</template>

<style scoped></style>
