<div class="row">
  <div class="col-md-8">
    <br/>
    <div class="card">
      <div class="card-header">
         <label>Dining Name:</label>
         <%= @table.name %>
      </div>
      <div class="card-block">
        <table class="table table-striped">
          <thead>
          <tr>
            <th>Select</th>
            <th>Table Name</th>
            <th>Commissioner Ids</th>
            <th>In time</th>
            <th>Out time</th>
            <th>Action</th>
          </tr>
          </thead>

          <tbody>
          <% @duties_in.each do |in_duty| %>
              <tr class="induty_tr" data-ref="<%= in_duty.id %>">
                <td>
                  <input type="radio" style="width:20px;" name="checkbox" class="checkbox_check"></td>
                <td><%= in_duty.dining_facility.name rescue '-' %></td>
                <td><%= in_duty.commissioner.name rescue '-' %></td>
                <td><%= in_duty.in_time.strftime("%Y-%m-%d/%I:%M %p") rescue '-' %></td>
                <td><%= in_duty.out_time.strftime("%Y-%m-%d/%I:%M %p") rescue '-' %></td>
                <td>
                  <button class="delete btn btn-danger btn-sm waves-effect" data-ref="../table/<%=in_duty.dining_facility.id%>/<%= @booking_id %>/destroy_in_duty/<%=in_duty.id%>" data-method="delete">
                Remove
              </button>

              <span class="hidden" id="delete_text">
                  <h6>Are you sure you want to remove this row ?</h6>
                  <h6>This action can't be undo. </h6> 
              </span>
                </td>
              </tr>
          <% end %>
          </tbody>
        </table>
      </div>
    <%= paginate @duties_in %>
    </div>
  </div>
  
  <div class="col-md-3 partial">
    <%= render 'form', in_duty: @in_duty, table: @table %>
  </div>

  <div class="col-md-1">
  <br>
    <button type="button" class="btn bg-default btn-block" onclick="window.location.href = '/origami/<%=@table.type.downcase%>/<%=@table.id%>'"><i class="material-icons m-t--5">reply</i> Back </button>
    <button class="form-control btn btn-info btn-block" id="reset">New</button>
  </div>
</div>

<script type="text/javascript">
    $(document).on('click', ".induty_tr", function () {

        $(this).closest('tr').find('.checkbox_check').prop("checked", true);

        var in_duty_id = $(this).attr('data-ref');
        // var table_id = "<%= @table.id %>";
        // var sale_id = "<%= @sale_id %>";
        var url = "/origami/assign_in_duty/" + in_duty_id + "/edit";

        $.ajax({
            type: "GET",
            url: url,
            data: {},

            success: function (data) {
                // debugger;
                // $('.partial').empty().append(data);
                var inT = new Date(data.in_duty.in_time);
                var in_time = ((inT.getDate() >= 10 ? inT.getDate() : '0'+inT.getDate()) +"-"+ (inT.getMonth() >= 10 ? inT.getMonth() : '0'+inT.getMonth()) +"-"+ inT.getFullYear() +" - "+ (inT.getHours()>10? inT.getHours() : '0'+inT.getHours()) +":"+ (inT.getMinutes() >= 10? inT.getMinutes() : '0'+inT.getMinutes()));
                var outT = new Date(data.in_duty.out_time);
                var out_time = ((outT.getDate() >= 10? outT.getDate() : '0'+outT.getDate()) +"-"+ (outT.getMonth() >= 10 ? outT.getMonth() : '0'+outT.getMonth()) +"-"+ outT.getFullYear() +" - "+ (outT.getHours()>10? outT.getHours() : '0'+outT.getHours()) +":"+ (outT.getMinutes() >= 10? outT.getMinutes() : '0'+outT.getMinutes()));

                $('#in_duty_id').val(data.in_duty.id);
                $('#in_duty_commissioner_ids').val(data.commissioner.id);
                $('#in_duty_in_time').val(in_time);
                $('#in_duty_out_time').val(out_time);

                // $('#update').removeAttr('disabled').val('');
                // $('#update').attr('value', 'Update');
                // $('#create').attr('disabled', 'disabled');

                $("#new_in_duty").attr('class', 'simple_form edit_in_duty');
                var id = "edit_in_duty_" + data.in_duty.id;
                $("#new_in_duty").attr('id', data.in_duty.id);

                $(".edit_in_duty").attr('id', data.in_duty.id);
                $(".edit_in_duty").attr('action', '/origami/edit_in_duty/' + $('#in_duty_id').val());
                $(".edit_in_duty").attr('action', '/origami/edit_in_duty/' + $('#in_duty_id').val());
                $(".patch_method").html('<input type="hidden" name="_method" value="patch">');

//                setInterval(function () {
//                    $('.partial').load('/controller_name/action_name');
//                }, 3000);
            }
        });
    });

    // $('#back').on('click', function () {
    //     window.location.href = '/origami/table/' + "<%= @table.id %>";
    // });
   $('#reset').click(function () {
       location.reload();
        /*var url = "<%= origami_index_in_duty_path %>";
        console.log(url);
        $.ajax({
            type: "GET",
            url: url,
            success: function (data) {
                $('.partial').empty().append(data);
                $('.induty_tr > td > .checkbox_check').prop("checked", false);
            }
        });*/
    });

</script>
