<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="markName">
                    <el-input v-model.trim="form.markName" placeholder="请输入标的名称" clearable show-word-limit
                        maxlength="64"></el-input>
                </el-form-item>
                <el-form-item label="单位" prop="unit">
                    <el-input v-model.trim="form.unit" placeholder="请输入单位" clearable show-word-limit
                        maxlength="64"></el-input>
                </el-form-item>
                <el-form-item label="标段编号" prop="bidNumber">
                    <el-input v-model.trim="form.bidNumber" placeholder="请输入标段编号" clearable show-word-limit
                        maxlength="32"></el-input>
                </el-form-item>
                <el-form-item label="标段名称" prop="bidName">
                    <el-input v-model.trim="form.bidName" placeholder="请输入标段名称" clearable show-word-limit
                        maxlength="64"></el-input>
                </el-form-item>
                <el-form-item label="最高投标限价（万元）" prop="maximumBidLimit">
                    <el-input v-model.trim="form.maximumBidLimit" placeholder="请输入最高投标限价（万元）" clearable show-word-limit
                        maxlength="64"></el-input>
                </el-form-item>
                <el-form-item label="安全文明施工费（万元）" prop="safetyConstFee">
                    <el-input v-model.trim="form.safetyConstFee" placeholder="请输入安全文明施工费（万元）" clearable show-word-limit
                        maxlength="64"></el-input>
                </el-form-item>
                <el-form-item label="投标保证金（万元）" prop="bidBond">
                    <el-input v-model.trim="form.bidBond" placeholder="请输入投标保证金（万元）" clearable show-word-limit
                        maxlength="64"></el-input>
                </el-form-item>
                <el-form-item label="招标阶段" prop="biddingStage">
                    <el-input v-model.trim="form.biddingStage" placeholder="请输入招标阶段" clearable show-word-limit
                        maxlength="32"></el-input>
                </el-form-item>
                <el-form-item label="工期" prop="duration">
                    <el-input type="textarea" v-model.trim="form.duration" placeholder="请输入工期" clearable show-word-limit
                    :autosize="{ minRows: 3, maxRows: 5 }" maxlength="128"></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 { editBidDataAPI } from '@/api/analysis/analysis'
export default {
    name: 'BidForm',
    props: ['width', 'rowData', 'title'],
    data() {
        return {
            lDialog: this.width > 500 ? 'w700' : 'w500',
            dialogVisible: true,
            form: {

            },
            rules: {
                markName: [
                    {
                        required: true,
                        message: '请输入标的名称',
                        trigger: 'blur',
                    },
                ],
                unit: [
                    {
                        required: true,
                        message: '请输入单位',
                        trigger: 'blur',
                    },
                ],
                bidNumber: [
                    {
                        required: true,
                        message: '请输入标段编号',
                        trigger: 'blur',
                    },
                ],
                bidName: [
                    {
                        required: true,
                        message: '请输入标段名称',
                        trigger: 'blur',
                    },
                ],
                maximumBidLimit: [
                    {
                        required: true,
                        message: '请输入最高投标限价（万元）',
                        trigger: 'blur',
                    },
                ],
                safetyConstFee: [
                    {
                        required: true,
                        message: '请输入安全文明施工费（万元）',
                        trigger: 'blur',
                    },
                ],
                bidBond: [
                    {
                        required: true,
                        message: '请输入投标保证金（万元）',
                        trigger: 'blur',
                    },
                ],
                biddingStage: [
                    {
                        required: true,
                        message: '请输入招标阶段',
                        trigger: 'blur',
                    },
                ],
                duration: [
                    {
                        required: true,
                        message: '请输入工期',
                        trigger: 'blur',
                    },
                ],
            },
        }
    },
    created() {
        this.initFormData()
    },

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

            this.form = {
                bidId: this.rowData.bidId || null,
                markName: this.rowData.markName || '',
                unit: this.rowData.unit || '',
                bidNumber: this.rowData.bidNumber || '',
                bidName: this.rowData.bidName || '',
                maximumBidLimit: this.rowData.maximumBidLimit || '',
                safetyConstFee: this.rowData.safetyConstFee || '',
                bidBond: this.rowData.bidBond || '',
                biddingStage: this.rowData.biddingStage || '',
                duration: this.rowData.duration || '',
            }
        },

        /*关闭弹窗 */
        handleClose() {
            this.dialogVisible = false
            this.$emit('closeDialog')
        },
        /**确认弹窗 */
        sureBtnClick() {
            this.dialogVisible = false
            this.$emit('closeDialog')
        },
        /**重置表单*/
        reset() {
            this.form = {
                bidId: null,
                markName: '',
                unit: '',
                bidNumber: '',
                bidName: '',
                maximumBidLimit: '',
                safetyConstFee: '',
                bidBond: '',
                biddingStage: '',
                duration: '',
            }
            this.resetForm('ruleForm')
        },
        handleReuslt(res) {
            this.$modal.msgSuccess(res.msg)
            this.reset()
            this.$emit('handleQuery')
            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)
                console.log(data);
                
                // 所有校验通过，组装完整数据
                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) => {
                // 修改
                editBidDataAPI(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>
