<?php
if($_REQUEST['filter']=="" || $_REQUEST['filter']=="current"){$filter="WHERE `statuslive`='1'";}
if($_REQUEST['filter']=="archive"){$filter="WHERE `statuslive`='0'";}
$stmt = $GLOBALS['con']->prepare("SELECT `contractor_number`,`contractor_name`,`pri_contact`,`tel`,`email`,`statuslive` FROM `contractors` $filter")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$cc=$stmt->get_result();$stmt->close();
?>
<script>
$('#navBox').html(
    '<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=contractor.php&view=list">Contractors</a><i class="fad fa-angle-double-right mx-2 mt-1" aria-hidden="true"></i></li><li class="breadcrumb-item active">Contractor List</li>'
)
</script>

<div class="row">
    <div class="col text-end">

        <?php if($_REQUEST['filter']=="" || $_REQUEST['filter']=="current"){?>
        <div id="exportTitle" style="display:none">
            <p style="font-size:14px; font-weight:bold">EnRep Reporting System</p>
            <p style="font-size:12px"><a href="">Contractor List</a> : <a href="">Active contractors</a></p>
        </div>

        <div class="col text-end">
            <a href="index.php?t=includes/master&p=contractor.php&view=new" class="btn btn-secondary btn-sm"><i class="fad fa-plus" aria-hidden="true"></i> New Contractor</a>
            <a href="index.php?t=includes/master&p=contractor.php&view=list&filter=archive" class="btn btn-secondary btn-sm">View Archived contractors</a>
            <script>
            $('#navBox').html(
                '<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=contractor.php&view=list">Contractors</a><i class="fad fa-angle-double-right mx-2 mt-1" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="javascript:void(0)">Contractor List</a><i class="fad fa-angle-double-right mx-2 mt-1" aria-hidden="true"></i></li><li class="breadcrumb-item active">Active contractors</li>'
            )
            </script>
        </div>
        <?php }

if($_REQUEST['filter']=="archive"){?>
        <div id="exportTitle" style="display:none">
            <p style="font-size:14px; font-weight:bold">EnRep Reporting System</p>
            <p style="font-size:12px"><a href="">Contractor List</a> : <a href="">Archived contractors</a></p>
        </div>

        <div class="col text-end">
            <a href="index.php?t=includes/master&p=contractor.php&view=list&filter=current" class="btn btn-secondary btn-sm">View Active contractors</a>
            <script>
            $('#navBox').html(
                '<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=contractor.php&view=list">Contractors</a><i class="fad fa-angle-double-right mx-2 mt-1" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="javascript:void(0)">Contractor List</a><i class="fad fa-angle-double-right mx-2 mt-1" aria-hidden="true"></i></li><li class="breadcrumb-item active">Archived contractors</li>'
            )
            </script>
        </div>
        <?php }?>

    </div>
</div>
<style>
#datatable>tbody>tr>td {
    vertical-align: middle
}
</style>
<table id="datatable" class="table table-striped table-sm table-bordered table-hover responsive mt-4" width="100%">
    <thead class="table-dark">
        <tr>
            <th>Name</th>
            <th>Primary Contact</th>
            <th>Phone</th>
            <th>Email</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <?php while($cd=mysqli_fetch_array($cc)){?>
        <tr>
            <td><?= $cd['contractor_name']?></td>
            <td><?= $cd['pri_contact']?></td>
            <td><?= $cd['tel']?></td>
            <td><?= $cd['email']?></td>
            <td align="right">
                <div class="btn-group" role="group">
                    <?php if($cd['tel']!=""){?><a id="phone" href="tel:<?= $cd['tel']?>" class="btn btn-secondary noprint me-3" data-mdb-tooltip-init data-mdb-placement="top"
                       data-mdb-trigger="hover" title="Phone Contractor"><i class="fad fa-phone"></i></a><?php }?>

                    <?php if(!filter_var($cd['email'], FILTER_VALIDATE_EMAIL) === false) {?><a href="#" class="btn btn-secondary noprint emailButton me-3"
                       data-values="<?= $cd['contractor_number']?>|<?= $cd['pri_contact']?>|<?= $cd['email']?>|<?= $cd['contractor_name']?>" data-href="<?=ROOT_URL?>/includes/master/mailer.php" data-mdb-tooltip-init data-mdb-placement="top"
                       data-mdb-trigger="hover" title="Email Contractor"><i class=" fad fa-envelope"></i></a><?php }?>

                    <a href="index.php?t=includes/master&p=contractor.php&contractor_number=<?= $cd['contractor_number']?>&view=edit" class="btn btn-secondary noprint me-3" data-mdb-tooltip-init data-mdb-placement="top"
                       data-mdb-trigger="hover" title="Contractor Details"><i class="fad fa-info-circle"></i></a>

                    <a href="index.php?t=includes/master&p=contractor.php&contractor_number=<?= $cd['contractor_number']?>&contractor_name=<?= $cd['contractor_name']?>&view=clientlist" class="btn btn-secondary noprint me-3"
                       data-mdb-tooltip-init data-mdb-placement="top"
                       data-mdb-trigger="hover" title="Contractor Assignments"><i
                           class="fad fa-th-list"></i></a>

                    <a href="index.php?t=includes/finance&amp;p=contractor.php&amp;&contractor_number=<?= $cd['contractor_number']?>" class="btn btn-secondary noprint me-3" data-mdb-tooltip-init data-mdb-placement="top"
                       data-mdb-trigger="hover" title="Contractor Finances"><i class="fad fa-file-chart-line"></i></a>

                    <?php if($cd['statuslive']==1){?>
                    <a href="javascript:void(0)" class="btn btn-secondary noprint archiveButton me-3" data-mdb-tooltip-init data-mdb-placement="top" data-mdb-trigger="hover" title="Archive Contractor"
                       onClick="archiveContractor('<?= $cd['contractor_number']?>')"><i class="fad fa-archive"></i></a>
                    <?php }?>

                    <?php if($cd['statuslive']==0){?>
                    <a href="javascript:void(0)" class="btn btn-secondary noprint archiveButton" data-mdb-tooltip-init data-mdb-placement="top" data-mdb-trigger="hover" title="Activate Contractor"
                       onClick="activateContractor('<?= $cd['contractor_number']?>')"><i class="fad fa-upload"></i></a>
                    <?php }?>
                </div>
            </td>
        </tr>
        <?php }?>
    </tbody>
</table>

<?php 
$table="datatable";
        $dateTarget="5";        
        $length="25";
        $sortTarget="1";
        $sortCell="";
        $logo="yes";
        $scroller="";
        $printTarget='exportDiv';
        $printHeader='Outstanding Payments';
        $popup="false";
 require(ROOT_URL.'/includes/tables/single_table.php');?>