<?php
include('../../includes/checkSession.php');
include('../../includes/con2pma3.php');
include('../includes/header_in.php'); ?>
<script>
    function sendforinitiate(bid)
    {
        var c = confirm("Are you sure you want to send this document for review? It will show in the list of documents to be assigned as an initiated document.");
        if (c)
        {
            window.location.href = "../../DB/03-scope/BriefDesign1DB.php?send=" + bid;
        }
        else
        {
            return false;
        }
    }
</script>
<?php $BID = isset($_GET['BID']) ? $_GET['BID'] : 0;
$cid = $_SESSION['id'];
$scope = 'design-brief';
$result = mysql_query("SELECT briefD.*,projects.projectCode,client.clientName FROM projects,client,briefD where briefD.companyID='$id' AND briefD_id='$BID' AND (briefD.briefD_status='1' OR briefD.briefD_status='3') AND projects.projectSerial=briefD.project_id AND client.clientSerial=briefD.client_id ");
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$bstatus = $row["briefD_status"];
$PreparedBy = "";
if ($row["PreparedBy"] == 0) {
    $resultPre = mysql_query("SELECT firstName, lastName FROM login where loginId='" . $id . "'");
    $PreparedBy .= mysql_result($resultPre, 0, 0);
    $PreparedBy .=" " . mysql_result($resultPre, 0, 1);
} else {
    $resultPre = mysql_query("SELECT internal_personnel_firstName, internal_personnel_lastName FROM internal_personnel where internal_personnel_id='" . $row["PreparedBy"] . "'");
    $PreparedBy .= mysql_result($resultPre, 0, 0);
    $PreparedBy .=" " . mysql_result($resultPre, 0, 1);
}
?>
<style>
    .editfield{display:none}
    .pheadin-txt .panel-numbering,.pheadin-txt .htitle {font-size:14px; font-weight:bold}
    .whead{    padding: 4px 0 8px 0;}
    .panel-edit a{margin-right:7px}
    .panel-edit span{margin-left:7px}
    .panel-edit{text-align:right}
    .panel-detail{padding:0 !important}
    .update_alert{width:30% !important; margin:0 auto 2px auto !important; padding:3px !important; text-align:center }
     .panel-edit .glyph{float:right}
    .delhead{float:right}
    .delhead .fa{color:#900; font-size:16px;padding-right:0px !important}
    .delhead{margin: 0 0 0 10px !important}
</style>
<section class="content">
    <div class="container">
        <div class="row">
            <div class="main-content">
                <h5>
                    <?php if (isset($_REQUEST["msg"])) { ?>
                        <?php echo $_REQUEST["msg"]; ?>
                    <?php } ?>
                </h5>
                <?php if ($uacc == "Full Control") { ?>
                    
                        <div class="widget">
                            <div class="whead"> <h6> <i class="fa fa-play-circle"></i> Develope the Design Brief </h6>   </div>

                          

                                <div class="formRow fRow">
                                    <div class="col-sm-1-8 docdetail-label" style="padding-right:0px;">
                                      <label>Project Code | Title:</label>
                                  </div>

                                    <div class="col-sm-2-2 docdetail-field" style="padding-left:0px;">
                                        <?php echo $row["projectCode"] . " | " . $row["briefD_name"]; ?>
                                    </div>

                                   <div class="col-sm-1-8 docdetail-label" style="padding-right:0px;">

                                      <label>Prepared By:</label></div>

                                    <div class="col-sm-2-2 docdetail-field" style="padding-left:0px;">
                                        <?php echo $PreparedBy; ?>
                                    </div>
                                    
                                    
                                    <div class="col-sm-1-8 docdetail-label" style="padding-right:0px;">

                                      <label>Doc ID:</label></div>

                                    <div class="col-sm-2-2 docdetail-field" style="padding-left:0px;">
                                        <?php echo "DBRF - " . $BID; ?>
                                    </div>
                                    
                                    
                                </div>

                                <div class="formRow fRow">
                                    
								<div class="col-sm-1-8 docdetail-label" style="padding-right:0px;">
                                      <label>Revision:</label>
                                </div>

                                    <div class="col-sm-2-2 docdetail-field" style="padding-left:0px;">
									 <?php echo $row["briefD_rev"]; ?>
                                    </div>

								<div class="col-sm-1-8 docdetail-label" style="padding-right:0px;">

                                        <label>Client Name:</label></div>

                                    <div class="col-sm-2-2 docdetail-field" style="padding-left:0px;">
                                        <?php echo $row["clientName"]; ?>
                                    </div>


                                    <div class="col-sm-1-8 docdetail-label" style="padding-right:0px;">

                                        <label>Date Initiated:</label></div>

                                    <div class="col-sm-2-2 docdetail-field" style="padding-left:0px;">
                                        <?php echo $row["creationDate"]; ?>
                                    </div>
                                </div>
                         

                        </div>

                   




                    <div class="widget">
                        <div class="whead"> <h6> <i class="fa fa-play-circle"></i>Scope of Work </h6>
                        
                         <?php if ($bstatus != 3) { ?> <input align="right" onclick="window.location.href = 'BriefDesign1.php'" class="a1 a1 buttonM formSubmit bGreen" type="submit" name="Save5" id="Save5" value="Save" style="float:left; margin-left:750px"/> <input align="right" onclick="sendforinitiate(<?php echo $BID; ?>)" class="a1 a1 buttonM formSubmit bGreen" type="submit" name="Save4" id="Save4" value="Send" style="float:left; margin-left:10px"/>  <?php } ?> <input align="right" onclick="location.href='http://pm4.pmanywhere.co/Interface/03-scope/DBPrintView.php?BID=<?php echo $BID;?>';" class="a1 a1 buttonM formSubmit bGreen" type="submit" name="PrintDB" id="PrintDB" value="Print" style="float:left; margin-left:10px"/>
                          </div>
                        
                        <?php
                        $query = "SELECT * from scope_brief WHERE scope_type='$scope' AND (company_id= 0 OR company_id = '$cid') AND (brief_id= 0 OR brief_id = '$BID') ";
                        $result = mysql_query($query);
                        $i = 1;
                        ?>
                        <?php while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
                            <div class="formRow fRow">

                                <div class="col-md-12 panel-detail">
                                    <div class="col-md-12">

                                        <div class="pheadin-txt" id="pheading-<?php echo $i; ?>" >
                                            <div class=" alert-success update_alert" id="alert-<?php echo $row['id'] ?>"> </div>
                                            <div class="col-md-1 col-sm-2  panel-numbering" style="padding:0 3px;float:left"><?php echo $i  ?></div>
                                            <div class="col-md-10 com-sm-8 htitle" id="headingf-<?php echo $row['id'] ?>"><?php echo $row['heading'] ?> </div>
                                            <?php if ($bstatus != 3) { if ($row['company_id'] == $cid) {  ?>
                                            <div class="col-md-1 col-sm-2">
                                                <div class="  panel-edit" style="float:right; cursor:pointer" >
                                                     <a class="delhead" href="javascript:void(0)" onclick="deleteHeading('<?php echo $row["id"]; ?>')">
                                                          <i class="fa fa-times" title="Delete"></i>
                                                        </a> 
                                                    <div class="glyph" onclick="openedit('heading', '<?php echo $row['id']; ?>')">
                                                        <div class="fs1 iconb" data-icon="" title="Edit"></div>
                                                    </div> 
                                                   
                                                 
                                                    
                                                </div>
                                             </div>
                                            <?php } } ?>
                                        </div>
                                        <?php if ($row['company_id'] == $cid) { ?>
                                            <div class="editfield" id="peditheading-<?php echo $row['id']; ?>" >
                                                <form action="" method="post" id="formforheading-<?php echo $row['id'] ?>" onsubmit="submitheadingform('<?php echo $row['id'] ?>')">
                                                    <div class="col-md-1 col-sm-2  panel-numbering" style="padding:0 3px;float:left"><?php echo $k ?></div>
                                                    <div class="col-md-10 com-sm-8"><input type="text" required name="headingtext" value="<?php echo $row['heading'] ?>" /></div>
                                                    <input type="hidden" name="task" value="editheading"> 
                                                    <input type="hidden" name="headingid" value="<?php echo $row['id'] ?>">
                                                </form>
                                                <div class="col-md-1 col-sm-2 panel-edit" style="float:right"><a href="javascript:void(0)" onclick="submitheadingform('<?php echo $row['id'] ?>')"><input type="image" src="../images/icon-save7.png" width="12" height="12" border="0"></a>|<span onclick="closeedit('heading', '<?php echo $row['id'] ?>')" style="cursor:pointer"><img src="../images/icon-del1.png" width="10" height="10" border="0"></span></div>   
                                            </div>
                                            <?php 
                                        } ?>
                                    </div>

                                    <?php
                                    $query2 = "SELECT * from scope_brief_description WHERE `heading_id` = '" . $row['id'] . "' AND `brief_id` ='" . $BID . "' AND company_id= '$cid'";
                                    $results = mysql_query($query2);
                                    $row2 = mysql_fetch_object($results);
                                    ?> 
                                    <div class="col-md-12">
                                        <div id="ptext-<?php echo $row['id']; ?>" >

                                            <div class="col-md-1 col-sm-2 panel-numbering" style="padding:0 3px;float:left">&nbsp;</div> 
                                            <div class="col-md-10 com-sm-8 "  id="btextf-<?php echo $row['id'] ?>"><?php echo @$row2->description ?> </div>
 <?php if ($bstatus != 3) { ?>
                                            <div  class="col-md-1 col-sm2 panel-edit" style="float:right; cursor:pointer" onclick="openedit('text', '<?php echo $row['id'] ?>')">
                                                <div class="glyph">
                                                    <i class="fa fa-paragraph" aria-hidden="true"></i>
                                                </div> 

                                            </div>
                                            <?php } ?>
                                        </div>
                                        <div class="editfield" id="pedittext-<?php echo $row['id']; ?>" >
                                            <div class="col-md-1 col-sm-2 panel-numbering" style="padding:0 3px;float:left">&nbsp;</div> 
                                            <form method="post" action="" id="submittextfor-<?php echo $row['id']; ?>">
                                                <div class="col-md-10 com-sm-8"><textarea name="btext" required class="ckeditor" id="btext-<?php echo $row['id'] ?>"> <?php echo @$row2->description ?></textarea></div>
                                                <input type="hidden" name="bdescid" value="<?php echo @$row2->id ?>">
                                                <input type="hidden" name="headingid" value="<?php echo $row['id'] ?>">
                                                <input type="hidden" name="scopename" value="<?php echo $scope ?>">
                                                <input type="hidden" name="task" value="changedesc">
                                                <input type="hidden" name="brief_id" value="<?php echo $BID ?>">
                                            </form>  
                                            <div class="col-md-1 col-sm2 panel-edit" style="float:right"><a style="cursor:pointer" href="javascript:void(0)" onclick="submittextform('<?php echo $row['id'] ?>')"><input type="image" src="../images/icon-save7.png" width="12" height="12" border="0"></a>|<span onclick="closeedit('text', '<?php echo $row['id'] ?>')" style="cursor:pointer"><img src="../images/icon-del1.png" width="10" height="10" border="0"></span></div> 
                                            
                                          
                                            
                                        </div>
                                    </div>
                                </div>

                            </div>

    <?php $i = $i + 1; } 
	
	?>
<?php if ($bstatus != 3) { ?>
<div class="formRow fRow" style="padding:21px 0 !important">
                            <div class="col-md-12">
                                <div class="col-md-12">

                                    <div class="newheading" id="newheading" >
                                        <div class="col-md-1 col-sm-2  panel-numbering" style="padding:0 3px;float:left">New Heading</div>
                                        <form id="newheadingform" method="post" action="" onsubmit="savenewheading()">
                                            <div class="col-md-10 com-sm-8">
                                                <input type="text" name="nheading" value="" /></div>
                                            <input type="hidden" name="scopename" value="<?php echo $scope ?>">
                                            <input type="hidden" name="task" value="newheading">
                                            <input type="hidden" name="brief_id" value="<?php echo $BID ?>">
											<input type="image" src="../images/icon-save7.png" width="12" height="12" border="0">
                                        </form>
                                        <!--<div class="col-md-1 col-sm-2 panel-edit" style="float:right"><a href="#" onclick="savenewheading()"><input type="image" src="../images/icon-save7.png" width="12" height="12" border="0"></a></div>-->
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

<?php } } ?>


            </div>
        </div>
    </div>
