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

    function carMycarsAppointment(id) {
        $('<div></div>').dialog({
            id:'car-mycars-appointment-dlg',
            title: '预约记录',
            width: 700,
            height: 500,
            closed: false,
            cache: false,
            href: '/appointment/index?car_id=' + id,
            modal: true,
            onClose : function() {
                $(this).dialog('destroy');
            }
        });
    }

    function carInsurer(id) {
        $('<div></div>').dialog({
            id:'car-insurer-dlg',
            title: '保险记录',
            width: 800,
            height: 320,
            closed: false,
            cache: false,
            href: '/car/insurer?car_id=' + id,
            modal: true,
            onClose : function() {
                $(this).dialog('destroy');
            }
        });
    }

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

    $(function(){
        //表格列表
        $('#car-mycars-grid').datagrid({
            url: '/car/json-data?user_id=<?=$user_id?>',
            method:'GET',
            idField: 'id',
            pagination: true,
            singleSelect: true,
            rownumbers: true,
            pageSize: 20,
            pageList: [20,30,50],
            fitColumns: true,
            striped: true,
            toolbar: [{
                iconCls: 'icon-reload',
                text:'刷新',
                handler: function() {
                    $('#car-mycars-grid').datagrid('reload');
                }
            }],
            columns: [[
                { field: 'car_no', title: '车牌号',width:110 },
                { field: 'car_man', title: '车主',width:120 },
                { field: 'phone', title: '联系电话',width:110 },
                { field: 'engine_no', title: '发动机号',width:150 },
                { field: 'car_frame_no', title: '车架号',width:200 },
                { field: 'seats', title: '座位数',align:'center',width:50 },
                { field: 'user', title: '业务员',align:'center',width:120 },
                { field: 'created_at', title: '操作', formatter: function(value,row,index){
                    return '[<a href="#" onclick="carMycarsEdit('+row.id+',\'修改车辆信息\');">编辑</a>] '+
                        '[<a href="#" onclick="carMycarsAppointment('+row.id+');">预约记录</a>]' +
                        '[<a href="#" onclick="carInsurer('+row.id+');">保险记录</a>]';
                }}
            ]]
        });
    });
</script>
