<?php $__env->startSection('content'); ?>
    <style>
        #table-4 {
            max-width: 100% !important;
            width: 100%
        }

        @media  screen and (max-width: 1600px) {

            #table-4 {
                max-width: 1600px !important;
                width: 1600px
            }
        }
    </style>
    <ol class="breadcrumb bc-3">
        <li>
            <a href="<?php echo action('dashboard'); ?>"><i class="entypo-home"></i>Home</a>
        </li>
        <li>

            <a href="<?php echo URL::to('dealmanagement'); ?>">Deal Management</a>
        </li>
        <li class="active">
            <strong>Edit Deal</strong>
        </li>
    </ol>
    <h3>Edit Deal</h3>
    <?php echo $__env->make('includes.errors', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
    <?php echo $__env->make('includes.success', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>

    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-primary" data-collapsed="0">
                <div class="panel-heading">
                    <div class="panel-title">
                        Deal Summary
                    </div>
                    <div class="panel-options">
                        <a href="#" data-rel="collapse"><i class="entypo-down-open"></i></a>
                    </div>
                </div>
                <div class="panel-body">
                    <div class=" ">
                        <div class="row">
                            <div class="col-md-6">
                                <h4><strong>Profit</strong></h4>
                                <table class="table table-bordered">
                                    <tbody>
                                    <tr>
                                        <th> Live Sales</th>
                                        <td>
                                            $<?php echo ($deal_summary['customer_sum']['actual_cost'] ? $deal_summary['customer_sum']['actual_cost'] : "0"); ?>

                                        </td>
                                    </tr>
                                    <tr>
                                        <th> Live Cost</th>
                                        <td>
                                            $<?php echo ($deal_summary['vendor_sum']['actual_cost'] ? $deal_summary['vendor_sum']['actual_cost']  : "0"); ?>

                                        </td>
                                    </tr>
                                    <tr>
                                        <th> Live Profit</th>
                                        <td>
                                           $<?php echo ($deal_summary['customer_sum']['actual_cost'] ? number_format(str_replace(',','',$deal_summary['customer_sum']['actual_cost']) - str_replace(',','',$deal_summary['vendor_sum']['actual_cost']) , 6 , '.' , ',') : "0"); ?>

                                        </td>
                                    </tr>

                                    </tbody>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <h4><strong>Customer</strong></h4>
                                <table class="table table-bordered">
                                    <tbody>
                                    <?php foreach($deal_summary['customer_sum'] as $index => $data_total): ?>
                                        <tr>
                                            <th> <?php echo ucfirst(str_replace('_',' ',$index)); ?></th>
                                            <td>
                                                <?php echo $data_total; ?>

                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                    <tr>
                                        <th> %</th>
                                        <td>
                                            <?php if(number_format((int)$deal_summary['customer_sum']['planned_cost'] , 6 , '.' , '') == 0){ $deal_summary['customer_sum']['planned_cost'] = 1; } ?>
                                            <?php echo ($deal_summary['customer_sum']['planned_cost'] ? number_format((int)$deal_summary['customer_sum']['actual_cost']/$deal_summary['customer_sum']['planned_cost'] , 6 , '.' , '') : "0") *100; ?>%
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <h4><strong>Vendor</strong></h4>
                                <table class="table table-bordered">
                                    <tbody>
                                    <?php foreach($deal_summary['vendor_sum'] as $index => $data_total): ?>
                                        <tr>
                                            <th> <?php echo ucfirst(str_replace('_',' ',$index)); ?></th>
                                            <td>
                                                <?php echo $data_total; ?>

                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                    <tr>
                                        <th> %</th>
                                        <td>
                                            <?php if(number_format((int)$deal_summary['vendor_sum']['planned_cost'] , 6 , '.' , '') == 0){ $deal_summary['vendor_sum']['planned_cost'] = 1; } ?>
                                            <?php echo ($deal_summary['vendor_sum']['planned_cost'] ? number_format((int)$deal_summary['vendor_sum']['actual_cost']/$deal_summary['vendor_sum']['planned_cost'] , 6 , '.' , '') : "0") *100; ?>%
                                        </td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <h4><strong>Summary</strong></h4>
                                <table class="table table-bordered">
                                    <thead>
                                    <tr>
                                        <th style="width: 9% !important">Type</th>
                                        <th style="width: 12% !important">Destination</th>
                                        <th style="width: 12% !important">Destination Break</th>
                                        <th style="width: 8% !important">Prefix</th>
                                        <th style="width: 9% !important">Trunk</th>
                                        <th style="width: 8%">Revenue/Cost</th>
                                        <th style="width: 7%">Deal Rate</th> <!-- Sale Price -->
                                        <th style="width: 7%">Actual Rate</th> <!-- Buy Price -->
                                        <th style="width: 7%">Actual Revenue/Cost</th> <!-- Buy Price -->
                                        <th style="width: 7%">Actual Minutes</th> <!-- Buy Price -->
                                        <th style="width: 7%">%</th> <!-- Buy Price -->
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php foreach($deal_summary as $deal_detail_id => $deal_detail): ?>
                                        <?php if(isset($deal_detail['detail'])): ?>
                                        <tr>
                                            <?php

                                            $deal_detail_row = $deal_detail['detail'];
                                            $deal_detail_data = $deal_detail['data'];
                                            if($deal_detail_row->Revenue > 0){
                                                $deal_percentage = ($deal_detail_data['TotalCharges'] /$deal_detail_row->Revenue)*100;
                                            }else{
                                                $deal_percentage = 0;
                                            }
                                            
                                            ?>


                                            <td> <?php echo $deal_detail_row->Type; ?></td>
                                            <td> <?php echo $deal_detail_row->DestinationCountryID ? $Countries[$deal_detail_row->DestinationCountryID] : ''; ?> </td>
                                            <td> <?php echo $deal_detail_row->DestinationBreak; ?> </td>
                                            <td> <?php echo $deal_detail_row->Prefix; ?> </td>
                                            <td> <?php if($deal_detail_row->TrunkID != 0): ?> <?php echo $Trunks[$deal_detail_row->TrunkID]; ?> <?php endif; ?> </td>
                                            <td> <?php echo number_format($deal_detail_row->Revenue , 6 , '.' , ','); ?> </td>
                                            <td> <?php echo number_format($deal_detail_row->SalePrice , 6 , '.' , ','); ?> </td>
                                            <td> <?php echo number_format($deal_detail_row->BuyPrice , 6 , '.' , ','); ?> </td>
                                            <td> <?php echo number_format($deal_detail_data['TotalCharges'] , 6 , '.' , ','); ?> </td>
                                            <td> <?php echo number_format($deal_detail_data['TotalBilledDuration'] , 0 , '' , ','); ?> </td>
                                            <td> <?php echo number_format($deal_percentage , 6 , '.' , ''); ?>%<div class="progress"> <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="<?php echo $deal_percentage; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $deal_percentage; ?>%"> <span class="sr-only"><?php echo $deal_percentage; ?>% Complete</span> </div> </div> </td>
                                        </tr>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
    <p style="text-align: right;">
        <button type="button"  class="save btn btn-primary btn-sm btn-icon icon-left" data-loading-text="Loading..." id="save_deal">
            <i class="entypo-floppy"></i>
            Save
        </button>

        <a href="<?php echo URL::to('/dealmanagement'); ?>" class="btn btn-danger btn-sm btn-icon icon-left">
            <i class="entypo-cancel"></i>
            Close
        </a>
    </p>
    <br>
    <div class="row">
        <div class="col-md-12">
            <form role="form" id="deal-from" method="post" action="<?php echo URL::to('dealmanagement/update/'.$id); ?>" class="form-horizontal form-groups-bordered">

                <div class="panel panel-primary" data-collapsed="0">
                    <div class="panel-body">
                        <div class="form-group">
                            <label class="col-md-2 control-label">Title*</label>
                            <div class="col-md-4">
                                <input type="text" name="Title" class="form-control" id="field-1" placeholder="" value="<?php echo $Deal->Title; ?>" />
                            </div>
                            <label class="col-md-2 control-label">Deal Type*</label>
                            <div class="col-md-4">
                                <?php if($DealDetails != false && count($DealDetails) > 0): ?>
                                    <?php echo Form::select('DealType',Deal::$TypeDropDown, $Deal->DealType,array("class"=>"select2", "disabled" => "disabled")); ?>

                                    <input type='hidden' name='DealType' value='<?php echo $Deal->DealType; ?>'>
                                <?php else: ?>
                                    <?php echo Form::select('DealType',Deal::$TypeDropDown, $Deal->DealType,array("class"=>"select2")); ?>

                                <?php endif; ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-2 control-label">Account*</label>
                            <div class="col-md-4">
                                <?php echo Form::select('AccountID',$Accounts, $Deal->AccountID,array("class"=>"select2")); ?>

                            </div>
                            <label class="col-md-2 control-label">Codedeck*</label>
                            <div class="col-md-4">
                                <?php if($DealDetails != false && count($DealDetails) > 0): ?>
                                    <?php echo Form::select('CodedeckID',$codedecklist,$Deal->CodedeckID,array("class"=>"select2", "disabled" => "disabled")); ?>

                                    <input type='hidden' name='CodedeckID' value='<?php echo $Deal->CodedeckID; ?>'>
                                <?php else: ?>
                                    <?php echo Form::select('CodedeckID',$codedecklist,$Deal->CodedeckID,array("class"=>"select2")); ?>

                                <?php endif; ?>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-2 control-label">Status*</label>
                            <div class="col-md-4">
                                <?php echo Form::select('Status',Deal::$StatusDropDown, $Deal->Status,array("class"=>"select2")); ?>

                            </div>
                            <label class="col-md-2 control-label">Alert Email</label>
                            <div class="col-md-4">
                                <input type="text" name="AlertEmail" class="form-control" id="field-1" placeholder="" value="<?php echo $Deal->AlertEmail; ?>" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-md-2 control-label">Start Date*</label>
                            <div class="col-md-4">
                                <?php echo Form::text('StartDate', date("Y-m-d", strtotime($Deal->StartDate)), array("class"=>"form-control small-date-input datepicker", 'id' => 'StartDate', "data-date-format"=>"yyyy-mm-dd" ,"data-enddate"=>date('Y-m-d'))); ?>

                            </div>
                            <label class="col-md-2 control-label">End Date*</label>
                            <div class="col-md-4">
                                <?php echo Form::text('EndDate', date("Y-m-d", strtotime($Deal->EndDate)), array("class"=>"form-control small-date-input datepicker", 'id' => 'EndDate',"data-date-format"=>"yyyy-mm-dd" ,"data-enddate"=>date('Y-m-d'))); ?>

                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel panel-primary" data-collapsed="0">
                    <div class="panel-heading">
                        <div class="panel-title">
                            Deal Detail
                        </div>

                        <div class="panel-options">
                            <button type="button" onclick="addDeal()" class="btn btn-primary btn-xs add-deal" data-loading-text="Loading...">
                                <i></i>
                                +
                            </button>
                            <a href="#" data-rel="collapse"><i class="entypo-down-open"></i></a>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-bordered dealTable" id="table-4">
                                <thead>
                                <tr class="revenueRow">
                                    <th style="width: 9% !important">Type</th>
                                    <th style="width: 12% !important">Destination</th>
                                    <th style="width: 12% !important">Destination Break</th>
                                    <th style="width: 8% !important">Prefix</th>
                                    <th style="width: 9% !important">Trunk</th>
                                    <th style="width: 8%">Revenue/Cost</th>
                                    <th style="width: 7%">Deal Rate</th> <!-- Sale Price -->
                                    <th style="width: 7%">Actual Rate</th> <!-- Buy Price -->
                                    <th style="width: 8%">(Profit/Loss) per min</th>
                                    <th style="width: 7%">Minutes</th>
                                    <th style="width: 10%">Profit/Loss</th>
                                    <th style="width: 5%">Action</th>
                                </tr>
                                <tr class="paymentRow">
                                    <th style="width: 9% !important">Type</th>
                                    <th style="width: 12% !important">Destination</th>
                                    <th style="width: 12% !important">Destination Break</th>
                                    <th style="width: 8% !important">Prefix</th>
                                    <th style="width: 9% !important">Trunk</th>
                                    <th style="width: 7%">Minutes</th>
                                    <th style="width: 7%">Deal Rate</th> <!-- Sale Price -->
                                    <th style="width: 7%">Actual Rate</th> <!-- Buy Price -->
                                    <th style="width: 8%">(Profit/Loss) per min</th>
                                    <th style="width: 8%">Revenue/Cost</th>
                                    <th style="width: 10%">Profit/Loss</th>
                                    <th style="width: 5%">Action</th>
                                </tr>
                                </thead>
                                <tbody>
                                <input type="hidden" name="TotalPL" value="<?php echo $Deal->TotalPL; ?>">
                                <?php foreach($DealDetails as $dealDetail): ?>
                                    <tr data-pl="<?php echo $dealDetail->TotalPL; ?>" data-rev="<?php echo $dealDetail->Revenue; ?>" data-dealer="<?php echo $dealDetail->Type; ?>">
                                        <td>
                                            <select class="select2 dealer" name="Type[]" onchange="changePrice(this)">
                                                <option <?php if($dealDetail->Type == "Customer"): ?> selected <?php endif; ?> value="Customer">Customer</option>
                                                <option <?php if($dealDetail->Type == "Vendor"): ?> selected <?php endif; ?> value="Vendor">Vendor</option>
                                            </select>
                                        </td>
                                        <td>
                                            <?php echo Form::select('Destination[]', $Countries, $dealDetail->DestinationCountryID, array("class"=>"select2 destination")); ?>

                                        </td>
                                        <td>
                                            <?php echo Form::select('DestinationBreak[]', $destinationBreaks, $dealDetail->DestinationBreak,array("class"=>"select2 destinationBreaks")); ?>

                                        </td>
                                        <td>
                                            <input type="text" name="Prefix[]" class="form-control" value="<?php echo $dealDetail->Prefix; ?>">
                                        </td>
                                        <td>
                                            <?php echo Form::select('Trunk[]', $Trunks, $dealDetail->TrunkID, array("class"=>"select2")); ?>

                                        </td>
                                        <td>
                                            <?php if($Deal->DealType == "Revenue"): ?>
                                                <input type="number" name="Revenue[]" value="<?php echo $dealDetail->Revenue; ?>" onkeyup="changePrice(this)" onchange="changePrice(this)" onblur="changePrice(this)" class="form-control revenue">
                                            <?php else: ?>
                                                <input type="number" name="Minutes[]" value="<?php echo $dealDetail->Minutes; ?>" class="form-control minutes" onkeyup="changePrice(this)" onchange="changePrice(this)" onblur="changePrice(this)">
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <input type="number" name="SalePrice[]" value="<?php echo $dealDetail->SalePrice; ?>" onkeyup="changePrice(this)" onchange="changePrice(this)" onblur="changePrice(this)" class="form-control salePrice">
                                        </td>
                                        <td>
                                            <input type="number" name="BuyPrice[]" value="<?php echo $dealDetail->BuyPrice; ?>" onkeyup="changePrice(this)" onchange="changePrice(this)" onblur="changePrice(this)" class="form-control buyPrice">
                                        </td>
                                        <td>
                                            <input readonly type="number" name="PLPerMinute[]" value="<?php echo $dealDetail->PerMinutePL; ?>" class="form-control pl-minute">
                                        </td>
                                        <td>
                                            <?php if($Deal->DealType == "Revenue"): ?>
                                                <input readonly type="number" name="Minutes[]" value="<?php echo $dealDetail->Minutes; ?>" class="form-control minutes">
                                            <?php else: ?>
                                                <input type="number" name="Revenue[]" value="<?php echo $dealDetail->Revenue; ?>" readonly class="form-control revenue">
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <input readonly type="number" name="PL[]" value="<?php echo $dealDetail->TotalPL; ?>" class="form-control pl-total">
                                        </td>
                                        <td>
                                            <button type="button" title="Delete" onclick="deleteDeal(this)" class="btn btn-danger btn-xs del-deal" data-loading-text="Loading...">
                                                <i></i>
                                                -
                                            </button>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                                </tbody>
                                <tfoot>
                                <tr class="revenueRow">
                                    <th colspan="9"></th>
                                    <th>Total</th>
                                    <th class="pl-grand">0</th>
                                </tr>
                                <tr class="paymentRow">
                                    <th colspan="8"></th>
                                    <th>Total</th>
                                    <th class="rev-grand">0</th>
                                    <th class="pl-grand">0</th>
                                </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
                <div class="panel panel-primary" data-collapsed="0">
                    <div class="panel-heading">
                        <div class="panel-title">
                            Notes
                        </div>

                        <div class="panel-options">
                            <button type="button" onclick="addNote()" class="btn btn-primary btn-xs add-note" data-loading-text="Loading...">
                                <i></i>
                                +
                            </button>
                            <a href="#" data-rel="collapse"><i class="entypo-down-open"></i></a>
                        </div>
                    </div>
                    <div class="panel-body">
                        <table class="table table-bordered noteTable" id="table-44">
                            <thead>
                            <tr>
                                <th style="width: 65%">Note</th>
                                <th style="width: 15%">Created By</th>
                                <th style="width: 15%">Created At</th>
                                <th style="width: 5%">Action</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php foreach($DealNotes as $dealNote): ?>
                                <tr>
                                    <td>
                                        <textarea name="Note[]" placeholder="Write note here..." class="form-control"><?php echo $dealNote->Note; ?></textarea>
                                    </td>
                                    <td>
                                        <?php echo $dealNote->CreatedBy; ?>

                                    </td>
                                    <td class="dateTime">
                                        <?php echo date("Y-m-d", strtotime($dealNote->created_at)); ?>

                                    </td>
                                    <td>
                                        <button type="button" title="Delete" onclick="deleteNote(this)" class="btn btn-danger btn-xs del-deal" data-loading-text="Loading...">
                                            <i></i>
                                            -
                                        </button>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <?php echo $__env->make('dealmanagement.add_edit_script', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
    <?php echo $__env->make('includes.ajax_submit_script', array('formID'=>'deal-from' , 'url' => "dealmanagement/$id/update" ), array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('footer_ext'); ?>
    @parent
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layout.main', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>