</section>
<script>
            function openedit(str, id){

            var eid = "pedit" + str + "-" + id;
                    var pid = "p" + str + "-" + id;
                    $("#" + pid).hide();
                    $("#" + eid).show();
            }
    function closeedit(str, id){

    var eid = "pedit" + str + "-" + id;
            var pid = "p" + str + "-" + id;
            $("#" + eid).hide();
            $("#" + pid).show();
    }

    function submittextform(id){

    var ht = $('body .cke_editable.cke_contents_ltr').html();
            $("#btext-" + id).val(ht);
            for (instance in CKEDITOR.instances) {
    CKEDITOR.instances[instance].updateElement();
    }
    formData = $("#submittextfor-" + id).serialize();
            url = "briefActions.php";
            $.ajax({
            url: url,
                    type: 'POST',
                    data: formData,
                    success: function (data) {
                    if (data == 0) {
                    closeedit('text', id);
                            $("#alert-" + id).html("Descripton updated successfully with empty data");
                            $("#alert-" + id).show();
                            setTimeout(function () {
                            $("#alert-" + id).hide();
                            }, 2000);
                    }

                    else {
                    $("#btextf-" + id).html(data);
                            closeedit('text', id);
                            $("#alert-" + id).html("Descripton updated successfully");
                            $("#alert-" + id).show();
                            setTimeout(function () {
                            $("#alert-" + id).hide();
                            }, 2000);
                    }
                    },
                    error: function (e) {

                    }
            });
    }

    function submitheadingform(id){

    formData = $("#formforheading-" + id).serialize();
            url = "briefActions.php";
            $.ajax({
            url: url,
                    type: 'POST',
                    data: formData,
                    success: function (data) {
                    if (data == 0) {

                    alert('error');
//                  location.reload();
                    }

                    else {
                    $("#headingf-" + id).html(data);
                            closeedit('heading', id);
                            $("#alert-" + id).html("Heading updated successfully");
                            $("#alert-" + id).show();
                            setTimeout(function () {
                            $("#alert-" + id).hide();
                            }, 2000);
                    }
                    },
                    error: function (e) {

                    }
           });
    }


    function savenewheading(){

    formData = $("#newheadingform").serialize();
            url = "briefActions.php";
            $.ajax({
            url: url,
                    type: 'POST',
                    data: formData,
                    success: function (data) {


                    if (data == 1) {

                    alert(" Added Successfully");
                            location.reload();
                    }

                    else {
                    alert(data);
                    }
                    },
                    error: function (e) {

                    }
            });
    }

    function deleteHeading(id){
    if (id > 0) {
    alertify.confirm("Are you sure you want to delete heading.", function (e) {
    if (e) {
    url = "briefActions.php";

    $.ajax({
    url: url,
            type: 'POST',
            data: {task:'deleteheading', hid:id},
            success: function (data) {
            if (data == 1) {
            location.reload();
            }

            else {
            alert("Something went wrong. Please try later");
            }
            },
            error: function (e) {

            }
    });
    } else {
    return 0;
    }


    });
    }
   
    }
</script>

<?php include('../includes/footer.php'); ?>
