<template>
    <!-- 小型弹窗，用于完成，删除，保存等操作 -->
    <el-dialog class="l-dialog" :class="lDialog" :title="title" :visible.sync="dialogVisible" :showClose="true"
        :closeOnClickModal="false" @close="handleClose" :append-to-body="true">
        <div class="dialog-content-scrollable">
            <el-form :model="form" :rules="rules" ref="ruleForm" label-width="110px" label-position="top">
                <el-form-item label="项目名称" prop="proName">
                    <el-input v-model.trim="form.proName" placeholder="请输入项目名称" clearable show-word-limit
                        maxlength="64"></el-input>
                </el-form-item>
                <el-form-item label="项目编号" prop="项目编号">
                    <el-input v-model.trim="form.proCode" placeholder="请输入项目编号" clearable show-word-limit
                        maxlength="32"></el-input>
                </el-form-item>
                <el-form-item label="招标人" prop="tenderer">
                    <el-input v-model.trim="form.tenderer" placeholder="请输入招标人" clearable show-word-limit
                        maxlength="32"></el-input>
                </el-form-item>
                <el-form-item label="代理机构" prop="agency">
                    <el-input v-model.trim="form.agency" placeholder="请输入代理机构" clearable show-word-limit
                        maxlength="64"></el-input>
                </el-form-item>
                <el-form-item label="开标时间" prop="bidOpeningTime" >
                    <el-date-picker class="form-item"  v-model="form.bidOpeningTime" type="date" placeholder="请选择开标时间" value-format="yyyy-MM-dd HH:mm:ss" clearable></el-date-picker>
                </el-form-item>
                <el-form-item label="开标方式" prop="bidOpeningMethod">
                    <el-input v-model.trim="form.bidOpeningMethod" placeholder="请输入开标方式" clearable show-word-limit
                        maxlength="128"></el-input>
                </el-form-item>
                <el-form-item label="项目简介" prop="proIntroduction">
                    <el-input type="textarea" v-model.trim="form.proIntroduction" placeholder="请输入项目简介" clearable show-word-limit
                    :autosize="{ minRows: 5, maxRows: 8 }" maxlength="512"></el-input>
                </el-form-item>

            </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" class="confirm-btn" @click="submitForm('ruleForm')">确认</el-button>
            <el-button class="cancel-btn" @click="handleClose">取消</el-button>
        </span>
    </el-dialog>
