<script type="text/javascript" language="javascript" src="/js/jquery-1.8.3.js"></script>
<script type="text/javascript" language="javascript" src="/js/datepicker/WdatePicker.js"></script>
<script type="text/javascript" language="javascript" src="/js/ajax.js"></script>
<script type="text/javascript" language="javascript" src="/js/func.js"></script>
<form id="theFrm">
    <input type="hidden" name="id" value="<?= isset($info) ? $info->id : 0 ?>">
    <table width="98%" border="0" align="center" cellpadding="2" cellspacing="1" class="table">
        <tr>
            <td width="10%" height="25" class="bg_tr" align="right">品牌</td>
            <td height="25" class="td_bg" width="300">
                <select name="brand_id" id="brand_id">
                    <option value="0">--请选择--</option>
                    <?php if ($brands): ?>
                        <?php foreach ($brands as $brand): ?>
                            <option value="<?= $brand->id ?>" <?= isset($info) ? (22 == $brand->id ? 'selected' : '') : '' ?>><?= $brand->name ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
            </td>
        </tr>
        <tr>
            <td width="100" height="25" class="bg_tr" align="right">车型</td>
            <td height="25" class="td_bg" width="300">
                <select name="series_id" id="series_id">
                    <option value="0">--请选择--</option>
                    <?php if (isset($series)): ?>
                        <?php foreach ($series as $serie): ?>
                            <option value="<?= $serie->id ?>" <?=isset($info) ? ($serie->id == $info->series_id ? 'selected' : '') : '' ?>><?= $serie->name ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
            </td>
        </tr>
        <tr>
            <td width="100" height="25" class="bg_tr" align="right">排量</td>
            <td height="25" class="td_bg">
                <select name="displacement_id" id="displacement_id">
                    <option value="0">--请选择--</option>
                    <?php if (isset($displacements)): ?>
                        <?php foreach ($displacements as $displacement): ?>
                            <option value="<?= $displacement->id ?>" <?= isset($info) ? ($displacement->id == $info->displacement_id) ? 'selected' : '' : ''?>><?= $displacement->name ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
            </td>
        </tr>
        <tr>
            <td width="100" height="25" class="bg_tr" align="right">年款</td>
            <td height="25" class="td_bg" colspan="5">
                <select name="year_id" id="year_id">
                    <option value=""></option>
                    <?php if (count($years) > 0): ?>
                        <?php foreach ($years as $year): ?>
                            <option value="<?= $year->id ?>" <?= isset($info) ? ($year->id == $info->year_id ? 'selected' : '' ) : ''?>><?= $year->name ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2" class="td_bg" align="center">
                <input type="button" class="act_btn" value="<?= isset($info) ? '保存' : '提交' ?>" onclick="add()">
                <input type="button" class="act_btn" value="返回" id="back_btn">
            </td>
        </tr>
    </table>
</form>
<script>
    //品牌 change 事件
    $('#brand_id').change(function () {
        var brand_id = $(this).val();
        $('#series_id').html('<option value="0">---请选择---</option>');
        $('#displacement_id').html('<option value="0">---请选择---</option>');
        if (brand_id > 0) {
            $.get('/car/series', {brand_id: brand_id}, function (obj) {
                if (obj.success) {
                    $('#series_id').html(obj.html);
                }
            }, 'json');
        }
    });

    //车型 change 事件
    $('#series_id').change(function () {
        var series_id = $(this).val();
        $('#displacement_id').html('<option value="0">---请选择---</option>');
        if (series_id > 0) {
            $.get('/car/displacement', {series_id: series_id}, function (obj) {
                if (obj.success) {
                    $('#displacement_id').html(obj.html);
                }
            }, 'json');
        }
    });

    //提交
    function add(){
        if(!confirm('确定要提交吗？')) return false;

        var params = $('#theFrm').serialize();
        $.post('/car-model/info',params,function(data){
            alert(data.msg);
            if(data.success){
                $('#back_btn').click();
            }
        },'json')
    }
    //返回
    $('#back_btn').on('click',function(){
        window.location.href = "/car-model/index";
    });
</script>