<?php

include "../../pidbco/pidbjoin.php";

if(isset($_POST['btn_show_type_wse_data']))
{
    ?>
    <div class="col-md-3 col-sm-12 col-xs-12">
        <input type="text" id="chq_no" name="chq_no" required="required" class="form-control" placeholder="Cheque Number*">
    </div>

    <div class="col-md-3 col-sm-12 col-xs-12">
        <input type="date" id="chq_dt" name="chq_dt" required="required" class="form-control" placeholder="Cheque Date*">
    </div>
    <?php
    exit();
}

if(isset($_POST['btn_show_edt_form_data']))
{
    $edt = strip_tags(trim($_POST['edt']));
    $cst_not = 0;

    $stmt = $con->prepare("SELECT id, comp_name, person_name, phone, phone2, country, state, district, location, executive_bdo, email,cust_or_not FROM tele_calls_det WHERE cust_or_not=? AND id=?");
    $stmt -> bind_param("ii",$cst_not,$edt);
    $stmt -> execute();
    $stmt -> store_result();
    $stmt -> bind_result($id, $comp_name, $person_name, $phone, $phone2, $country, $state, $district, $location, $executive_bdo, $email,$cust_or_not);
    $all_posts = Array();
    while($stmt->fetch())
    {
        $individual_post = Array(
            'id' => $id,
            'comp_name' => $comp_name,
            'person_name' => $person_name,
            'phone' => $phone,
            'phone2' => $phone2,
            'country'   => $country,
            'state'   => $state,
            'district'   => $district,
            'location'   => $location,
            'executive_bdo'   => $executive_bdo,
            'email'   => $email,
            'cust_or_not'   => $cust_or_not

        );
        array_push($all_posts, $individual_post);
    }
    $stmt->close();
?>
    <!-- select2 -->
    <script src="../components/js/select/select2.full.js"></script>
    <script>
        $(document).ready(function () {
            $(".country").select2({
                placeholder: "Select a Country",
                allowClear: true
            });
            $(".state").select2({
                placeholder: "Select a State",
                allowClear: true
            });
            $(".district").select2({
                placeholder: "Select a district",
                allowClear: true
            });
            $(".bdo").select2({
                placeholder: "Select a Telecaller (BDO)*",
                allowClear: true
            });
        });
    </script>

    <!-- /select2 -->
    <script type="text/javascript">
        $(document).ready(function () {
            $('#est_date').daterangepicker({
                singleDatePicker: true,
                calender_style: "picker_4"
            }, function (start, end, label) {
                console.log(start.toISOString(), end.toISOString(), label);
            });
        });
    </script>
    <form class="form-horizontal form-label-left">

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

            <div class="form-group form-group-sm">
                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="number" id="phone1" disabled name="phone1"  value="<?php if(!empty($individual_post['phone'])){ echo $individual_post['phone']; } ?>" required="required" class="form-control" placeholder="Primary Phone*">
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="number" id="phone2" name="phone2" value="<?php if(!empty($individual_post['phone2'])){ echo $individual_post['phone2']; } ?>"  class="form-control" placeholder="Secondary Phone">
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="email" id="email" name="email" value="<?php if(!empty($individual_post['email'])){ echo $individual_post['email']; } ?>"  class="form-control" placeholder="Email">
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="text" id="address_line1" name="address_line1"  required="required" class="form-control" placeholder="Address Line1*">
                </div>
                <input type="hidden" name="td" id="td" value="<?php echo base64_encode($individual_post['id']); ?>">
            </div>

            <div class="form-group form-group-sm">
                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="text" disabled id="company" name="company" value="<?php if(!empty($individual_post['comp_name'])){ echo $individual_post['comp_name']; } ?>" required="required" class="form-control" placeholder="Company Name*">
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="text" id="contact_person" name="contact_person" value="<?php if(!empty($individual_post['person_name'])){ echo $individual_post['person_name']; } ?>" required="required" class="form-control" placeholder="Contact Person*">
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="text" id="designation" name="designation"   class="form-control" placeholder="Designation">
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="text" id="address_line2" name="address_line2" class="form-control" placeholder="Address Line2">
                </div>
            </div>

            <div class="form-group form-group-sm">
                <div class="col-md-3 col-sm-12 col-xs-12">
                    <select class="country form-control " id="country" name="country" tabindex="-1">
                        <?php
                        if(!empty($individual_post['country']))
                        {
                            ?>
                            <option value="<?php echo $individual_post['country']; ?>"><?php echo $individual_post['country']; ?></option>
                        <?php
                        }
                        ?>
                        <option value=""></option>
                        <?php
                        $stmt01 = mysqli_query($con,"SELECT * FROM `countries` ORDER BY id ASC");
                        while($res01 = mysqli_fetch_object($stmt01))
                        {
                            ?>
                            <option value="<?php echo $res01->country_name; ?>"><?php echo $res01->country_name; ?></option>
                        <?php
                        }
                        ?>
                    </select>
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <select class="state form-control" id="state" name="state" tabindex="-1">
                        <?php
                        if(!empty($individual_post['state']))
                        {
                            ?>
                            <option value="<?php echo $individual_post['state']; ?>"><?php echo $individual_post['state']; ?></option>
                        <?php
                        }
                        ?>
                        <option value=""></option>
                        <?php
                        $stmt01 = mysqli_query($con,"SELECT * FROM `states` ");
                        while($res01 = mysqli_fetch_object($stmt01))
                        {
                            ?>
                            <option value="<?php echo $res01->state_name; ?>"><?php echo $res01->state_name; ?></option>
                        <?php
                        }
                        ?>
                    </select>
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <select class="district form-control" id="district" name="district" tabindex="-1">
                        <?php
                        if(!empty($individual_post['district']))
                        {
                            ?>
                            <option value="<?php echo $individual_post['district']; ?>"><?php echo $individual_post['district']; ?></option>
                        <?php
                        }
                        ?>
                        <option value=""></option>
                        <?php
                        $stmt01 = mysqli_query($con,"SELECT * FROM `cities` ");
                        while($res01 = mysqli_fetch_object($stmt01))
                        {
                            ?>
                            <option value="<?php echo $res01->cities_name; ?>"><?php echo $res01->cities_name; ?></option>
                        <?php
                        }
                        ?>
                    </select>
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="text" id="location" name="location" value="<?php if(!empty($individual_post['location'])){ echo $individual_post['location']; } ?>" required="required" class="form-control" placeholder="Location*">
                </div>
            </div>
        </div>
        <div class="col-md-12 ">
            <div class="form-group form-group-sm">
                <div class="col-md-3 col-sm-12 col-xs-12">
                    <select id="pro_cat" name="pro_cat" required="required" class="form-control" placeholder="Product Category*">
                        <option value="">Product Category*</option>
                        <?php
                        $cat = mysqli_query($con,"SELECT id,cat_name,status FROM category_det WHERE status='1'");
                        while($cat_list = mysqli_fetch_object($cat))
                        {
                            ?>
                            <option value="<?php echo $cat_list->id; ?>"><?php echo $cat_list->cat_name; ?></option>
                        <?php
                        }
                        ?>
                    </select>
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="text" id="domain" name="domain" class="form-control" placeholder="Domain">
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="text" id="qtn_no" name="qtn_no" required="required" class="form-control" placeholder="Qtn Number*">
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="text" id="source" name="source" required="required" class="form-control" placeholder="Source*">
                </div>
            </div>
            <div class="form-group form-group-sm">

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <select style="width: 100%;" class="bdo form-control" required="required" id="bdo" name="bdo" tabindex="-1">
                        <option value=""></option>
                        <?php
                        $emp = mysqli_query($con,"SELECT id,emp_name FROM employee_registration");
                        while($emp_list = mysqli_fetch_object($emp))
                        {
                            ?>
                            <option value="<?php echo $emp_list->id; ?>"><?php echo $emp_list->emp_name; ?></option>
                        <?php
                        }
                        ?>
                    </select>
                </div>
                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="date" id="est_date" name="est_date" required="required" class="form-control" placeholder="Estimate Date*">
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="number" id="ord_amt" name="ord_amt" required="required" class="form-control" placeholder="Order Amount*">
                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">
                    <input type="number" id="adv_amt" name="adv_amt" required="required" class="form-control" placeholder="Advance*">
                </div>
            </div>

        </div>
        <div class="col-md-12">
            <div class="form-group form-group-sm">
                <div class="col-md-3 col-sm-12 col-xs-12">
                    <select  id="amt_type" name="amt_type" required="required" onchange="amt_type_chng(this.value);" class="form-control">
                        <option value="">Select Amount Type*</option>
                        <option value="1">Cash</option>
                        <option value="2">Cheque</option>
                    </select>
                </div>
                <div id="chq_data">

                </div>

                <div class="col-md-3 col-sm-12 col-xs-12">

                </div>
            </div>
            <div class="form-group form-group-sm">
                <div class="col-md-12 col-sm-12 col-xs-12">
                    <textarea class="form-control" id="order_det" style="min-height: 100px;" name="order_det" required="required" placeholder="Order Details*"></textarea>
                </div>
            </div>
            <div class="col-md-2 col-sm-2 col-xs-12" style="float: right;" id="btn_data">
                <button type="button" name="save" onclick="save_next_data();"  class="btn btn-primary ">Save & Next</button>
            </div>
        </div>
    </form>
<?php
}
if(isset($_POST['btn_reg_data']))
{
    $chq_dt="";
    $phone1 = strip_tags(trim($_POST['phone1']));        
    $phone2 = strip_tags(trim($_POST['phone2']));
    $email = strip_tags(trim($_POST['email']));
    $address_line1 = strip_tags(trim($_POST['address_line1']));
    $address_line2 = strip_tags(trim($_POST['address_line2']));
    $td = strip_tags(trim(base64_decode($_POST['td'])));
    $company = strip_tags(trim($_POST['company']));
    $contact_person = strip_tags(trim($_POST['contact_person']));
    $designation = strip_tags(trim($_POST['designation']));
    $country = strip_tags(trim($_POST['country']));
    $state = strip_tags(trim($_POST['state']));
    $district = strip_tags(trim($_POST['district']));
    $location = strip_tags(trim($_POST['location']));
    $pro_cat = strip_tags(trim($_POST['pro_cat']));
    $domain = strip_tags(trim($_POST['domain']));
    $qtn_no = strip_tags(trim($_POST['qtn_no']));
    $source = strip_tags(trim($_POST['source']));
    $bdo = strip_tags(trim($_POST['bdo']));
    $est_date = strip_tags(trim($_POST['est_date']));
    $ord_amt = strip_tags(trim($_POST['ord_amt']));
    $adv_amt = strip_tags(trim($_POST['adv_amt']));
    $amt_type = strip_tags(trim($_POST['amt_type']));
    $chq_no = strip_tags(trim($_POST['chq_no']));
    $chq_dt = strip_tags(trim($_POST['chq_dt']));
    $order_det = strip_tags(trim($_POST['order_det']));
    $date = date('Y-m-d');

    if (!empty($phone1) && !empty($address_line1) && !empty($company) && !empty($contact_person) && !empty($td) && !empty($location) && !empty($pro_cat) && !empty($bdo) && !empty($est_date) && !empty($ord_amt) && !empty($adv_amt) && !empty($amt_type) && !empty($order_det))
    {

        /* check data exist */
        $stmt3 = $con->prepare("SELECT company_name FROM customers_det WHERE company_name=?");
        $stmt3 -> bind_param("s",$company);
        $stmt3 -> execute();
        $stmt3 -> store_result();
        $num = $stmt3 -> num_rows;


        $stmt4 = $con->prepare("Select mob1 FROM customers_det WHERE mob1 = ?");
        $stmt4 -> bind_param("s",$phone1);
        $stmt4 -> execute();
        $stmt4 -> store_result();
        $num1 = $stmt4 -> num_rows;

        if($phone2 != "")
        {
            $stmt5 = $con->prepare("Select mob2 FROM customers_det WHERE mob2 = ?");
            $stmt5 -> bind_param("s",$phone2);
            $stmt5 -> execute();
            $stmt5 -> store_result();
            $num2 = $stmt5 -> num_rows;
        }
        if($num > 0 || $num1 >0 || $num2 >0)
        {
            if($num > 0)
            {
                ?><script>alert("Error! Company Name Exist");</script><?php
            }
            if($num1 > 0)
            {
                ?><script>alert("Error! Phone number One Exist");</script><?php
            }
            if($num2 > 0)
            {
                ?><script>alert("Error! Phone number Two Exist");</script><?php
            }

        }
        else
        {
           $st = 1;
            $stmt = $con->prepare("INSERT INTO customers_det(reg_date, tele_id, company_name, contact_person, addline1, addline2, designation, mob1, mob2, country, state, district, loc, email, status) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            $stmt -> bind_param("sissssssssssssi",$date,$td,$company,$contact_person,$address_line1,$address_line2,$designation,$phone1,$phone2,$country,$state,$district,$location,$email,$st);
            $stmt -> execute();
            $stmt -> store_result();
            $aff = $stmt->affected_rows;
            if($aff > 0)
            {
                  mysqli_query($con,"UPDATE tele_calls_det SET cust_or_not = '1' WHERE id = '$td'");

                 $sta = 1 ;
                       $last_id = $stmt -> insert_id;
                       $stmt02 = $con->prepare("INSERT INTO order_det(ord_date, cust_id, bdo_id, pro_cat_id, domain_nm, qtn_num, source, est_date, ord_amt, advance, order_details, amt_type, chq_date, chq_num, status) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
                       $stmt02 -> bind_param("siiisssssssissi",$date,$last_id,$bdo,$pro_cat,$domain,$qtn_no,$source,$est_date,$ord_amt,$adv_amt,$order_det,$amt_type,$chq_dt,$chq_no,$sta);
                       $stmt02 -> execute();
                       $stmt02 -> store_result();
                       $last_ord_id = $stmt02->insert_id;

                       /*$aff_02 = $stmt02->affected_rows;
                       if($aff_02 > 0)
                       {
                           ?><script>alert('Order Created.')</script><?
                       }*/
             }
             else
             {
                 ?><script>alert('Error..!')</script><?php
             }
             $stmt->close();
        }


    }
  if(!empty($last_ord_id))
  {
 ?>
  <script>
      upld_materials(<?php echo $last_ord_id; ?>);
  </script>
<?php
  }
}
if(isset($_POST['btn_upld_data']))
{
    $ord_id = strip_tags(trim($_POST['ord']));
?>
    <form action="upload.php" method="post" id="myForm" enctype="multipart/form-data">
        <label for="file">Files:</label><br>
        <input type="file" name="file[]" id="file" multiple><br>
        <input type="hidden" name="od" value="<?php echo base64_encode($ord_id); ?>">
        <input type="submit" name="submit" class="btn btn-success" value="Complete">
    </form>

    <!--<a class="btn btn-default" href="Marketing-Order-Details">Complete</a>-->
<?php
}
?>