</template>
<script>
import _ from 'lodash'
import { editProDataAPI } from '@/api/analysis/analysis'
export default {
    name: 'ProForm',
    props: ['width', 'rowData', 'title'],
    data() {
        return {
            lDialog: this.width > 500 ? 'w700' : 'w500',
            dialogVisible: true,
            form: {

            },
            rules: {
                proName: [
                    {
                        required: true,
                        message: '请输入项目名称',
                        trigger: 'blur',
                    },
                ],
                proCode: [
                    {
                        required: true,
                        message: '请输入项目编号',
                        trigger: 'blur',
                    },
                ],
                tenderer: [
                    {
                        required: true,
                        message: '请输入招标人',
                        trigger: 'blur',
                    },
                ],
                agency: [
                    {
                        required: true,
                        message: '请输入代理机构',
                        trigger: 'blur',
                    },
                ],
                bidOpeningTime: [
                    {
                        required: true,
                        message: '请选择开标时间',
                        trigger: 'change',
                    },
                ],
                bidOpeningMethod: [
                    {
                        required: true,
                        message: '请输入开标方式',
                        trigger: 'blur',
                    },
                ],
                proIntroduction: [
                    {
                        required: true,
                        message: '请输入项目简介',
                        trigger: 'blur',
                    },
                ],

            },
        }
    },
    created() {
        this.initFormData()
    },

    methods: {
        /* 初始化表单数据 */
        initFormData() {
            console.log(this.rowData);

            this.form = {
                proId: this.rowData.proId || null,
                proName: this.rowData.proName || '',
                proCode: this.rowData.proCode || '',
                tenderer: this.rowData.tenderer || '',
                agency: this.rowData.agency || '',
                bidOpeningTime: this.rowData.bidOpeningTime || '',
                bidOpeningMethod: this.rowData.bidOpeningMethod || '',
                proIntroduction: this.rowData.proIntroduction || '',
            }
        },

        /*关闭弹窗 */
        handleClose() {
            this.dialogVisible = false
            this.$emit('closeDialog')
        },
        /**确认弹窗 */
        sureBtnClick() {
            this.dialogVisible = false
            this.$emit('closeDialog')
        },
        /**重置表单*/
        reset() {
            this.form = {
                proId: null,
                proName: '',
                proCode: '',
                tenderer: '',
                agency: '',
                bidOpeningTime: '',
                bidOpeningMethod: '',
                proIntroduction: '',
            }
            this.resetForm('ruleForm')
        },
        handleReuslt(res) {
            this.$modal.msgSuccess(res.msg)
            const newData = {...this.form};
            this.$emit('handleProQuery',newData)
            this.reset()
            this.handleClose()
        },
        validate(formName) {
            return new Promise((resolve, reject) => {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        resolve(this.form) // 校验成功返回表单数据
                    } else {
                        reject(new Error('数据未填写完整'))
                    }
                })
            })
        },
        /**验证 */
        async submitForm(formName) {
            try {
                const data = await this.validate(formName)
                
                // 所有校验通过，组装完整数据
                let formData = {
                    ...data,
                }
                // 显示遮罩层
                this.loading = this.$loading({
                    lock: true,
                    text: '数据提交中，请稍候...',
                    background: 'rgba(0,0,0,0.5)',
                    target:
                        this.$el.querySelector('.el-dialog') || document.body,
                })
                console.log('所有表单校验通过，完整数据:', formData)
                const res = await this.saveData(formData)
                if (res.code === 200) {
                    this.handleReuslt(res)
                } else {
                    this.$modal.msgError(res.msg)
                }
            } catch (error) {
            } finally {
                if (this.loading) {
                    this.loading.close()
                }
            }
        },

        // 保存接口
        async saveData(formData) {
            return new Promise((resolve, reject) => {
                // 修改
                editProDataAPI(formData)
                    .then((res) => {
                        resolve(res)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },
    },
}
</script>
<style lang="scss" scoped>
.w700 ::v-deep .el-dialog {
    width: 700px;
    font-family: Source Han Sans CN, Source Han Sans CN;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    margin-top: 5vh !important;
    margin-bottom: 5vh !important;
}

.w500 ::v-deep .el-dialog {
    width: 500px;
    font-family: Source Han Sans CN, Source Han Sans CN;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    margin-top: 5vh !important;
    margin-bottom: 5vh !important;
}

// 弹窗主体区域
.w500 ::v-deep .el-dialog__body,
.w700 ::v-deep .el-dialog__body {
    flex: 1;
    overflow: hidden;
    padding: 20px;
    display: flex;
    flex-direction: column;
}

.w500 ::v-deep .el-dialog__header,
.w700 ::v-deep .el-dialog__header {
    flex-shrink: 0;
    border-bottom: 1px solid #EBEEF5;

    .el-dialog__title {
        font-size: 16px;
    }
}

.yxq .el-range-separator {
    margin-right: 7px !important;
}

.el-date-editor--daterange.el-input__inner {
    width: 260px;
}

.form-item {
    width: 100%;
}

.select-style {
    display: flex;
    justify-content: space-between;
}

.confirm-btn {
    width: 98px;
    height: 36px;
    background: #1f72ea;
    box-shadow: 0px 4px 8px 0px rgba(51, 135, 255, 0.5);
    border-radius: 4px 4px 4px 4px;
    color: #fff;
    border: none;
    font-size: 14px;
    transition: all 0.3s;

    &:hover {
        background: #4a8bff;
        box-shadow: 0px 6px 12px 0px rgba(51, 135, 255, 0.6);
    }
}

.cancel-btn {
    width: 98px;
    height: 36px;
    background: #e5e5e5;
    box-shadow: 0px 4px 8px 0px rgba(76, 76, 76, 0.2);
    border-radius: 4px 4px 4px 4px;
    color: #333;
    border: none;
    font-size: 14px;
    transition: all 0.3s;

    &:hover {
        background: #d0d0d0;
        box-shadow: 0px 6px 12px 0px rgba(76, 76, 76, 0.3);
    }
}

::v-deep .el-dialog__footer {
    text-align: center;
    flex-shrink: 0;
}

// 内容区域可滚动
.dialog-content-scrollable {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding-right: 6px;
    max-height: calc(90vh - 120px); // 减去头部和底部的高度

    // 自定义滚动条样式
    &::-webkit-scrollbar {
        width: 6px;
    }

    &::-webkit-scrollbar-track {
        background: transparent;
    }

    &::-webkit-scrollbar-thumb {
        background: rgba(0, 0, 0, 0.2);
        border-radius: 3px;

        &:hover {
            background: rgba(0, 0, 0, 0.3);
        }
    }
}
</style>
