{include file="app/manage/view/index/emlog_nav"}

<h1 class="page-title"><i class="fas fa-store"></i> <?php echo !empty($this->ret['id']) ? '编辑商城' : '添加商城'; ?></h1>

<form id="mallForm" method="post" action="index.php?r=manage/union/<?php echo !empty($this->ret['id']) ? 'editorMall' : 'addMall'; ?>">
    <?php if(!empty($this->ret['id'])): ?><input type="hidden" name="id" value="<?php echo $this->ret['id']; ?>"><?php endif; ?>
    <div class="form-group">
        <label>商城名称</label>
        <input class="form-control" name="name" value="<?php echo $this->ret['name'] ?? ''; ?>">
    </div>
    <div class="form-group">
        <label>所属联盟模型</label>
        <select class="form-control" name="union_id">
            <option value="0">请选择</option>
            <?php if(!empty($this->union)): ?>
            <?php foreach($this->union as $u): ?>
            <option value="<?php echo $u['id']; ?>" <?php echo (!empty($this->ret['union_id']) && $this->ret['union_id'] == $u['id']) ? 'selected' : ''; ?>><?php echo $u['name']; ?></option>
            <?php endforeach; ?>
            <?php endif; ?>
        </select>
    </div>
    <button type="submit" class="btn btn-primary" id="mallSaveBtn">保存</button>
    <a href="index.php?r=manage/union/mall" class="btn btn-default">返回</a>
</form>

{include file="app/manage/view/index/emlog_footer"}

<script>
jQuery(function($){
    $('#mallForm').on('submit', function(e){
        e.preventDefault();
        var btn = $('#mallSaveBtn'), text = btn.text();
        btn.prop('disabled', true).text('保存中...');
        $.ajax({
            url: $(this).attr('action'),
            type: 'POST',
            data: $(this).serialize(),
            dataType: 'json',
            success: function(res){
                if(res && res.need_login){
                    showToast(res.info || '登录已过期，请重新登录', 'danger');
                    setTimeout(function(){ window.location.href = 'index.php?r=manage/login/index'; }, 1200);
                    return;
                }
                if(res && res.status == 'y'){
                    showToast(res.info, 'success');
                    setTimeout(function(){ window.location.href = 'index.php?r=manage/union/mall'; }, 1200);
                } else {
                    showToast((res && res.info) || '保存失败', 'danger');
                    btn.prop('disabled', false).text(text);
                }
            },
            error: function(xhr){
                var msg = '请求异常，请稍后重试';
                try { var r = JSON.parse(xhr.responseText); if (r && r.info) msg = r.info; } catch(e) {}
                showToast(msg, 'danger');
                btn.prop('disabled', false).text(text);
            }
        });
    });
});
</script>
