<style>
    .text-white{
        color: white;
    }
    a{
        color: #4c0870;
    }
</style>
<link href='<?php echo THEME_PATH; ?>plugins/fullcalendar-4.2.0/packages/core/main.css' rel='stylesheet' />
<link href='<?php echo THEME_PATH; ?>plugins/fullcalendar-4.2.0/packages/daygrid/main.css' rel='stylesheet' />
<link href='<?php echo THEME_PATH; ?>plugins/fullcalendar-4.2.0/packages/timegrid/main.css' rel='stylesheet' />
<link href='<?php echo THEME_PATH; ?>plugins/fullcalendar-4.2.0/packages/list/main.css' rel='stylesheet' />
<script src='<?php echo THEME_PATH; ?>plugins/fullcalendar-4.2.0/packages/core/main.js'></script>
<script src='<?php echo THEME_PATH; ?>plugins/fullcalendar-4.2.0/packages/interaction/main.js'></script>
<script src='<?php echo THEME_PATH; ?>plugins/fullcalendar-4.2.0/packages/daygrid/main.js'></script>
<script src='<?php echo THEME_PATH; ?>plugins/fullcalendar-4.2.0/packages/timegrid/main.js'></script>
<script src='<?php echo THEME_PATH; ?>plugins/fullcalendar-4.2.0/packages/list/main.js'></script>
<link href="<?php echo THEME_PATH; ?>css/pipeline.css" rel="stylesheet" type="text/css"/>
<script src="<?php echo THEME_PATH; ?>js/general.js"></script>
<link href="<?php echo THEME_PATH; ?>css/crm-profile.css" rel="stylesheet" type="text/css"/>
<script src="<?php echo THEME_PATH; ?>js/jquery.timeago.min.js"></script>
<link href="<?php echo THEME_PATH; ?>plugins/icheck/icheck.css" rel="stylesheet" type="text/css"/>
<script src="<?php echo THEME_PATH; ?>plugins/icheck/icheck.min.js" type="text/javascript"></script>
<style>
    input[type=checkbox]{
        display:initial !important;
    }
