<template>
    <span class="self-increasing-sequence">
        <span>{{options.prefix}}</span>
        <span>{{options.number}}</span>
        <span v-if="options.suffix">{{options.suffix}}</span>
    </span>
</template>

<script>
    export default {
        name: "plSequence",
        props:{
            formData: {type:Object,default:() => {return {};}},
            formId: {type: String, required: true},//表id
            fieldId: {type: String, required: true},//字段id
            number:{type: String,default:""},
            prefix:{type: String,default:""},
            suffix:{type:String,default:""},
            description:{type:String,default:""},
            oldDescription:{type:String,default:""},
            readonly: {type: Boolean, default: false},//只读
            operate: {type: String, default: "view"},//是否添加序列
        },
        data(){
            return {
                options:{
                    number : "",
                    prefix : "",
                    suffix : "",
                    description : ""
                },
                originData:"",
                originNumber:"",
            }
        },
        watch:{
            description(){
                this.$set(this.options,"description",this.description);
                if(this.operate == "add"){
                    this.addSequence();
                }else if(this.operate == "update"){
                    if(this.description == this.originData){
                        let value = this.originData + this.prefix + this.originNumber + this.suffix;
                        this.$set(this.options,"number",this.originNumber);
                        this.$set(this.formData,this.fieldId + "_sequence",value);
                    }else{
                        this.addSequence();
                    }
                }
            },
            prefix(){
                this.$set(this.options,"prefix",this.prefix);
            },
            suffix(){
                this.$set(this.options,"suffix",this.suffix);
            }
        },
        created(){
            console.log(this.formId);
            this.originNumber = this.number; // 修改时初始化时的值
            this.originData = this.description; // 修改时初始化时的值
            this.init();
        },
        methods:{
            async init(){
                this.$set(this.options,"number",this.number);
                this.$set(this.options,"prefix",this.prefix);
                this.$set(this.options,"suffix",this.suffix);
                this.$set(this.options,"description",this.description);
                //如果在添加状态下，自增序列
                if(this.operate == "add"){
                    this.addSequence();
                }else{
                    let value = this.options.description + this.prefix + this.options.number + this.suffix;
                    this.$set(this.formData,this.fieldId + "_sequence",value);
                }
            },

            /*
            ** 添加序列
            */
            async addSequence(){
                const res = await this.axios.post(this.api.plSequence.find, {
                    'formId': this.formId,
                    'fieldId': this.fieldId,
                    'sequenceIdenty': this.options.description || "$",
                    'order': '(SEQUENCE_VALUE+0) DESC'
                });
                if(res && res.data){
                    let params = {};
                    if(res.data.length == 0){
                        this.$set(this.options,"number","1");
                        params = {
                            'formId': this.formId,
                            'fieldId': this.fieldId,
                            'sequenceIdenty': this.options.description,
                            'sequenceValue': this.options.number
                        };
                        await this.axios.post(this.api.plSequence.add, params);
                    }else{
                        const seqObj = res.data[0];
                        this.$set(this.options,"number",Number(seqObj.sequenceValue) + 1);
                        params = {
                            'id': seqObj.id,
                            'formId': seqObj.formId,
                            'fieldId': seqObj.fieldId,
                            'sequenceIdenty': seqObj.sequenceIdenty,
                            'sequenceValue': this.options.number
                        };
                        await this.axios.post(this.api.plSequence.update, params);
                    }
                    let value = this.options.description + this.prefix + this.options.number + this.suffix;
                    this.$set(this.formData,this.fieldId + "_sequence",value);
                }
            },
        }
    }
</script>

<style scoped>
    .self-increasing-sequence{
        display: inline-block;
        padding: 2px;
    }
</style>
