<template>
    <div class="monthly-add-form">
        <!-- 表单区域 -->
        <div class="form-section">
            <el-form
                ref="formRef"
                :model="formData"
                :rules="rules"
                label-width="140px"
                size="large"
                class="month-form"
            >
                <el-form-item label="计划执行日期:" prop="nowDate" class="month-item">
                    <el-date-picker
                        type="date"
                        style="width: 200px"
                        v-model="formData.nowDate"
                        value-format="YYYY-MM-DD"
                        placeholder="请选择计划日期"
                        @change="onHandleDateChange"
                    />
                </el-form-item>
            </el-form>

            <!-- 搜索区域 -->
            <div class="search-section">
                <el-input
                    clearable
                    @keyup.enter="onSearch"
                    class="search-input"
                    placeholder="输入关键字"
                    v-model.trim="formData.keyword"
                >
                    <template #prefix>
                        <el-icon class="search-icon" @click="onSearch">
                            <Search />
                        </el-icon>
                    </template>
                </el-input>
            </div>
        </div>

        <!-- 任务列表 -->
        <div class="table-section">
            <el-table
                :data="tableData"
                v-loading="loading"
                @selection-change="onSelectionChange"
                class="task-table"
                stripe
                border
                :header-cell-style="{ background: '#f5f7fa', color: '#606266', fontWeight: 600 }"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column
                    prop="projectName"
                    label="项目名称"
                    min-width="260"
                    align="center"
                    show-overflow-tooltip
                />
                <el-table-column
                    prop="workContent"
                    label="工作任务"
                    min-width="360"
                    show-overflow-tooltip
                    align="center"
                />
            </el-table>
        </div>
    </div>
</template>

<script setup name="MonthlyPlanAddForm">
import { ref, watch, getCurrentInstance, reactive } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { listDailyPlanPopupAPI } from '@/api/planMange/dailyPlan'
import dayjs from 'dayjs'

const { proxy } = getCurrentInstance()

const formRef = ref(null)
const loading = ref(false)
const tableData = ref([
    { name: '项目1', workTask: '工作任务1' },
    { name: '项目2', workTask: '工作任务2' },
])
const selectedRows = ref([])

const formData = ref({
    nowDate: dayjs().add(1, 'day').format('YYYY-MM-DD'), // 默认当天 + 1天
    keyword: '',
})

const rules = {
    nowDate: [{ required: true, message: '请选择计划执行日期', trigger: 'change' }],
}

// 搜索 / 刷新列表
const fetchData = async () => {
    await formRef.value.validate()
    loading.value = true
    try {
        const params = {
            nowDate: formData.value.nowDate,
            keyWord: formData.value.keyword,
        }
        const res = await listDailyPlanPopupAPI(params)
        tableData.value = res?.data
    } catch (e) {
        console.error('加载计划任务列表失败:', e)
        tableData.value = []
    } finally {
        loading.value = false
    }
}

const onSearch = () => {
    fetchData()
}

const onSelectionChange = (rows) => {
    selectedRows.value = rows
}

const onHandleDateChange = (value) => {
    fetchData()
}

// 暴露方法给父组件：获取选中任务、触发查询、校验
defineExpose({
    getSelectedTasks: () => selectedRows.value,
    getCurrentDate: () => formData.value.nowDate,
    search: fetchData,
    validate: async () => {
        await formRef.value.validate()
        if (selectedRows.value.length === 0) {
            proxy.$modal.msgError('请选择项目')
            return false
        }
        return true
    },
})

onMounted(() => {
    fetchData()
})
</script>

<style scoped lang="scss">
.monthly-add-form {
    padding: 0;
    padding-bottom: 30px;

    .form-section {
        margin-bottom: 20px;

        .month-form {
            margin-bottom: 16px;

            .month-item {
                margin-bottom: 0;

                :deep(.el-form-item__label) {
                    font-weight: 500;
                    color: #303133;

                    &::before {
                        content: '*';
                        color: #f56c6c;
                        margin-right: 4px;
                    }
                }

                :deep(.el-form-item__content) {
                    margin-left: 0 !important;
                }
            }
        }

        .search-section {
            display: flex;
            justify-content: flex-start;
            margin-top: 16px;

            .search-input {
                width: 300px;

                :deep(.el-input__wrapper) {
                    border-radius: 4px;
                    box-shadow: 0 0 0 1px #dcdfe6 inset;
                    transition: all 0.3s;

                    &:hover {
                        box-shadow: 0 0 0 1px #c0c4cc inset;
                    }

                    &.is-focus {
                        box-shadow: 0 0 0 1px #409eff inset;
                    }
                }

                .search-icon {
                    color: #909399;
                    cursor: pointer;
                    font-size: 16px;
                    transition: color 0.3s;

                    &:hover {
                        color: #409eff;
                    }
                }
            }
        }
    }

    .table-section {
        .task-table {
            border: 1px solid #ebeef5;
            border-radius: 4px;
            overflow: hidden;

            :deep(.el-table__header) {
                th {
                    padding: 12px 0;
                    font-size: 14px;
                }
            }

            :deep(.el-table__body) {
                td {
                    padding: 12px 0;
                    font-size: 14px;
                }

                tr {
                    &:hover {
                        background-color: #f5f7fa;
                    }
                }
            }

            :deep(.el-checkbox) {
                .el-checkbox__input.is-checked .el-checkbox__inner {
                    background-color: #409eff;
                    border-color: #409eff;
                }
            }
        }
    }
}
</style>