</style>
<div class="page-inner">
    <div id="main-wrapper">
        <form id="formdeal2">
            <input type="hidden" id="current_deal_id" name="deal_id" value="<?php echo $dealDetails['deal_id'];?>">
            <input type="hidden" id="current_contact_id" name="current_contact_id" value="<?php echo $dealDetails['contact_id'];?>">
            <input type="hidden" id="org_id" name="organization_id" value="<?php echo $dealDetails['organization_id'];?>">
            <input type="hidden" id="stage_id" name="stage_id" value="<?php echo $dealDetails['stage_id'];?>">
            <input type="hidden" id="current_origin" name="current_origin" value="deal_prof">
        </form>
        <div class="col-md-12 m-t-lg">
            <div class="profile-timeline">
                <ul class="list-unstyled" id="activitylist">
                    <li class="timeline-item" id="dealbar">
                        <div class="panel panel-white">
                            <div class="panel-body">
                                <div class="timeline-item-header" id="modals">
                                    <!--                                    < ?php echo $dealDetails['stage_id']!= 3 ? 'hidden' :'';?> -->
                                    <div id="choosenstate" deal-state="<?php echo $dealDetails['deal_state'];?>" class=""> <span>
                                        <a class="btn btn-success hidden" data-state="Won"> <?php print CommonLanguage::getMessage('won'); ?></a>
                                        <a data-toggle="modal" data-target="#reason-modal" class="btn btn-danger hidden" data-state="Lost"><?php print CommonLanguage::getMessage('lost'); ?></a>
                                        <a  id="revertstate"class="btn btn-info hidden" data-state="Incomplete"><?php print CommonLanguage::getMessage('change'); ?></a>
                                        </span>
                                        <!--                                        <a class="btn btn-danger removedeal hidden">X</a>-->
                                    </div>
                                    <h3  class="bold"><?php echo $dealDetails['deal_title'];?></h3>
                                    <h3> 
                                        <span class="semi-bold"><?php echo $dealDetails['deal_value'] ? $dealDetails['deal_value'].' '.$dealDetails['Currency_Name'] : '';?></span>
                                        <i class="fa fa-user"></i>
                                        <a href="<?php SITE_LINK;?>crm/contactProfile/<?php echo $dealDetails['contact_id'];?>"><?php echo $dealDetails['contact_name'];?></a> <i class="fa fa-building"></i>
                                        <a href="<?php SITE_LINK;?>crm/customerProfile/<?php echo $dealDetails['organization_id'];?>"><?php echo $dealDetails['organization_customer_name'];?></a> 

                                    </h3>
                                </div>
                                <div class="timeline-item-post">
                                    <div class="miniPipelineWrapper">
                                        <div class="pipelineSummary">
                                            <div class="pipelineStages open">
                                                <ul id="stages">
                                                    <li data-stage-id="1" class="active last">
                                                        <div class="stageContent">
                                                            <span class="stagename">0 days</span>
                                                            <span class="stageArrow"></span>
                                                        </div>
                                                    </li>
                                                    <li data-stage-id="2" class="">
                                                        <div class="stageContent">
                                                            <span class="stagename"></span>
                                                            <span class="stageArrow"></span>
                                                        </div>
                                                    </li>
                                                    <li data-stage-id="3" class="">
                                                        <div class="stageContent">
                                                            <span class="stagename"></span>
                                                            <span class="stageArrow"></span>
                                                        </div>
                                                    </li>
                                                </ul>
                                            </div>
                                            <div class="additionalInfo">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </li>
                </ul>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <div class="panel panel-default">
                        <div class="panel-body"><span class="title bold"><?php print CommonLanguage::getMessage('organization'); ?></span>
                            <hr>
                            <div ><i class='fa fa-3x fa-building'></i> <?php print CommonLanguage::getMessage('Name'); ?>:
                                <a  data-type="text" class="customer_profile_name" href="<?php  echo SITE_LINK.'crm/customerProfile/'.$dealDetails['organization_id'];?>"><?php echo $dealDetails['organization_customer_name'];?></a>
                                <div>
                                <?php print CommonLanguage::getMessage('Phone'); ?>:
                                    <a  data-type="text" ><?php echo $dealDetails['orgaization_mobile_phone'];?></a>
                                    <br> <?php print CommonLanguage::getMessage('address'); ?>:
                                    <a  data-type="text" ><?php echo $dealDetails['organization_address'];?></a>
                                    <br> <?php print CommonLanguage::getMessage('is_potiential'); ?>: 
                                    <a  data-type="text" ><?php echo CommonLanguage::getMessage($dealDetails['is_potiential'] == 0 ? 'no' : 'yes');?></a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="panel panel-default">
                        <div class="panel-body"><span class="title bold"><?php print CommonLanguage::getMessage('Person'); ?></span>
                            <hr>
                            <div id=persondata><i class=' fa fa-3x fa-user'></i>  <?php print CommonLanguage::getMessage('Name'); ?>:
                                <a id="CName" data-type="text"  data-title="Edit address" href="<?php  echo SITE_LINK.'crm/contactProfile/'.$dealDetails['contact_id'];?>"><?php echo $dealDetails['contact_name'];?></a>
                                <div>
                                <?php print CommonLanguage::getMessage('Phone'); ?>:
                                    <a id="CMobile" data-type="text"  data-title="Edit address"><?php echo $dealDetails['contact_mobile_phone'];?></a>
                                    <br> <?php print CommonLanguage::getMessage('work_phone'); ?>:
                                    <a id="CWPhone" data-type="text"  data-title="Edit address"><?php echo $dealDetails['contact_work_phone'];?></a>
                                </div>
                                <?php print CommonLanguage::getMessage('Email'); ?>:
                                <a id="CEmail" data-type="text"  data-title="Edit address"><?php echo $dealDetails['contact_email'];?></a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-8"> 
                    <span id="activitylistt2">		
                        <div class='panel panel-default'> 
                            <div class="panel-body">
                                <span>
                                    <a class='btn btn-warning add btn-activity2 ' data-toggle="modal" data-target="#activity_modal" data-origin="deal_prof" id="add_activity"  data-event-name='add' data-customer-id="<?php echo $dealDetails['organization_id'];?>" data-customer-name="<?php echo $dealDetails['organization_customer_name'];?>"  data-contact-id="<?php echo $dealDetails['contact_id'];?>" data-contact-name="<?php echo $dealDetails['contact_name'];?>" data-deal-id="<?php echo $dealDetails['deal_id'];?>" data-deal-title="<?php echo $dealDetails['deal_title'];?>">
                                    
                                    <?php print CommonLanguage::getMessage('add_activity'); ?></a>
                                    <span class="noteseditor btn btn-complete"><?php print CommonLanguage::getMessage('add_notes'); ?></span>
                                    <a style="color:white !important;" class='btn bg-info-light text-white ' data-toggle="modal" data-target="#current_call_modal" ><?php print CommonLanguage::getMessage('current_call'); ?></a>
                                </span>
                                <div id="noteseditor" class="hidden">
                                    <div class="row"> 
                                        <div class="col-md-12 m-t-10 m-b-10">
                                            <textarea id="overall_note" class="form-control"></textarea>
                                        </div>
                                    </div>
                                    <div class="btn-notes">
                                        <a class='btn btn-primary save'><?php print CommonLanguage::getMessage('Save'); ?></a> 
                                        <a class='btn btn-default cancel'><?php print CommonLanguage::getMessage('cancel'); ?></a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </span>
                    <h3 id='planevent'><?php print CommonLanguage::getMessage('planned'); ?></h3>
                    <span data-type=plan>
                        <div   id="vertical-timeline" class="vertical-container light-timeline no-margins">
                        </div>
                    </span>
                    <h3 id='over'><?php print CommonLanguage::getMessage('overdue'); ?></h3>
                    <span data-type=overdue>
                        <div id="vertical-timeline" class="vertical-container light-timeline no-margins">
                        </div>
                    </span>
                    <h3><?php  echo CommonLanguage::getMessage('log');?></h3>
                    <div id="logfilter" class="btn-group">
                        <a data-filter-type=All class="btn btn-default active"><?php  echo CommonLanguage::getMessage('all_log');?></a>
                        <a data-filter-type=activity class="btn btn-default"><?php  echo CommonLanguage::getMessage('activity');?></a>
                        <a data-filter-type=log class="btn btn-default "><?php  echo CommonLanguage::getMessage('log');?></a>
                        <a data-filter-type=statelog class="btn btn-default "><?php  echo CommonLanguage::getMessage('statelog');?></a>
                        <a data-filter-type=notelog class="btn btn-default "><?php  echo CommonLanguage::getMessage('notes');?></a>
                    </div>
                    <span id='past'>
                        <div id="vertical-timeline" class="identify vertical-container light-timeline no-margins">
                        </div>
                    </span>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var filterlogtype='All',activity_flag=2,reason_modal_flag = 1,
        is_moved =<?php echo json_encode($dealDetails['is_moved']);?>,
            default_stage_change_deal_state = <?php  echo json_encode(smc_session()['default_setting']['default_stage_change_deal_state']);?>;
            $(function(){
                getDatesCurrentDeal()
                initialState($("#choosenstate").attr("deal-state"))
                loadlog('deal_prof', $("#org_id").val(),$("#current_contact_id").val(),$("#current_deal_id").val());
                loadActivities(0,$("#current_deal_id").val(),0)
                $("#stages li").removeClass("last active");
                $("[data-stage-id=" +  $("#stage_id").val()+ "]").prevAll().addClass("active");
                $("[data-stage-id=" +  $("#stage_id").val() + "]").addClass("last active");
                $("#dealsdialog").on("click",".add-deal-profile",function(){
                    $("#deal-modal").modal('show')
                });
                $(".btn-notes a.cancel").click(function(e) {
                    $("#noteseditor").addClass('hidden');
                });
                $(".btn-notes a.save").click(function(e) {
                    if ($("#overall_note").val() != "") {
                        savenote("crmAjax/addNote", $("#org_id").val(),$("#current_deal_id").val(),$("#current_contact_id").val(),function(){
                            loadlog('deal_prof', $("#org_id").val(),$("#current_contact_id").val(),$("#current_deal_id").val());
                        });
                        $("#noteseditor").addClass('hidden');
                    }
                });
                $(".noteseditor").on("click", function() {
                    $("#noteseditor").removeClass('hidden');
                });
                $("#logfilter").on("click", " a", function (event) {
                    $("#logfilter a").removeClass("active");
                    $(this).addClass("active");
                    var currentheight = $(document).height();
                    $("body").attr("style", "height:" + currentheight + "px !important");
                    event.preventDefault();
                    event.stopPropagation();
                    switch ($(this).attr("data-filter-type")) {
                        case "All":
                            filterlogtype = "All";
                            break;
                        case "activity":
                            filterlogtype = "activity";
                            break;
                        case "log":
                            filterlogtype = "log";
                            break;
                        case "statelog":
                            filterlogtype = "statelog";
                            break;
                        case "notelog":
                            filterlogtype = "notelog";
                            break;
                                                            }
                    loadlog('deal_prof', $("#org_id").val(),$("#current_contact_id").val(),$("#current_deal_id").val());
                });
                $("#choosenstate a").on("click", function () {
                    var state = $(this).attr("data-state");
                    if (state == "Lost") {
                        //show reason modal
                        $("#reason_deal_id").val($("#current_deal_id").val())
                        //parentThis.updatestate(id, state);
                    } else if (state == "Won") {
                        initialState(state);
                        //update the state
                        updatestate($("#current_deal_id").val(), state);
                    } else {
                        initialState(state);
                        updatestate($("#current_deal_id").val(), "Incomplete");
                    }
                });
                $("#main-wrapper").on("click", "#stages li", function (event) {
                    event.stopPropagation();
                    if ($(this).attr("data-stage-id") != $("#stage_id").val()) {
                        $("#stages li").removeClass("last active");
                        $(this).prevAll().addClass("active");
                        $(this).addClass("last active");
                        $("#stage_id").val($(this).attr("data-stage-id"));
                        updatedealsaccordingtoprogressbar($(this).attr("data-stage-id"));
                    }
                });
            })
    function initialState(state){
        if (state == "Won" || state == "Lost") {
            $("#choosenstate a").addClass('hidden');
            $("#revertstate").removeClass('hidden');
        } else {
            if($("#stage_id").val() == default_stage_change_deal_state){
                $("#choosenstate a").removeClass('hidden')
                $("#revertstate").addClass('hidden');
            }
        }
    }
    function updatestate(id, state){
        if (state != "") {
            $.ajax({
                type: "POST",
                url: "crmAjax/submitDealState",
                data: {
                    //id of the deal
                    deal_id: id,
                    org_id:$("#org_id").val(),
                    //new state
                    deal_state: state,
                    reason_for_loss: ""
                },
                dataType:"json",
                success: function (data) {
                    console.log(data)
                    //refilling data
                    loadlog('deal_prof', $("#org_id").val(),$("#current_contact_id").val(),$("#current_deal_id").val());
                    //ParentThis.reasonmodal.modal("hide");
                }
            });
        }
    }
    function updatedealsaccordingtoprogressbar(stage_id){
        $.ajax({
            //url to submit data to it
            url: "crmAjax/updateDealStage",
            type: "POST",
            contentType: false,
            //important so it donot process data as a string
            processData: false,
            data: new FormData(document.getElementById("formdeal2")),
            success: function (data) {
                getDatesCurrentDeal();
                if(stage_id == default_stage_change_deal_state){
                    $("#choosenstate a").removeClass('hidden');
                    $("#revertstate").addClass('hidden');
                }else{
                    $("#choosenstate a").addClass('hidden');
                    $("#revertstate").addClass('hidden');
                }
            },
            error: function () {
                console.log("something wrong occured");
            }
        });
    }
    function getDatesCurrentDeal(){
        $.ajax({
            dataType: "JSON",
            type: "POST",
            url: "crmAjax/dealTransaction",
            data: {
                deal_id: $("#current_deal_id").val()
            },
            success: function (data) {
                var a
                $.each(data, function (index, element) {
                    var today = moment();
                    var date = element['progress_date'];
                    if (date.trim() != "0000-00-00 00:00:00")
                        $($(".stagename")[index]).text(today.diff(date, 'days') + " Days");
                    else {
                        $($(".stagename")[index]).text("");
                    }
                });
            }
        });

    }
</script>
<?php
include 'Views/crm/activity_modal.php';
include 'Views/crm/reason_modal.php';
include 'Views/crm/current_call_modal.php';