<template>
    <!-- 小型弹窗，用于完成，删除，保存等操作 -->
    <el-dialog class="l-dialog" :class="lDialog" :title="title" :visible.sync="dialogVisible" :showClose="true"
        :closeOnClickModal="false" @close="handleClose" :append-to-body="true">
        <div>
            <el-form :model="form" :rules="rules" ref="ruleForm" label-width="110px">
                <el-form-item label="参数名称" prop="configName">
                    <el-input class="form-item" v-model="form.configName" clearable show-word-limit
                        placeholder="请输入参数名称" maxlength="32" :disabled="isAdd === 'edit'"></el-input>
                </el-form-item>
                <el-form-item label="参数编码" prop="configCode">
                    <el-input class="form-item" v-model="form.configCode" clearable show-word-limit
                        placeholder="请输入参数编码" maxlength="32" :disabled="isAdd === 'edit'"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="useStatus">
                    <el-radio v-model="form.useStatus" label="0">启用</el-radio>
                    <el-radio v-model="form.useStatus" label="1">禁用</el-radio>
                </el-form-item>
            </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button class="clear-btn" @click="handleClose" :disabled="disabled">取消</el-button>
            <el-button type="primary" class="search-btn" :disabled="disabled"
                @click="submitForm('ruleForm')">确认</el-button>
        </span>
    </el-dialog>
</template>
<script>
import _ from 'lodash'
import {
    addAPI,
    editAPI,
} from '@/api/system/setting'
export default {
    name: "SettingForm",
    props: ["width", "dataForm", "title", "disabled", "isAdd", "rowData"],
    dicts: ['dimension', 'file_related_type'],
    data() {
        return {
            lDialog: this.width > 500 ? "w700" : "w500",
            dialogVisible: true,
            isDisabled: true,
            form: {
                configName: null,
                configCode: null,
                useStatus: '0',
            },
            loading: null,
            rules: {
                configName: [
                    { required: true, message: '请输入参数名称', trigger: 'blur' }
                ],
                configCode: [
                    { required: true, message: '请输入参数编码', trigger: 'blur' }
                ],
            },
            errorObj: {
                configName: '参数名称',
                configCode: '参数编码',
            },
        };
    },
    created() {
        this.initFormData();
    },
    methods: {
        /** 初始化表单数据 */
        initFormData() {
            if (this.isAdd === 'edit' && this.rowData) {
                console.log(this.rowData);
                // 编辑模式：填充表单数据
                this.form = {
                    id: this.rowData.id,
                    configName: this.rowData.configName || null,
                    configCode: this.rowData.configCode || null,
                    useStatus: this.rowData.useStatus || '0',
                };
            } else {
                // 新增模式：重置表单
                this.form = {
                    configName: null,
                    configCode: null,
                    useStatus: '0',
                };
            }
        },
        /*关闭弹窗 */
        handleClose() {
            this.dialogVisible = false;
            this.$emit("closeDialog");
            /* setTimeout(() => {
                this.dialogVisible = true;
            }); */
        },
        /**确认弹窗 */
        sureBtnClick() {
            this.dialogVisible = false;
            this.$emit("closeDialog");
            /* setTimeout(() => {
                this.dialogVisible = true;
            }); */
        },
        /**重置表单*/
        reset() {
            this.form = {
                configName: null,
                configCode: null,
                useStatus: '0',
            };
            this.resetForm("ruleForm");
        },
        handleReuslt(res) {
            this.$modal.msgSuccess(res.msg);
            this.reset();
            this.$emit('handleQuery');
            this.handleClose();
        },
        /**验证 */
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    // 显示遮罩层
                    this.loading = this.$loading({
                        lock: true,
                        text: "数据提交中，请稍候...",
                        background: 'rgba(0,0,0,0.5)',
                        target: this.$el.querySelector('.el-dialog') || document.body
                    })
                    let params = _.cloneDeep(this.form);
                    
                    if (this.isAdd === 'add') {
                        addAPI(params).then(res => {
                            this.loading.close();
                            if (res.code === 200) {
                                this.handleReuslt(res);
                            } else {
                                this.$modal.msgError(res.msg);
                            }
                        }).catch(error => {
                            this.loading.close();
                            this.$modal.msgError(this.errorMsg(error));
                        });
                    } else {
                        editAPI(params).then(res => {
                            this.loading.close();
                            if (res.code === 200) {
                                this.handleReuslt(res);
                            } else {
                                this.$modal.msgError(res.msg);
                            }
                        }).catch(error => {
                            this.loading.close();
                            this.$modal.msgError(this.errorMsg(error));
                        });
                    }
                }
            });
        },
        errorMsg(error) {
            const errorMessage = error.message;
            const errorArr = errorMessage.split('：');
            const errorObjData = errorArr.length > 1 ? JSON.parse(errorArr[1]) : '';
            if(errorObjData){
                return this.errorObj[errorObjData.paramName] + errorArr[0] + ':' + errorObjData.originalValue;
            }else{
                return errorMessage;
            }
            
        }
    }
};
</script>
<style lang="scss" scoped>
.w700 ::v-deep .el-dialog {
    width: 700px;
}

.w500 ::v-deep .el-dialog {
    width: 500px;
}

.w500 ::v-deep .el-dialog__header,
.w700 ::v-deep .el-dialog__header {
    // background: #eeeeee;

    .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;
}
</style>