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

if(isset($_POST['btn_show_edt_form_exst_cst_data']))
{
    $edt2 = strip_tags(trim($_POST['edt2']));

    $stmt = $con->prepare("SELECT id, reg_date, tele_id, company_name, contact_person, addline1, addline2, designation, mob1, mob2, country, state, district, loc, email, status FROM customers_det WHERE id=?");
    $stmt -> bind_param("i",$edt2);
    $stmt -> execute();
    $stmt -> store_result();
    $stmt -> bind_result($id,$reg_date, $tele_id, $company_name, $contact_person, $addline1, $addline2, $designation, $mob1, $mob2, $country, $state, $district, $loc, $email, $status);
    $all_posts = Array();
    while($stmt->fetch())
    {
        $individual_post = Array(
            'id' => $id,
            'reg_date'       => $reg_date,
            'tele_id'        => $tele_id,
            'company_name'   => $company_name,
            'contact_person' => $contact_person,
            'addline1'       => $addline1,
            'addline2'       => $addline2,
            'designation'    => $designation,
            'mob1'           => $mob1,
            'mob2'           => $mob2,
            'country'        => $country,
            'state'          => $state,
            'district'       => $district,
            'loc'            => $loc,
            'email'          => $email,
            'status'         => $status

        );
        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>
    <script>
        $(".panel-body :input").attr("disabled", true);
    </script>
    <form class="form-horizontal form-label-left">

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

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

            <div class="col-md-3 col-sm-12 col-xs-12">
                <input type="email" id="cst_email" name="cst_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="cst_address_line1" name="cst_address_line1" value="<?php if(!empty($individual_post['addline1'])){ echo $individual_post['addline1']; } ?>"  required="required" class="form-control" placeholder="Address Line1*">
            </div>
            <input type="hidden" name="csd" id="csd" 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="cst_company" name="cst_company" value="<?php if(!empty($individual_post['company_name'])){ echo $individual_post['company_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="cst_contact_person" name="cst_contact_person" value="<?php if(!empty($individual_post['contact_person'])){ echo $individual_post['contact_person']; } ?>" required="required" class="form-control" placeholder="Contact Person*">
            </div>

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

            <div class="col-md-3 col-sm-12 col-xs-12">
                <input type="text" id="cst_address_line2" name="cst_address_line2" value="<?php if(!empty($individual_post['addline2'])){ echo $individual_post['addline2']; } ?>" 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="country1 form-control " id="cst_country" name="cst_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="state1 form-control" id="cst_state" name="cst_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="district1 form-control" id="cst_district" name="cst_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="cst_location" name="cst_location" value="<?php if(!empty($individual_post['loc'])){ echo $individual_post['loc']; } ?>" required="required" class="form-control" placeholder="Location*">
            </div>
        </div>
        </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="cst_pro_cat" name="cst_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="cst_domain" name="cst_domain" class="form-control" placeholder="Domain">
            </div>

            <div class="col-md-3 col-sm-12 col-xs-12">
                <input type="text" id="cst_qtn_no" name="cst_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="cst_source" name="cst_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="bdo1 form-control" required="required" id="cst_bdo" name="cst_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="cst_est_date" name="cst_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="cst_ord_amt" name="cst_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="cst_adv_amt" name="cst_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="cst_amt_type" name="cst_amt_type" required="required" onchange="exst_cst_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="cst_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="cst_order_det" style="min-height: 100px;" name="cst_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_exst_cst_data();"  class="btn btn-primary ">Save & Next</button>
        </div>
    </div>
    </form>
<?php
}

if(isset($_POST['btn_show_type_wse_exst_cst_data']))
{
    ?>
    <div class="col-md-3 col-sm-12 col-xs-12">
        <input type="text" id="cst_chq_no" name="cst_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="cst_chq_dt" name="cst_chq_dt" required="required" class="form-control" placeholder="Cheque Date*">
    </div>
    <?php
    exit();
}
if(isset($_POST['btn_reg_data_exst_cst']))
{
    $chq_dt="";
    $csd = strip_tags(trim(base64_decode($_POST['csd'])));
    $cst_pro_cat = strip_tags(trim($_POST['cst_pro_cat']));
    $cst_domain = strip_tags(trim($_POST['cst_domain']));
    $cst_qtn_no = strip_tags(trim($_POST['cst_qtn_no']));
    $cst_source = strip_tags(trim($_POST['cst_source']));
    $cst_bdo = strip_tags(trim($_POST['cst_bdo']));
    $cst_est_date = strip_tags(trim($_POST['cst_est_date']));
    $cst_ord_amt = strip_tags(trim($_POST['cst_ord_amt']));
    $cst_adv_amt = strip_tags(trim($_POST['cst_adv_amt']));
    $cst_amt_type = strip_tags(trim($_POST['cst_amt_type']));
    $cst_chq_no = strip_tags(trim($_POST['cst_chq_no']));
    $cst_chq_dt = strip_tags(trim($_POST['cst_chq_dt']));
    $cst_order_det = strip_tags(trim($_POST['cst_order_det']));
    $date = date('Y-m-d');

    if (!empty($csd) && !empty($cst_pro_cat) && !empty($cst_bdo) && !empty($cst_est_date) && !empty($cst_ord_amt) && !empty($cst_adv_amt) && !empty($cst_amt_type) && !empty($cst_order_det))
    {

        $sta = 1 ;
        $stmt03 = $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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        $stmt03 -> bind_param("siiisssssssissi",$date,$csd,$cst_bdo,$cst_pro_cat,$cst_domain,$cst_qtn_no,$cst_source,$cst_est_date,$cst_ord_amt,$cst_adv_amt,$cst_order_det,$cst_amt_type,$cst_chq_dt,$cst_chq_no,$sta);
        $stmt03 -> execute();
        $stmt03 -> store_result();
        $last_ord_id1 = $stmt03->insert_id;
        //$affect = $stmt03 -> affected_rows;
        /*if($affect > 0)
        {
            ?><script>alert('created..!')</script><?php
        }
        else
        {
            ?><script>alert('Error..!')</script><?php
        }*/

    }
    else
    {
      ?><script>alert('Error..!');</script><?php
    }


    if(!empty($last_ord_id1))
    {
        ?>
        <script>
            upld_materials_exst_cst(<?php echo $last_ord_id1; ?>);
        </script>
    <?php
    }

    exit();
}
if(isset($_POST['btn_upld_data_exst_cst']))
{
    $exst_cst_ord = strip_tags(trim($_POST['exst_cst_ord']));
    ?>
    <form action="upload01.php" method="post" id="myForm" enctype="multipart/form-data">
        <label for="file">Files:</label><br>
        <input type="file" name="files[]" id="files" multiple><br>
        <input type="hidden" name="od1" value="<?php echo base64_encode($exst_cst_ord); ?>">
        <input type="submit" name="submit" class="btn btn-success" value="Complete">
    </form>

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