'use strict';

/**
 *
 *
 * @author Mai
 * @date 2018/8/21
 * @version
 */
// 向后端请求中间计量号
function getNewCode() {
    postData('/tender/'+ $('#tenderId').val() +'/change/newCode', null, function (code) {
        if (code !== '') {
            $('#bj-code').val(code);
        }
    });
}

class codeRuleSet {
    constructor (obj) {
        this.body = obj;
        // 切换规则组件类型
        $('.rule-change', obj).change(function () {
            const codeType = this.selectedIndex;
            if (codeType === ruleConst.ruleType.addNo) {
                $('#format', obj).show();
                $('#text', obj).show();
                $('#text>label', obj).text('起始编号');
                $('#text>input', obj).val('001');
                const s = '0000000000' + 1;
                $('#text>input', obj).val(s.substr(s.length - $('#format>input', obj).val()));
            } else if (codeType === ruleConst.ruleType.text) {
                $('#format', obj).hide();
                $('#text', obj).show();
                $('#text>label', obj).text('文本');
                $('#text>input', obj).val('').attr('placeholder', '请在这里输入需要的文本');
            } else {
                $('#format', obj).hide();
                $('#text', obj).hide();
            }
        });
        // 修改编号位数
        $('#format>input', obj).change(function () {
            const s = '0000000000' + parseInt($('#text>input', obj).val());
            $('#text>input', obj).val(s.substr(s.length - $(this).val()));
        });

        // 修改连接符
        $('.connector-change', obj).change(function () {
            const connectorType = this.options[this.selectedIndex].text;
            const rules = $('span>span', obj), ruleText = [];
            for (const r of rules) {
                ruleText.push($.trim(r.innerText));
            }
            if (connectorType === '无') {
                $('#preview', obj).text(ruleText.join(''));
            } else {
                $('#preview', obj).text(ruleText.join(connectorType));
            }
            connectorRule = this.options[this.selectedIndex].value;
        });

        // 新增规则组件
        $('#addRule', obj).click(function () {
            const codeType = $('select', obj)[1].selectedIndex;
            const rule = {rule_type: codeType}, html = [];
            let preview;
            switch (codeType) {
                case ruleConst.ruleType.tenderName: {
                    preview = $('#tenderName').val();
                    break;
                }
                case ruleConst.ruleType.text: {
                    rule.text = $('#text>input', obj).val();
                    preview = rule.text;
                    break;
                }
                case ruleConst.ruleType.inDate: {
                    preview = moment().format('YYYY');
                    break;
                }
                case ruleConst.ruleType.addNo: {
                    rule.format = parseInt($('#format>input', obj).val());
                    rule.start = parseInt($('#text>input', obj).val());
                    const s = '0000000000';
                    preview = s.substr(s.length - rule.format);
                    break;
                }
                default: {
                    toast('请选择组件再添加', 'error');
                    return false;
                }
            }
            // 更新规则
            codeRule.push(rule);
            // 更新规则显示
            html.push('<span class="badge badge-light" title="' + ruleConst.ruleString[codeType] + '" rule="' + JSON.stringify(rule) + '">');
            html.push('<span>' + preview + '</span>');
            html.push('<a href="javascript: void(0);" class="text-danger" title="移除"><i class="fa fa-remove"></i></a>');
            html.push('</span>');
            const part = $('#ruleParts', obj).append(html.join(''));
            // 更新规则预览
            const connectorType = connectorRule !== '' && parseInt(connectorRule) !== ruleConst.connectorType.nothing ? ruleConst.connectorString[connectorRule] : '';
            const previewtext = $.trim($('#preview', obj).text()) === '' ? preview : $.trim($('#preview', obj).text()) + connectorType + preview;
            $('#preview', obj).text(previewtext);
        });
        // 删除规则组件
        $($('#ruleParts', obj)).on('click', 'a', function () {
            const index = $('a', obj).index(this);
            codeRule.splice(index-1, 1);
            $(this).parent().remove();
            const rules = $('span>span', obj), ruleText = [];
            for (const r of rules) {
                ruleText.push($.trim(r.innerText));
            }
            const connectorType = connectorRule !== '' && parseInt(connectorRule) !== ruleConst.connectorType.nothing ? ruleConst.connectorString[connectorRule] : '';
            $('#preview', obj).text(ruleText.join(connectorType));
        });
    }
}

$(document).ready(() => {
    // 首次进入设置
    let showNoNeed = false;
    if (parseInt(cRuleFirst)) {
        codeRule = [];
        showNoNeed = true;
        // const firstSet = new codeRuleSet($('div.modal-body', '#first'));
        // // 确认规则上传服务器
        // $('#setRule', '#first').bind('click', function () {
        //     const data = {
        //         rule: ruleType,
        //         connector: connectorRule,
        //         data: JSON.stringify(codeRule),
        //
        //     };
        //     postData('/tender/rule', data, function () {
        //         $('#first').modal('hide');
        //         $('#add-bj').modal('show');
        //     });
        // });

        // $('#first').modal('show');
        $('#setting').modal('show');
    } else if ($('#changList').children.length === 0) {
        $('#add-bj').modal('show');
    }
    // 设置
    const ruleSet = new codeRuleSet($('div.modal-body', '#setting'));
    $('#setRule', '#setting').bind('click', function () {
        const data = {
            rule: ruleType,
            connector: connectorRule,
            data: JSON.stringify(codeRule),
        };
        if (codeRule.length !== 0) {
            $('#autoCodeShow').show();
        }
        postData('/tender/rule', data, function () {
            if (parseInt(cRuleFirst) && showNoNeed) {
                $('#changeFirst').click();
                $('#add-bj').modal('show');
            } else {
                $('#setting').modal('hide');
            }
        });
    })
    // 新增变更令 modal显示
    $('#add-bj').on('show.bs.modal', function() {
        getNewCode();
        if ($('#changeList').children.length === 0) {
            $('#addCancel').hide();
        } else {
            $('#addCancel').show();
        }
        $('#bj-code').removeClass('is-invalid');
    });
    // 获取最新可用变更令号
    $('#autoCode').click(getNewCode);
    // 新增变更令 确认
    $('#addOk').click(function () {
        $(this).attr('disabled', true);
        const data = {
            code: $('#bj-code').val(),
            name: $('#bj-name').val(),
        };
        if (data.code || data.code !== '' || data.name || data.name !== '') {
            postData('/tender/'+ $('#tenderId').val() +'/change/add', data, function (rst) {
                $('#bj-code').removeClass('is-invalid');
                $('#mj-add').modal('hide');
                $(this).attr('disabled', false);
                window.location.href = '/tender/'+ $('#tenderId').val() +'/change/' + rst.cid + '/info';
            }, function () {
                $('#mj-code').addClass('is-invalid');
                $('#mj-Hint').show();
                $(this).attr('disabled', false);
            });
        }
    });

    //状态切换
    $('#status_select').change(function () {
       const status = $(this).val();
       let url = '/tender/'+ $('#tenderId').val() +'/change';
       if (status !== 0) {
           url += '/status/'+ status;
       }
       window.location.href = url;
    });
    // 不再显示首次使用
    $('#changeFirst').click(function () {
        showNoNeed = false;
        $('#changeFirst').remove();
        $('#hide_modal').show();
        $('#setting').modal('hide');
        postData('/tender/'+ $('#tenderId').val() +'/rule/first', '', function () {
        });
    });

    // 弹出删除变更框赋值
    $('.delete-cid-modal').on('click', function () {
        $('#delete-cid').val($(this).attr('cid'));
    });
});
