@extends('adminlte::page') @section('content')
<?php Fun::lang() ?>
<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="#">Set Up Information</a></li>
            <li><a href="#">Work Task</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 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-working-construction.store_task_contractor','method'=>'POST', 'enctype'=>'multipart/form-data','id'=>'form-create-task')) !!}
                <div class="row">
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Group Work</label>
                        {!! Form::select('group_work', $group_work_types, null, ['class' => 'form-control select2','id'=>'group_work-id','required']) !!}
                    </div>
                    <div class="form-group col-sm-4 col-xs-12">
                        <label class="required">Category Task </label>
                        {!! Form::select('category_task', $sub_category_tasks,null, ['class' => 'form-control select2','required']) !!}
                    </div>
                </div>
                <div id="bwi-append">
                    <div class="row contract-constructor" style="padding: 15px;">
                        <table class="table table-striped table-bordered" width="100%">
                            <thead>
                                <tr>
                                    <th>
                                        <label class="required">
                                            Name
                                        </label>
                                    </th>
                                    <th>
                                        <label class="required">
                                            Amount
                                        </label>
                                    </th>
                                    <th>
                                        <label>
                                            Description
                                        </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('name[]', null, array('placeholder' => 'Name','class' => 'form-control','required')) !!}
                                    </td>
                                    <td>
                                        {!! Form::text('amount[]', null, array('placeholder' => 'Amount','class' => 'form-control number-only','required')) !!}
                                    </td>
                                    <td>
                                        {!! Form::textarea('description[]', 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 class="row">
                    <div class="col-sm-12 col-xs-12">
                        <div class="form-group gr-btn">
                            <button type="submit" id="btn-save-task-submit" class="button_sm button_primary pull-right" style="display: none;"></button>
                            <button type="button" id="btn-save-task" class="button_sm button_primary pull-right"><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')
<script>
    $(document).on("click","#btn-save-task",function(e)
    {
        let b = false;
        var row = $(".tb_building_work").find("tr");
        $(row).each(function(index,value){
            /*let row_code = $(this).find("td:eq(0)").find("input").val();*/
            let row_name = $(this).find("td:eq(0)").find("input").val();
            let count_ch = 0;
            $(row).each(function(num,val){
                if(index!=num){
                    /*if(row_code==$(this).find("td:eq(0)").find("input").val()){
                        b = true;
                        $.alert("Code must be unique !");
                        return false;
                    }*/
                    if(row_name == $(this).find("td:eq(0)").find("input").val()){
                        b = true;
                        $.alert("Name must be unique !");
                        return false;
                    }
                }
            });
            if(b == true){
                return false;
            }
        });
        if(b == true){
            $(".gr-btn").html(`
                <button type="submit" id="btn-save-task-submit" class="button_sm button_primary pull-right" style="display: none;"></button>
                <button type="button" id="btn-save-task" class="button_sm button_primary pull-right"><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>`);
            return false;
        }
        else {
            $('#btn-save-task-submit').click();
        }
    });
    $(document).on("change","#group_work-id",function(){
        var $_value = $(this).val();
        $("#main-spinner").show();
        var route = "{{ route('task-working-construction.get_sub_category_task',':id') }}";
        route = route.replace(":id",$_value);
        $.ajax({
            url:route,
            type:"GET",
            dataType:"JSON",
            success:function(data){
                var opt = `<option value="">Please Select</option>`;
                $.each(data,function(index,value){
                    opt += `<option = "${value.id}">${value.name}</option>`;
                });
                $('select[name=sub_category_task]').html(opt);
            },
            complete:function(){
                $("#main-spinner").hide();
            }
        });
    });
    function add_row_buliding_work(obj)
    {
        var tb_building_work = `
                            <tr>
                                <td>
                                    {!! Form::text('name[]', null, array('placeholder' => 'Name','class' => 'form-control','required')) !!}
                                </td>
                                <td>
                                    {!! Form::text('amount[]', null, array('placeholder' => 'Amount','class' => 'form-control number-only','required')) !!}
                                </td>
                                <td>
                                    {!! Form::textarea('description[]', null, array('placeholder' => 'Description','class' => 'form-control','rows'=>'2')) !!}
                                </td>
                                <td><button type="button" class="btn btn-sm btn-danger" onclick="remove(this)">
                                    <span class="fa fa-trash"></span></button>
                                </td>
                            </tr>
        `;
        $(obj).closest('table').find('tbody').append(tb_building_work);
        
    }
    function remove(obj)
    {
        var index = 1;
        var tr_class =  $(obj).closest("table").find('tbody').attr('class');
        $(obj).closest('tr').remove();
        $("tr.tr-table-"+tr_class).each(function(){
            $(this).find('td:eq(0)').html(`<span>${++index}</span>`);
        });
    }
</script>
@endsection