<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>
<script type="text/javascript" language="javascript" src="/js/common.js"></script>
<table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" class="table">
    <form id="searchForm" method="GET">
        <tr>
            <td class="td_bg">
                <label for="brand_id">品牌：</label>
                <select name="brand_id" id="brand_id">
                    <option value="">--请选择--</option>
                    <?php if (count($brands) > 0): ?>
                        <?php foreach ($brands as $brand): ?>
                            <option value="<?= $brand->id ?>" <?= $brand_id == $brand->id ? 'selected' : '' ?>><?= $brand->name ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
                <label for="series_id">型号：</label>
                <select name="series_id" id="series_id">
                    <option value="0">---请选择---</option>
                    <?php if (count($series) > 0): ?>
                        <?php foreach ($series as $item): ?>
                            <option value="<?= $item->id ?>" <?= $item->id == $series_id ? 'selected' : '' ?>><?= $item->name ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
                <label for="displacement_id">排量：</label>
                <select name="displacement_id" id="displacement_id">
                    <option value="0">---请选择---</option>
                    <?php if (count($displacements) > 0): ?>
                        <?php foreach ($displacements as $displacement): ?>
                            <option value="<?= $displacement->id ?>" <?= $displacement->id == $displacement_id ? 'selected' : '' ?>><?= $displacement->name ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
                <label for="year_id">年份：</label>
                <select name="year_id" id="year_id">
                    <option value="0">---请选择---</option>
                    <?php if (count($years) > 0): ?>
                        <?php foreach ($years as $year): ?>
                            <option value="<?= $year->id ?>" <?= $year_id == $year->id ? 'selected' : '' ?>><?= $year->name ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
                <input type="submit" class="ACT_btn" value="搜索">
                <input type="button" class="act_btn" value="添加" onclick="add();">
            </td>
        </tr>
    </form>
</table>
<table width="98%" border="0" align="center" cellpadding="2" cellspacing="1" class="table">
    <tr>
        <td width="50" align="center" align="center" class="bg_tr" nowrap>排序</td>
        <td align="center" align="center" class="bg_tr">品牌</td>
        <td align="center" align="center" class="bg_tr">型号</td>
        <td align="center" align="center" class="bg_tr">排量</td>
        <td align="center" align="center" class="bg_tr">年份</td>
        <td width="30" align="center" class="bg_tr">操作</td>
    </tr>
    <?php if (count($SDY) > 0): ?>
        <?php foreach ($SDY as $index => $item): ?>
            <tr onMouseOver=overColor(this) onMouseOut=outColor(this)>
                <td align="center" class="td_bg" nowrap><?= $index + 1 ?></td>
                <td align="left" class="td_bg" nowrap><?= $item->series->brand->name ?></td>
                <td align="left" class="td_bg" nowrap><?= $item->series->name ?></td>
                <td align="left" class="td_bg" nowrap><?= $item->displacement->name ?></td>
                <td align="left" class="td_bg" nowrap><?= $item->year->name ?></td>
                <td align="center" class="td_bg" nowrap>
                    [<a href="javascript:void(0);" onclick="edit(<?= $item->id ?>)">详情</a>]
                    [<a href="javascript:void(0);" onclick="del(<?= $item->id ?>,$(this))">删除</a>]
                </td>
            </tr>
        <?php endforeach; ?>
    <?php endif; ?>
    <tr>
        <td height="25" colspan="7" align="center" class="td_bg"><?= $page_info ?></td>
    </tr>
</table>
<script type="text/javascript" language="javascript">
    //编辑
    function edit(id) {
        window.location.href = '/car-model/info?id=' + id;
    }

    //品牌 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() {
        window.location.href = "/car-model/info";
    }
    //删除操作
    function del(id,obj){
        if(!confirm("确定要删除吗？"))return false;

        obj.parents('tr').addClass('remove-'+id);
        $.post('/car-model/remove',{id:id},function(data){
          if(data.success){
              $('.remove-'+id).remove();
          }
        },'json')
    }
</script>
