<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="100" height="25" class="bg_tr">品牌</td>
            <td height="25" class="td_bg" width="300">
                <select name="brand_id" id="brand_id">
                    <option value="0">---请选择---</option>
                    <?php if (isset($brands)): ?>
                        <?php foreach ($brands as $brand): ?>
                            <option value="<?= $brand->id ?>" <?= isset($info) ? ($info->brand_id == $brand->id ? 'selected' : '') : '' ?>><?= $brand->name ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
            </td>
            <td width="100" height="25" class="bg_tr">车型</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>
            <td width="100" height="25" class="bg_tr">排量</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">年款</td>
            <td height="25" class="td_bg" colspan="5">
                <select name="car_year" id="year_id">
                    <option value="0">---请选择---</option>
                    <?php if (isset($years) && count($years) > 0): ?>
                        <?php foreach ($years as $year): ?>
                            <option value="<?= $year->id ?>" <?= isset($info) ? ($year->id === $info->car_year ? 'selected' : '') : '' ?>><?= $year->name ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
            </td>
        </tr>
    </table>
    <table width="98%" border="0" align="center" cellpadding="2" cellspacing="1" class="table">
        <tr>
            <td width="100" height="25" class="bg_tr">分类</td>
            <td height="25" class="td_bg" width="300">
                <select>
                    <?php if (isset($parents) && count($parents) > 0): ?>
                        <?php foreach ($parents as $parent): ?>
                            <option value="<?= $parent->id ?>" <?= isset($info) ? ($parent->name == $info->name ? 'selected' : '') : '' ?>><?= $parent->name ?></option>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </select>
            </td>
            <td width="100" height="25" class="bg_tr">配件名称</td>
            <td class="td_bg" width="300">
                <input type="text" name="name" list="name_list" id="name"
                       value="<?= isset($info) ? $info->name : '' ?>">
                <datalist id="name_list" onchange="alert(1)">
                    <?php if (isset($data_lists) && count($data_lists) > 0): ?>
                        <?php foreach ($data_lists as $data_list): ?>
                            <option label="<?= $data_list->category->name ?>" value="<?= $data_list->category->name ?>"/>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </datalist>
            </td>
            <td width="100" height="25" class="bg_tr">数量</td>
            <td class="td_bg">
                <input type="text" name="number" value="<?= isset($info) ? $info->number : '' ?>">
            </td>
        </tr>
        <tr>
            <td width="100" height="25" class="bg_tr">价格</td>
            <td height="25" class="td_bg" colspan="5">
                <input type="text" name="price" value="<?= isset($info) ? $info->price : '' ?>">
            </td>
        </tr>
        <tr>
            <td height="22" colspan="6" align="center" class="td_bg">
                <?php if (!isset($info)): ?>
                    <input id="submit-btn" type=button class="ACT_btn" name=Submit1 value=" 提交 "/>
                <?php elseif(!in_array($info->status,[2,3])): ?>
                    <input id="save-btn" type=button class="ACT_btn" name=Submit1 value=" 保存 "/>
                <?php endif; ?>
                <input id="back-btn" type="button" class="ACT_btn" name="Submit2" value=" 返回 ">
            </td>
        </tr>
    </table>
</form>
<script type="text/javascript" language="javascript">
    //提交按钮
    $('#submit-btn').on('click', function () {
        if (!confirm('确定提交吗')) return false;
        var params = $('#theFrm').serialize();
        $.post('/purchase/create', params , function (msg) {
            alert(msg.msg);
            if (msg.success) {
                window.location.href = "/purchase/index";
            }
        }, 'json');
    });
    //保存按钮
    $('#save-btn').on('click', function () {
        var params = $('#theFrm').serialize();
        $.post('/purchase/create', params, function (msg) {
            alert(msg.msg);
            if (msg.success) {
                location.reload();
            }
        }, 'json');
    });
    //返回按钮
    $('#back-btn').on('click', function () {
        window.location.href = '/purchase/index';
    });
    $('#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');
        }
    });
    $('#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');
        }
    });

</script>
