<script>
    layui.use(['table', 'form', 'element', 'layer', 'admin', 'index', 'laydate', 'xmSelect'], function () {
        let admin = layui.admin;
        let form = layui.form;
        let table = layui.table;

        $('#supplierDetailUrl').click(function () {
            admin.showLoading({
                type: 3,
            });
        });


        //点击芯链
        form.on('checkbox(stockup_type_filter)', function (data) {
            if (data.elem.checked && data.elem.name === "stockup_type[5]") {
                let url = '/api/supplier/checkHasLadderPriceSetting?supplier_id=' + getQueryVariable('supplier_id');
                let elem = data.elem;
                let self = $(this);
                $.ajax({
                    url: url,
                    type: 'POST',
                    async: true,
                    data: data.field,
                    dataType: 'json',
                    timeout: 20000,
                    success: function (res) {
                        if (res.err_code === 0) {

                        } else {
                            layer.msg('请先去魔方系统配置阶梯系数,再新建芯链账号', {icon: 5})
                            $('input[name="stockup_type[5]"]').next().click();
                        }
                    },
                    error: function () {
                        layer.msg('网络错误', {icon: 5});
                    }
                });
            }
        });


        form.on('submit(updateSupplier)', function (data) {
            let confirmMessage = '';
            if (data.field.status === '-2') {
                confirmMessage = '确定要重新入驻吗,该供应商就会再次进入审核阶段'
            } else if (data.field.status === '-1') {
                confirmMessage = '确定要提交新增供应商信息吗?一旦提交,该供应商就会进入待复审阶段,审核过程中无法进行信息修改'
            } else if (data.field.status === '3') {
                confirmMessage = '确定要重新提交审核吗?一旦提交,该供应商就会再次进入审核中阶段,审核过程中无法进行信息修改';
            } else {
                //修改不需要进入审核了,有个单独提交审核的按钮
                updateSupplier(data);
                return;
            }
            layer.confirm(confirmMessage, function (index) {
                updateSupplier(data);
            });
        })

        form.on('select(apply_audit_reason_selector)', function (data) {
            $('#apply_audit_reason').val(data.value);
        });

        form.on('submit(applyAuditSupplier)', function (data) {
            $('input:disabled').attr('disabled', false);
            let fieldNameList = [];
            $('.attachment_fields').each(function () {
                fieldNameList.push($(this).text());
            });

            //提交先提示是否要转换供应商类型
            let radioObj = $('input[name="supplier_type"]');
            //已经上传品质保证协议(代理商则提示为“已经上传代理证”),是否切换为正式供应商?
            let supplierGroup = data.field.supplier_group;
            let msg;
            let obj1Checked = 0;
            let obj2Checked = 0;
            if (data.field.supplier_type === '2') {
                //如果已经上传品质保证协议,并且供应商性质不是代理商(代理商要代理证)
                if (inArray('quality_assurance_agreement', fieldNameList) && supplierGroup !== '1') {
                    msg = '已经上传品质保证协议,是否切换为正式供应商?'
                    obj1Checked = true;
                    obj2Checked = false;
                }
                if (inArray('proxy_certificate', fieldNameList) && supplierGroup === '1') {
                    msg = '该供应商为代理供应商,已经上传代理证,是否切换为正式供应商?'
                    obj1Checked = true;
                    obj2Checked = false;
                }
            } else {
                if (!inArray('quality_assurance_agreement', fieldNameList) && !inArray(supplierGroup, ['1', '4'])) {
                    msg = '未上传品质保证协议,是否切换为临时供应商?'
                    obj1Checked = false;
                    obj2Checked = true;
                }
                if (!inArray('proxy_certificate', fieldNameList) && supplierGroup === '1') {
                    msg = '该供应商为代理供应商,但是未上传代理证,是否切换为临时供应商?'
                    obj1Checked = false;
                    obj2Checked = true;
                }
            }
            @if ($ignore_supplier_type_change_tips)
                msg = '';
            @endif
            if (msg) {
                layer.confirm(msg, {btn: ["确认", "取消"]},
                    function (index) {
                        if (obj1Checked !== 0) {
                            radioObj.get(0).checked = obj1Checked;
                            radioObj.get(1).checked = obj2Checked;
                            layui.form.render('radio');
                            data.field.supplier_type = obj1Checked ? "1" : "2";
                            layer.closeAll();
                        }
                        auditAndUpdateSupplier(data);
                    },
                    function (index) {
                        auditAndUpdateSupplier(data);
                    }
                );
            } else {
                auditAndUpdateSupplier(data);
            }
            return false;
        });

        //直接审核并且更新供应商
        function auditAndUpdateSupplier(data) {
            data.field.is_audit = 1;
            if (data.field.supplier_type === '2') {
                layer.open({
                    type: 1,
                    area: ['450px', '370px'], //宽高
                    content: $('#apply_audit_reason_div'),
                    btn: ['保存并申请审核', '取消'],
                    btn1: function (index, layero) {//点击保存按钮
                        let reason = $('#apply_audit_reason').val();
                        if (reason === '') {
                            layer.msg('请选择申请理由', {icon: 5})
                            return false;
                        }
                        data.field.apply_audit_reason = reason;
                        updateSupplier(data);
                        setTimeout(function () {
                            layer.close(index);
                        }, 1000);
                    },
                });
            } else {
                layer.confirm('确定直接申请审核吗?确定后会直接进入审核中的状态,审核完成前无法进行二次修改', function (index) {
                    updateSupplier(data);
                });
            }
        }

        function updateSupplier(data) {
            admin.showLoading();
            admin.showLoading('.layui-layer-btn0');
            $.ajax({
                url: '/api/supplier/UpdateSupplier',
                type: 'POST',
                async: true,
                data: data.field,
                dataType: 'json',
                timeout: 20000,
                success: function (res) {
                    if (res.err_code === 0) {
                        admin.putTempData("needFreshList", 1)
                        table.reload('receiptList')
                        location.href = "/supplier/SupplierDetail?view=iframe&supplier_id={{$supplier['supplier_id']}}"
                        layer.msg(res.err_msg, {icon: 6})
                    } else {
                        let errMsg = res.err_msg;
                        let msg = '';
                        $.each(errMsg.split('|'), function (index, value) {
                            msg += "<span>" + value + "</span><br>"
                        });
                        layer.msg(msg, {icon: 5})
                    }
                    admin.removeLoading();
                    admin.showLoading('.layui-layer-btn0');
                },
                error: function () {
                    admin.removeLoading();
                    admin.showLoading('.layui-layer-btn0');
                    layer.msg('网络错误', {icon: 5});
                }
            });

        }
    });
</script>