<!-- 自增序列 -->
<template>
    <span class="self-increasing-sequence">
        <span class="desc" v-if="isView">{{options.desc}}</span>
        <span v-else style="padding-right: 4px;">
            <el-input v-model="options.desc"
                      size="small"
                      style="width: 200px;"
                      :disabled="readonly"
                      @change="onChange"></el-input>
        </span>
        <span class="prefix">{{options.prefix}}</span>
        <span class="seq">{{options.number}}</span>
        <span class="suffix" v-if="!!options.suffix">{{options.suffix}}</span>
    </span>
</template>

<script>
    export default {
        name: "plIncrease",
        props: {
            value: {},
            //表名
            formId: {
                type: String,
                required: true
            },
            //字段名
            fieldId: {
                type: String,
                required: true
            },
            //选项
            options: {
                type: Object,
                default: () => ({number: '', prefix: '', suffix: '', desc: ''})
            },
            //只读
            readonly: {
                type: Boolean,
                default: false
            },
            //是否添加序列
            isAdd: {
                type: Boolean,
                default: true
            },
            //是否查看序列
            isView: {
                type: Boolean,
                default: false
            },
        },
        watch: {
            value(){
                this.options.desc = this.value;
                //如果在添加状态下，自增序列
                if(this.isAdd){
                    this.addSequence();
                }
            }
        },
        created(){
            this.init();
        },
        methods: {
            async init(){
                //如果在添加状态下，自增序列
                if(this.isAdd){
                    this.addSequence();
                }
            },
            /*
            ** 添加序列
            */
            async addSequence(){
                const res = await this.spcpFormAxios.post(this.api.plSequence.find, {
                    'formId': this.formId,
                    'fieldId': this.fieldId,
                    'sequenceIdenty': this.options.desc,
                    'order': '(SEQUENCE_VALUE+0) DESC'
                });
                if(res && res.data){
                    let params = {};
                    if(res.data.rows.length <= 0){
                        this.options.number = 1;
                        params = {
                            'formId': this.formId,
                            'fieldId': this.fieldId,
                            'sequenceIdenty': this.options.desc,
                            'sequenceValue': this.options.number
                        };
                        await this.spcpFormAxios.post(this.api.plSequence.add, params);
                    }else{
                        const seqObj = res.data.rows[0];
                        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.spcpFormAxios.post(this.api.plSequence.updateById, params);
                    }
                }
            },
            onChange(){
                this.$emit('input', this.options.desc || '');
            }
        }
    }
</script>

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