<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">
        <tr>
            <td class="td_bg">
                修理项目：<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(<?= isset($projects[0]) ? $projects[0]->group_id :'' ?>)">
        </td>
        <td colspan="8" 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 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 align="center" align="center" class="bg_tr">备注</td>
        <td width="30" align="center" class="bg_tr">操作</td>
    </tr>
    <?php if (count($projects) > 0): ?>
    <?php foreach ($projects as $index => $project): ?>
            <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="<?= $project->id ?>" data-price="<?= $project->price ?>"></td>
                <td align="center" class="td_bg" nowrap><input type="button" value="+" class="color1" onclick="oneChoiceBox($(this),<?= $project->id ?>,<?= $project->group_id ?>,<?= $project->price ?>)"></td>
                <td align="center" class="td_bg" nowrap><?= $project->category->name ?></td>
                <td align="center" class="td_bg" nowrap>--</td>
                <td align="center" class="td_bg" nowrap><?= $project->group ? $project->group->name : '' ?></td>
                <td align="center" class="td_bg" nowrap><?= 7 == $project->group_id ? $project->car_price : '--' ?></td>
                <td align="center" class="td_bg" nowrap><?= 7 == $project->group_id ? '--' : $project->car_price ?><?= 7 == $project->group_id ? '' : '万元' ?></td>
                <td align="center" class="td_bg" nowrap><?= number_format($project->price,2) ?>元</td>
                <td align="center" class="td_bg" nowrap>未知</td>
                <td align="center" class="td_bg" nowrap>
                    [<a href="javascript:void(0);" onclick="waitList($(this),<?= $project->id ?>,<?= $project->group_id ?>,<?= $project->price ?>)">待定</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="8" class="td_bg"></td>
    </tr>
    <tr>
        <td height="25" colspan="13" 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,gid,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:2,pay_type:payType,report_id:report_id,gid:gid,price:price},function(data){
            if(data.success){
                var url = '/fix-car-item/index?fix_id='+'<?= $fix_id ?>'+'&t=2';
                parent.openBottom(url);
            }
        },'json')
    }
    //批量添加
    function batchChoiceBox(gid) {
        //支付方式
        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: 2,pay_type:payType,report_id:report_id,gid:gid,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=2';
                parent.openBottom(url);
            }
        })
    }
    //待定清单
    function waitList(obj,id,gid){
        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:2,gid:gid,pay_type:payType,report_id:report_id}, function (data) {
            if (data.success) {
                var url = '/fix-car-item/index?fix_id=' + '<?= $fix_id ?>' + '&t=2';
                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>
