<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="sampleLibraryName">
                    <el-input class="form-item" v-model="form.sampleLibraryName" clearable show-word-limit
                        placeholder="请输入样本库名称" maxlength="32"></el-input>
                </el-form-item>
                <el-form-item label="样本库标签" prop="sampleLibraryLabel">
                    <el-select class="form-item" v-model="form.sampleLibraryLabel" clearable show-word-limit multiple
                        placeholder="请选择样本库标签">
                        <el-option v-for="item in dict.type.sample_label" :key="item.value" :label="item.label"
                            :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="数据类型" prop="sampleLibraryType">
                    <el-select class="form-item" v-model="form.sampleLibraryType" clearable show-word-limit
                        placeholder="请选择数据类型">
                        <el-option v-for="item in dict.type.sample_data_type" :key="item.value" :label="item.label"
                            :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="描述" prop="sampleLibraryDesc">
                    <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" class="form-item"
                        v-model.trim="form.sampleLibraryDesc" 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 { addSampleDataAPI, editSampleDataAPI, getSampleDetailAPI } from '@/api/data/sample'
const CONSTANT_PARAMS = {
    type: 'edit'
};
export default {
    name: "SampleForm",
    props: ["width", "rowData", "title", "disabled", "isAdd"],
    dicts: ['sample_label', 'sample_data_type'],
    data() {
        return {
            lDialog: this.width > 500 ? "w700" : "w500",
            dialogVisible: true,
            form: {
                sampleLibraryId: null,
                sampleLibraryName: '',
                sampleLibraryLabel: [],
                sampleLibraryType: null,
                sampleLibraryDesc: '',
            },
            rules: {
                sampleLibraryName: [
                    { required: true, message: '样本库名称不能为空', trigger: 'blur' }
                ],
                sampleLibraryLabel: [
                    { required: true, message: '样本库标签不能为空', trigger: 'blur' }
                ],
                sampleLibraryType: [
                    { required: true, message: '数据类型不能为空', trigger: 'blur' }
                ],
            },
        };
    },
    watch: {
        isAdd: {
            handler(newVal) {
                if (newVal === CONSTANT_PARAMS.type) {
                    this.initFormData();
                }
            },
            immediate: true,
        },
    },
    computed: {
        isAddData() {
            return this.isAdd === CONSTANT_PARAMS.type;
        }
    },
    methods: {
        /** 初始化表单数据 */
        async initFormData() {
            const { data } = await getSampleDetailAPI({ sampleLibraryId: this.rowData.sampleLibraryId });
            if (data) {
                this.form = {
                    ...this.form,
                    ...data,
                    sampleLibraryLabel: data.sampleLibraryLabel ? data.sampleLibraryLabel.split(',') : []
                };
            }
        },
        /*关闭弹窗 */
        handleClose() {
            this.dialogVisible = false;
            this.$emit("closeDialog");
        },
        /**确认弹窗 */
        sureBtnClick() {
            this.dialogVisible = false;
            this.$emit("closeDialog");
        },
        /**重置表单*/
        reset() {
            this.form = {
                sampleLibraryId: null,
                sampleLibraryName: '',
                sampleLibraryLabel: [],
                sampleLibraryType: null,
                sampleLibraryDesc: '',
            };
            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);
                    params.sampleLibraryLabel = params.sampleLibraryLabel.join(',');
                    if (!this.isAddData) { // 新增
                        addSampleDataAPI(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 {

                        editSampleDataAPI(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>
