@extends('adminlte::page') @section('content')
<?php Fun::lang() ?>
<style>
    .jconfirm .jconfirm-box div.jconfirm-content-pane .jconfirm-content {
        overflow: hidden;
    }
</style>
<div class="title-page">
    <div class="page-bar">
        <ul class="page-breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Dashboard</a></li>
            <li><a href="#">Construction</a></li>
            <li><a href="#">Manage Contractor</a></li>
            <li><a href="#">Task&Responsibilities</a></li>
            <li><a href="#">Create</a></li>
        </ul>
    </div>
</div>
<div class="page">
    <div class="top-content">
        <div class="row">
            <div class="col-sm-12 col-xs-12">
                <div class="title-page">
                    <div class="pull-left">
                        <h3>Create Task and Responsibilities of Contractor</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12 col-xs-12">

            <div class="form-wrapper">
                {!! Form::open(array('route' => 'task_responsibilities_contractor.store','method'=>'POST', 'enctype'=>'multipart/form-data')) !!}
                <div class="row">
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Contructor Name </label>
                        {!! Form::select('contractor_id', $contractors, null, ['class' => 'form-control select2 contractor_id','required']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Project Name </label>
                        {!! Form::select('project_id', $projects, null, ['class' => 'form-control select2','required']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Zone Name </label>
                        {!! Form::select('zone_name', $zones,null, ['class' => 'form-control select2','required']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Date</label>
                        {!! Form::text('date', date('d-m-Y'), array('laceholder'=>'Date','class' => 'form-control date_picker','required')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Start Work Date</label>
                        {!! Form::text('start_work_date', date('d-m-Y'), array('laceholder'=>'Date','class' => 'form-control date_picker','required')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Work Group Type </label>
                        {!! Form::select('work_group_type_id', $work_group_types, null, ['class' => 'form-control select2','required']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Total Amount</label>
                        {!! Form::text('total_amount', 0, array('placeholder' => 'Total Amount','class' => 'form-control number-only main-total-amount','required')) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12" style="display: none;">
                        <label class="required">Deposit Amount</label>
                        {!! Form::text('deposit_amount', 0, array('placeholder' => 'Amount','class' => 'form-control number-only','required')) !!}
                    </div>
                    {{-- <div class="form-group col-sm-4 col-xs-12">
                        <label>Description</label>
                        {!! Form::textarea('description_boq', null, array('placeholder' => 'Description','class' => 'form-control','rows'=>'2')) !!}
                    </div> --}}
                </div>
                <div id="bwi-append">
                    <br/><br/>
                    <h3 class="contract-constructor">Building & Work Info</h3>
                    <div class="row contract-constructor" style="padding: 15px;">
                        <table class="table table-striped table-bordered" width="100%">
                            <thead>
                                <tr>
                                    <th>
                                        <label class="required">
                                            បរិយាយ
                                        </label>
                                    </th>
                                    <th>
                                        <label class="required">
                                            ទិន្នន័យ
                                        </label>
                                    </th>
                                    <th>
                                        <label>
                                            ផ្សេងៗ
                                        </label>
                                    </th>
                                    <th>
                                        <span class="pull-right button_primary btn-sm plus_ens fa fa-plus" onclick="add_row_buliding_work(this)"></span>
                                    </th>
                                </tr>
                            </thead>
                            <tbody class="tb_building_work">
                                <tr>
                                    <td>
                                        {!! Form::text('type_of_house_title', 'ប្រភេទផ្ទះ', array('placeholder' => 'Type','class' => 'form-control','required','readonly')) !!}
                                    </td>
                                    <td>
                                        {!! Form::text('type_of_house', null, array('placeholder' => 'បញ្ចូលប្រភេទផ្ទះ','class' => 'form-control','required')) !!}
                                    </td>
                                    <td>
                                        {!! Form::textarea('des_type_house', null, array('placeholder' => 'ផ្សេងៗ','class' => 'form-control','rows'=>'2')) !!}
                                    </td>
                                    <td><button class="btn btn-sm btn-danger" disabled>
                                        <span class="fa fa-trash"></span></button>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        {!! Form::text('duration_title', 'រយ:ពេលសាងសង់', array('placeholder' => 'Type','class' => 'form-control','required','readonly')) !!}
                                    </td>
                                    <td>
                                        {!! Form::text('total_duration_build', null, array('placeholder' => 'បញ្ចូលចំនួនថ្ងៃ','class' => 'form-control','required')) !!}
                                    </td>
                                    <td>
                                        {!! Form::textarea('des_duration', null, array('placeholder' => 'ផ្សេងៗ','class' => 'form-control','rows'=>'2')) !!}
                                    </td>
                                    <td><button class="btn btn-sm btn-danger" disabled>
                                        <span class="fa fa-trash"></span></button>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                
                <div id="trp-append">
                    <br/>
                    <h3 class="contract-constructor">Responsiblities for Contractor</h3>
                    <br/>
                    <div class="row contract-constructor">
                        <div style="border: 1px solid gray;padding: 15px;margin:15px;" id="responsibility-table-form">
                            <p style="color: #045FB4 !important;font-size: 90%;margin-top: -25px;background-color: white !important;
                            width: 90px !important;text-align: center;font-family: 'Khmer OS Muol Light'">
                                <a href="javascript:void(0)" class="add-table-responsibility" onclick="add_table_responsibility(this)">ADD NEW</a>
                            </p>
                            <div class="main-add-new">
                                <div class="row" style="padding:15px;" >
                                    <div class="row" style="padding:15px;">
                                        <div class="col-sm-4" style="padding: 0px;" >
                                            <label>Category Responsibilities</label>
                                            {!! Form::select('category_responsibilities',[], null, array('class' => 'form-control select2 select-category_responsibilities first-category-responsibilities','onchange'=>'select_get_work_responsibilities(this)')) !!}
                                        </div>
                                </div>
                                <div class="col-sm-4" style="padding: 0px;" > <label>Title : </label><input class="form-control" name="tt[]" placeholder="Title" id="title-responsibility1" onkeyup="title_press_key(this)" onchange="title_press_key(this)" required>
                                        <br/>
                                    </div>
                                    
                                    <table class="table table-striped table-bordered" width="100%">
                                        <thead>
                                            <tr>
                                                <th>No</th>
                                                <th>
                                                    <label class="required">
                                                        Responsibility
                                                    </label>
                                                </th>
                                                <th>
                                                    <label>
                                                        Description
                                                    </label>
                                                </th>
                                                <th>
                                                    <span class="pull-right button_primary btn-sm plus_ens fa fa-plus" id="btn-add-responsibility" onclick="add_row_responsibility(this)"></span>
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody class="responsibility1">
                                            <tr>
                                                <td>
                                                    <span>1</span>
                                                </td>
                                                <td>
                                                    {!! Form::textarea('responsibility[]', null, array('placeholder' => 'Responsibility','class' => 'form-control','rows'=>'2','required')) !!}
                                                </td>
                                                <td>
                                                    <input type="hidden" class="form-control" name="title_responsibility[]">
                                                    {!! Form::textarea('description_responsibility[]', null, array('placeholder' => 'Description','class' => 'form-control','rows'=>'2')) !!}
                                                </td>
                                                <td><button class="btn btn-sm btn-danger" disabled>
                                                    <span class="fa fa-trash"></span></button>
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div id='tsk-append'>
                    <br/><br/>
                    <h3>Tasks for Contractor</h3>
                    <br/>
                    <div class="row" >
                        <div style="border: 1px solid gray;padding: 15px;margin:15px;" id="task-table-form">
                            <p style="color: #045FB4 !important;font-size: 90%;margin-top: -25px;background-color: white !important;
                            width: 90px !important;text-align: center;font-family: 'Khmer OS Muol Light'">
                                <a href="javascript:void(0)" class="add-table-tasks" onclick="add_table_tasks(this)">ADD NEW</a>
                            </p>
                            <div class="main-add-new">
                                <div class="row" style="padding:15px;">
                                    <div class="row" style="padding:15px;" >
                                        <div class="row" style="padding:15px;">
                                            <div class="col-sm-4" style="padding: 0px;" >
                                                <label>Category Task</label>
                                                {!! Form::select('category_task',[], null, array('class' => 'form-control select2 select-category_task first-category-task','onchange'=>'select_get_work_tasks(this)')) !!}
                                            </div>
                                    </div>
                                    <div class="col-sm-6" style="padding: 0px;" > <label>Title : </label>
                                        <input class="form-control" name="t[]" placeholder="Title" id="title-task1" onkeyup="title_press_key_task(this)" onchange="title_press_key_task(this)" required>
                                            <br/>
                                    </div>
                                    <div class="col-sm-6" > 
                                        <label>Type : </label>
                                        <select class="form-control select2" onchange="type_main_task_change(this)" name="type_main_task" placeholder="Title" id="type_main_task1" required>
                                            <option value = "1">Labour</option>
                                            <option value = "2">Material</option>
                                        </select>
                                        <br/>
                                    </div>
                                    <table class="table table-striped table-bordered" width="100%">
                                        <thead>
                                            <tr>
                                                <th>No</th>
                                                <th>
                                                    <label class="required">
                                                        Task
                                                    </label>
                                                </th>
                                                <th style="width: 150px">
                                                    <label class="required">
                                                        Amount
                                                    </label>
                                                </th>
                                                <th style="width: 150px">
                                                    <label class="required">
                                                        Duration
                                                    </label>
                                                </th>
                                                <th>
                                                    <label>
                                                        Description
                                                    </label>
                                                </th>
                                                <th>
                                                    <span class="pull-right button_primary btn-sm plus_ens fa fa-plus" onclick="add_row_task(this)"></span>
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody class="task1">
                                            <tr>
                                                <td>
                                                    <span>1</span>
                                                </td>
                                                <td>
                                                    {!! Form::textarea('task[]', null, array('placeholder' => 'Task','class' => 'form-control','rows'=>'2','required')) !!}
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control number-only total-amount" name="amount[]" placeholder="Amount" required>
                                                </td>
                                                <td>
                                                    <input type="text" class="form-control number-only" name="duration[]" placeholder="ថ្ងៃ" required>
                                                </td>
                                                <td>
                                                    <input type="hidden" class="form-control" name="title_task[]">
                                                    {!! Form::textarea('description_task[]', null, array('placeholder' => 'Description','class' => 'form-control','rows'=>'2')) !!}
                                                </td>
                                                <td><button class="btn btn-sm btn-danger" disabled>
                                                    <span class="fa fa-trash"></span></button>
                                                    <input type="hidden" class="form-control task_type" name="task_type[]" value = "1">
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12 col-xs-12">
                        <div class="form-group">
                            <button type="submit" style="display: none;" class="button_sm button_primary pull-right" id="btn-submit-save"><span class="fa fa-save"></span> Submit</button>
                            <button type="button" class="button_sm button_primary pull-right" id="btn-save"><span class="fa fa-save"></span> Save</button>
                            <button type="button" onClick="javascript:history.go(-1)" class="button_sm button_warning pull-right">
                                <span class=" fa fa-arrow-circle-left"></span> Back</button>
                        </div>
                    </div>
                </div>
                <br/>

                {!! Form::close() !!}
            </div>
        </div>
    </div>
</div>
@endsection @section('js')
@include('administrator.construction.contractor-task-responsibilities.script')
<script>
    $(document).on('click','#btn-save',function(){
        var main_total_amount = Number($(".main-total-amount").val());
        var amount = 0;
        $(".total-amount").each(function(index,value){
            amount += Number($(this).val());
        });
        amount = Number(amount.toFixed(2));
        if(main_total_amount==amount){
            $("#main-spinner").show();
            let contractor_id = $("select[name=contractor_id]").val();
            let work_group_type_id = $("select[name = work_group_type_id]").val();
            let project_id = $("select[name = project_id]").val();
            let zone_name = $("select[name = zone_name]").val();
            $.ajax({
                type: "POST",
                url: "{{ route('task_responsibilities_contractor.check_existing') }}",
                data: {
                    'contractor_id':contractor_id,
                    'work_group_type_id':work_group_type_id,
                    'project_id':project_id,
                    'zone_name':zone_name,
                    "_token":"{{ csrf_token() }}"
                },
                dataType: "JSON",
                success: function (data) {
                        if(data.status==true) $("#btn-submit-save").click();
                        else{
                            $.confirm({
                                title: 'Confirm!',
                                content: data.message,
                                buttons: {
                                    formSubmit:{
                                        text: 'Submit',
                                        btnClass: 'btn-blue',
                                        action: function () {
                                            $("#btn-submit-save").click();
                                        }
                                    },
                                    cancel: function () {
                                        
                                    },
                                    
                                }
                            });
                        }
                },
                complete:function(data){
                    $("#main-spinner").hide();
                }
            });
        }
        else {
            $.alert("Amount for each task invalid ! (Total main : "+main_total_amount+", Total each task : "+amount+")");
            return false;
        }
    });
    $(document).on('change','select[name=work_group_type_id]',function(){
        var category_repsonsibilities = get_category_responsibilities($(this).val());
        $(".sub-div-app-responsibilities").remove();
        var opt = `<option value=''>Please Select</option>`;
        $.each(category_repsonsibilities,function(index,value){
            if(index>0){
                $("a.add-table-responsibility").click();
            }
            
            opt += `<option value="${value.id}">${value.name}</option>`;
        });
        $("select.select-category_responsibilities").html(opt);

        var category_task = get_category_task($(this).val());
        $(".row-sub-task").remove();
        var opt_task = `<option value=''>Please Select</option>`;
        $.each(category_task,function(index,value){
            if(index>0){
                $("a.add-table-tasks").click();
            }
            opt_task += `<option value="${value.id}">${value.name}</option>`;
        });
        $("select.select-category_task").html(opt_task);
    });

    $(document).on('change','.contractor_id',function(){
        $("#main-spinner").show();
        var route = "{{ route('construction.check_constructor_type',':id') }}";
        route = route.replace(":id",$(this).val());
        var div_work_info = `<br/><br/>
                <h3 class="contract-constructor">Building & Work Info</h3>
                <div class="row contract-constructor" style="padding: 15px;">
                    <table class="table table-striped table-bordered" width="100%">
                        <thead>
                            <tr>
                                <th>
                                    <label class="required">
                                        បរិយាយ
                                    </label>
                                </th>
                                <th>
                                    <label class="required">
                                        ទិន្នន័យ
                                    </label>
                                </th>
                                <th>
                                    <label>
                                        ផ្សេងៗ
                                    </label>
                                </th>
                                <th>
                                    <span class="pull-right button_primary btn-sm plus_ens fa fa-plus" onclick="add_row_buliding_work(this)"></span>
                                </th>
                            </tr>
                        </thead>
                        <tbody class="tb_building_work">
                            <tr>
                                <td>
                                    {!! Form::text('type_of_house_title', 'ប្រភេទផ្ទះ', array('placeholder' => 'Type','class' => 'form-control','required','readonly')) !!}
                                </td>
                                <td>
                                    {!! Form::text('type_of_house', null, array('placeholder' => 'បញ្ចូលប្រភេទផ្ទះ','class' => 'form-control','required')) !!}
                                </td>
                                <td>
                                    {!! Form::textarea('des_type_house', null, array('placeholder' => 'ផ្សេងៗ','class' => 'form-control','rows'=>'2')) !!}
                                </td>
                                <td><button class="btn btn-sm btn-danger" disabled>
                                    <span class="fa fa-trash"></span></button>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    {!! Form::text('duration_title', 'រយ:ពេលសាងសង់', array('placeholder' => 'Type','class' => 'form-control','required','readonly')) !!}
                                </td>
                                <td>
                                    {!! Form::text('total_duration_build', null, array('placeholder' => 'បញ្ចូលចំនួនថ្ងៃ','class' => 'form-control','required')) !!}
                                </td>
                                <td>
                                    {!! Form::textarea('des_duration', null, array('placeholder' => 'ផ្សេងៗ','class' => 'form-control','rows'=>'2')) !!}
                                </td>
                                <td><button class="btn btn-sm btn-danger" disabled>
                                    <span class="fa fa-trash"></span></button>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>`;
        var div_re_info = `<br/>
                <h3 class="contract-constructor">Responsiblities for Contractor</h3>
                <br/>
                <div class="row contract-constructor">
                    <div style="border: 1px solid gray;padding: 15px;margin:15px;" id="responsibility-table-form">
                        <p style="color: #045FB4 !important;font-size: 90%;margin-top: -25px;background-color: white !important;
                        width: 90px !important;text-align: center;font-family: 'Khmer OS Muol Light'">
                            <a href="javascript:void(0)" class="add-table-responsibility" onclick="add_table_responsibility(this)">ADD NEW</a>
                        </p>
                        <div class="main-add-new">
                            <div class="row" style="padding:15px;" >
                                <div class="row" style="padding:15px;">
                                    <div class="col-sm-4" style="padding: 0px;" >
                                        <label>Category Responsibilities</label>
                                        {!! Form::select('category_responsibilities',[], null, array('class' => 'form-control select2 select-category_responsibilities','onchange'=>'select_get_work_responsibilities(this)')) !!}
                                    </div>
                            </div>
                            <div class="col-sm-4" style="padding: 0px;" > <label>Title : </label><input class="form-control" name="tt[]" placeholder="Title" id="title-responsibility1" onkeyup="title_press_key(this)" onchange="title_press_key(this)" required>
                                    <br/>
                                </div>
                                
                                <table class="table table-striped table-bordered" width="100%">
                                    <thead>
                                        <tr>
                                            <th>No</th>
                                            <th>
                                                <label class="required">
                                                    Responsibility
                                                </label>
                                            </th>
                                            <th>
                                                <label>
                                                    Description
                                                </label>
                                            </th>
                                            <th>
                                                <span class="pull-right button_primary btn-sm plus_ens fa fa-plus" onclick="add_row_responsibility(this)"></span>
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody class="responsibility1">
                                        <tr>
                                            <td>
                                                <span>1</span>
                                            </td>
                                            <td>
                                                {!! Form::textarea('responsibility[]', null, array('placeholder' => 'Responsibility','class' => 'form-control','rows'=>'2','required')) !!}
                                            </td>
                                            <td>
                                                <input type="hidden" class="form-control" name="title_responsibility[]">
                                                {!! Form::textarea('description_responsibility[]', null, array('placeholder' => 'Description','class' => 'form-control','rows'=>'2')) !!}
                                            </td>
                                            <td><button class="btn btn-sm btn-danger" disabled>
                                                <span class="fa fa-trash"></span></button>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>`;
        $.ajax({
            url:route,
            type:"GET",
            dataType:"JSON",
            success:function(data){
                $("#trp-append").empty();
                if(data==0){
                    $("#trp-append").append(div_re_info);
                }else if(data==1){
                    $("#trp-append").empty();
                }else{

                }
            },
            complete:function(){
                $("#main-spinner").hide();
            }
        });
    });
</script>
@endsection