<?php
use yii\helpers\Url;
?>
<table id="company-pay-type-grid" ></table>
<script>
    function companyPayTypeEdit(id,title) {
        $('<div></div>').dialog({
            id:'company-pay-type-dlg',
            title: title,
            width: 400,
            height: 120,
            closed: false,
            cache: false,
            href: '/company-pay-type/edit?id=' + id,
            modal: true,
            onClose : function() {
                $(this).dialog('destroy');
            },
            buttons:[{
                text:'保存',
                handler:function(){
                    var params = $('#companyPayTypeFrm').serialize();
                    $.post('/company-pay-type/save',params,function(obj){
                        if(obj.success) {
                            $.messager.alert('成功',obj.msg,'info',function(){
                                $("#company-pay-type-dlg").dialog('destroy');
                                $('#company-pay-type-grid').datagrid('reload');
                            });
                        } else {
                            $.messager.alert('警告',obj.msg,'warning');
                        }
                    },'json');
                }
            },{
                text:'取消',
                handler:function(){
                    $("#company-pay-type-dlg").dialog('destroy');
                }
            }]
        });
    }

    function companyPayTypeDelete(id) {
        $.messager.confirm('确认', '是否删除选择项？', function(r){
            if (r){
                var csrf = '<?=Yii::$app->request->csrfToken ?>';
                $.post('/company-pay-type/delete',{id:id,_csrf:csrf},function(obj){
                    if(obj.success) {
                        $.messager.alert('成功',obj.msg,'info',function(){
                            $("#company-pay-type-dlg").dialog('destroy');
                            $('#company-pay-type-grid').datagrid('reload');
                        });
                    } else {
                        $.messager.alert('警告',obj.msg,'warning');
                    }
                },'json');
            }
        });
    }

    $(function(){
        //表格列表
        $('#company-pay-type-grid').datagrid({
            url: '/company-pay-type/json-data',
            method:'GET',
            idField: 'id',
            pagination: true,
            singleSelect: true,
            pageSize: 20,
            pageList: [20,30,50],
            fitColumns: true,
            striped: true,
            toolbar: [{
                iconCls: 'icon-add',
                text:'添加',
                handler: function() {
                    companyPayTypeEdit(0,'添加');
                }
            },{
                iconCls: 'icon-reload',
                text:'刷新',
                handler: function() {
                    $('#company-pay-type-grid').datagrid('reload');
                }
            }],
            columns: [[
                { field: 'id', title: 'ID', align:'center' },
                { field: 'name', title: '名称',width:300 },
                { field: 'created_at', title: '操作', formatter: function(value,row,index){
                    return '[<a href="#" onclick="companyPayTypeEdit('+row.id+',\'修改\');">编辑</a>] '+
                        '[<a href="#" onclick="companyPayTypeDelete('+row.id+');">删除</a>]';
                }}
            ]]
        });
    });
</script>
