Javascript App for Modal

var receipt_app = function(){
    var $md = null;
    var id = 0;

    function init(){
        $md = $('#md_receipt');

        $md.find('.bt_new').on('click', function(e){
            create_new(poid);
        });

        $md.find('.bt_print').on('click', function(e){
            window.open(base_url + 'admin/report/print_receipt?poid=' + poid);
        });
    }

    function refresh(){
        get_item();
    }

    function show(_id){
        id = _id;
        $md.modal();
        //clear();
        refresh();
    }

    function clear(){
        var $tbody = $md.find('table tbody');
        $tbody.find('tr').remove();
    }

    function close(){
        $md.modal('close');
    }

    function get_item(callback){
        var params = {
            'id': id
        }
        //console.log(params);return;

        $.get(base_url + 'admin/receipt/ajax_single', params, function(data){
            //console.log(data);
            if(data.status == null){
                alert(data);
                return;
            }else if(data.status == 'fail'){
                alert(data.message);
                return;
            }

            if(typeof callback == 'function'){
                callback.call(this, data.response);
                return;
            }else{
                var data = data.data;
                show_table(data);
            }
        });
    }

    function show_table(data){
        var $body = $md.find('tbody');
        $body.find('td span').html('');

        for(var key in data){
            var value = data[key];
            $body.find('.' + key).html(value);
        }
    }

    function cancel_receipt(id){
        var params = {
            id: id
        }
        //console.log(params);return;

        $.post(base_url + 'admin_api/receipt/cancel', params, function(data){
            //console.log(data);
            if(data.status == null){
                alert(data);
                return;
            }

            if(data.status == 'fail'){
                alert(data.message);
                return;
            }

            if(typeof callback == 'function'){
                callback.call(this, data.response);
                return;
            }else{
                var data = data.response;
                alert('取消成功');
                refresh();
            }
        });
    }

    function create_new(poid){
        var params = {
            poid: poid
        }
        //console.log(params);return;

        $.post(base_url + 'admin_api/receipt/create', params, function(data){
            //console.log(data);
            if(data.status == null){
                alert(data);
                return;
            }

            if(data.status == 'fail'){
                alert(data.message);
                return;
            }

            if(typeof callback == 'function'){
                callback.call(this, data.response);
                return;
            }else{
                var data = data.response;
                alert('新增成功');
                refresh();
            }
        });
    }

    return {
        init: init, 
        show: show, 
        close: close
    }
}();