<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" label-position="top">
                <el-form-item label="标签组名称" prop="labelGroupName">
                    <el-input class="form-item" v-model="form.labelGroupName" clearable show-word-limit
                        placeholder="请输入标签组名称" maxlength="32" :disabled="isDisabled"></el-input>
                </el-form-item>
                <el-form-item label="标签名称" prop="labelName">
                    <el-input class="form-item" v-model="form.labelName" clearable show-word-limit
                        placeholder="请输入标签名称" maxlength="32"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="labelDesc">
                    <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" class="form-item"
                        v-model.trim="form.labelDesc" clearable show-word-limit placeholder="请输入描述"
                        maxlength="200" ></el-input>
                </el-form-item>
            </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" class="confirm-btn" :disabled="disabled"
                @click="submitForm('ruleForm')">确认</el-button>
            <el-button class="cancel-btn" @click="handleClose" :disabled="disabled">取消</el-button>
        </span>
    </el-dialog>
</template>
<script>
import _ from 'lodash'
import { addLabelAPI, editLabelAPI } from '@/api/data/label'
const CONSTANT_PARAMS = {
    type:'edit'
};
export default {
    name: "LabelForm",
    props: ["width", "rowData", "title", "disabled", "isAdd"],
    data() {
        return {
            lDialog: this.width > 500 ? "w700" : "w500",
            dialogVisible: true,
            isDisabled: true,
            form: {
                labelGroupId: null,
                labelId: null,
                labelGroupName: '',
                labelName: '',
                labelDesc: '',
            },
            rules: {
                labelName: [
                    { required: true, message: '标签名称不能为空', trigger: 'blur' }
                ],
            },
        };
    },
    watch: {
        isAdd: {
            handler(newVal) {
              console.log('newVal',newVal)
              console.log('123',CONSTANT_PARAMS.type)

              if (newVal === CONSTANT_PARAMS.type) {
                console.log('1231')
                    this.initFormData();
                }else{
                    this.form.labelGroupName = this.rowData.labelGroupName
                    this.form.labelGroupId = this.rowData.labelGroupId
                }
            },
            immediate: true,
        },
    },
    computed: {
        isAddData() {
            return this.isAdd === CONSTANT_PARAMS.type;
        }
    },
    methods: {
        /** 初始化表单数据 */
        initFormData() {
          console.log('初始化表单数据：', this.rowData);
            if (this.rowData) {
                // 编辑模式：填充表单数据
                this.form = {
                    labelGroupId: this.rowData.labelGroupId,
                    labelGroupName: this.rowData.labelGroupName || '',
                    labelId: this.rowData.labelId || '',
                    labelName: this.rowData.labelName || '',
                    labelDesc: this.rowData.labelDesc || '',
                };
            }
        },
        /*关闭弹窗 */
        handleClose() {
            this.dialogVisible = false;
            this.$emit("closeDialog");
        },
        /**确认弹窗 */
        sureBtnClick() {
            this.dialogVisible = false;
            this.$emit("closeDialog");
        },
        /**重置表单*/
        reset() {
            this.form = {
                labelGroupId: null,
                labelId: null,
                labelGroupName: '',
                labelName: '',
                labelDesc: '',
            };
            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.isAddData) {
                        addLabelAPI(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 {

                        editLabelAPI(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));
                        });
                    }
                }
            });
        },
    }
};
</script>
<style lang="scss" scoped>
.w700 ::v-deep .el-dialog {
    width: 700px;
    font-family: Source Han Sans CN, Source Han Sans CN;
}

.w500 ::v-deep .el-dialog {
    width: 500px;
    font-family: Source Han Sans CN, Source Han Sans CN;
}

.w500 ::v-deep .el-dialog__header,
.w700 ::v-deep .el-dialog__header {
    .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;
}
</style>
