<script type="text/javascript" language="javascript" src="/js/jquery-1.8.3.js"></script>
<script type="text/javascript" language="javascript" src="/js/ajax.js"></script>
<script type="text/javascript" language="javascript" src="/js/func.js"></script>
<style>
    .color1 {
        background-color: #299AD6;
        color: white;
    }
</style>
<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="name">零件名称：</label>
                <input id="name" name="name" type="text" value="<?= $name ?>">
                <input type="button" class="act_btn" id="search-btn" name="search-btn" value="搜索">
            </td>
        </tr>
    </form>

</table>
<table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" class="table">
<tr>
    <td class="td_bg">
        <label for="pay_type">结算类型:</label>
        <select id="pay_type" onchange="reportChange($(this).val(),<?= $status ?>)">
            <option value="0"></option>
            <option value="1">保险理赔</option>
            <option value="2">自费维修</option>
        </select>
        <span style="display: none" id="js-report-no">
            &nbsp;&nbsp;&nbsp;
            <label for="report_id">报案号:</label>
            <select id="report_id">
                <?php if(isset($reports) && count($reports) > 0 ): ?>
                    <?php foreach ($reports as $report): ?>
                    <option value="<?= $report->id ?>"><?= $report->report_no ?></option>
                        <?php endforeach; ?>
                <?php endif; ?>
            </select>
        </span>
    </td>
</tr>
</table>
<table width="98%" border="0" align="center" cellpadding="2" cellspacing="1" class="table">
    <tr>
        <td width="5%" class="td_bg" nowrap></td>
        <td width="5%" class="td_bg" align="center"><input type="checkbox" class="js-all-choice-box"></td>
        <td width="10%" class="td_bg" align="center"><input type="button" value="批量添加" class="color1 js-all-choice-btn"
                                                            onclick="batchChoiceBox()"></td>
        <td colspan="7" class="td_bg"></td>
    </tr>
    <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="5%" align="center" align="center" class="bg_tr">数量</td>
        <td width="5%" align="center" align="center" class="bg_tr">价格</td>
        <td width="5%" align="center" align="center" class="bg_tr">备注</td>
        <td width="5%" align="center" align="center" class="bg_tr">操作</td>
    </tr>
    <?php if (count($stores) > 0): ?>
        <?php foreach ($stores as $index => $content): ?>
            <tr onMouseOver=overColor(this) onMouseOut=outColor(this)>
                <td align="center" class="td_bg" nowrap><?= $index + 1 ?></td>
                <td align="center" class="td_bg" nowrap><input type="checkbox" class="js-one-choice-box"
                                                               value="<?= $content->id ?>" data-price="<?= $content->selling_price ?>"></td>
                <td align="center" class="td_bg" nowrap><input type="button" value="+" class="color1"
                                                               onclick="oneChoiceBox($(this),'<?= $content->id ?>','<?= $content->selling_price ?>', 2)">
                </td>
                <td align="center" class="td_bg" nowrap><?= $content->name ?></td>
                <td align="center" class="td_bg" nowrap><?= $content->good_sn ?></td>
                <td align="center" class="td_bg" nowrap>1</td>
                <td align="center" class="td_bg"
                    nowrap><?= $content->selling_price ?>
                    元
                </td>
                <td align="center" class="td_bg" nowrap>未知</td>
                <td align="center" class="td_bg" nowrap>
                    [<a href="javascript:void(0)" onclick="waitList($(this),<?= $content->id ?>)">待定</a>]
                </td>
            </tr>
        <?php endforeach; ?>
    <?php endif; ?>
    <tr>
        <td width="50" class="td_bg" nowrap></td>
        <td class="td_bg" align="center"><input type="checkbox" class="js-all-choice-box"></td>
        <td class="td_bg" align="center"><input type="button" value="批量添加" class="color1 js-all-choice-btn"
                                                onclick="batchChoiceBox()"></td>
        <td colspan="7" class="td_bg"></td>
    </tr>
    <tr>
        <td height="25" colspan="12" align="center" class="td_bg"><?= $page_info ?></td>
    </tr>
</table>
<script type="text/javascript" language="javascript">
    var report_id = null;

    $('#pay_type').change();
    //全选
    $('.js-all-choice-box').on('click', function () {
        $('.js-one-choice-box').attr('checked', ($(this).attr('checked') ? true : false));
        $('.js-all-choice-box').attr('checked', ($(this).attr('checked') ? true : false));
    })

    //单选
    function oneChoiceBox(obj, id,price) {
        //支付方式
        var payType = $('#pay_type').val();
        if('0' === payType){
            alert('请先选择结算方式!');
            return false;
        }

        obj.parent('td').prev('td').find('.js-one-choice-box').attr('checked', true);
        obj.css('background-color', 'gray');

        if('1' === payType){
            <?php if(4 === $status): ?>
            report_id = $('#report_id').val();
            <?php endif; ?>
        }

        $.post('/fix-car-item/add', {id: id, fix_id:<?= $fix_id ?>, t: 1,pay_type:payType,report_id:report_id,gid:10,price:price}, function (data) {
            if (data.success) {
                var url = '/fix-car-item/index?fix_id=' + '<?= $fix_id ?>' + '&t=1';
                parent.openBottom(url);
            }
        }, 'json')
    }

    //批量添加
    function batchChoiceBox() {
        //支付方式
        var payType = $('#pay_type').val();
        if('0' === payType){
            alert('请先选择结算方式!');
            return false;
        }

        var idArr = [],priceArr = [];
        $('.js-one-choice-box:checked').each(function () {
            idArr.push($(this).val());
            priceArr.push($(this).data('price'));
        })
        if (0 === idArr.length) return false;

        if('1' === payType){
            <?php if(4 === $status): ?>
            report_id = $('#report_id').val();
            <?php endif; ?>
        }

        $.post('/fix-car-item/batch-add', {ids: idArr, fix_id:<?= $fix_id ?>, t: 1,pay_type:payType,report_id:report_id,gid:10,price:priceArr}, function (data) {
            if (data.success) {
                $('.js-one-choice-box:checked').each(function () {
                    $(this).parent('td').next('td').find('input').css('background-color','gray');
                })
                var url = '/fix-car-item/index?fix_id=' + '<?= $fix_id ?>' + '&t=1';
                parent.openBottom(url);
            }
        })
    }
    //待定清单
    function waitList(obj,id){
        obj.parents('tr').find('.js-one-choice-box').attr('checked', true);
        obj.parents('tr').find('input[type=button]').css('background-color', 'gray');
        //支付方式
        var payType = $('#pay_type').val();
        <?php if(4 === $status): ?>
        report_id = $('#report_id').val();
        <?php endif; ?>
        $.post('/fix-car-item/add', {id: id, fix_id:<?= $fix_id ?>, t: 4,wait:1,pay_type:payType,report_id:report_id,gid:10}, function (data) {
            if (data.success) {
                var url = '/fix-car-item/index?fix_id=' + '<?= $fix_id ?>' + '&t=1';
                parent.openBottom(url);
            }
        }, 'json')
    }
    function reportChange(payType,status){
        if('1' === payType && (4 === status )){
            $('#js-report-no').show();
        }else{
            report_id = null;
            $('#js-report-no').hide();
        }
    }
    //搜索
    $('#search-btn').on('click',function(){
        var $name = $('#name').val();
        if(!$name) return false;
        location.href="<?= $url ?>&name="+$name
    })
</script>
