@extends('admin.master')

@section('top')
  <div class="top-breadcrumb pull-left">
    <h5>翻译日志</h5>
  </div>
@endsection

@section('ordersearch')
  <div class="order-search mb-10">
    <div class="clearfix">
      <div class="pull-left" id="translate-number" style="line-height: 38px">
        翻译总字符数：<span v-text="setting.total_translate_number" style="color: #ff2020;margin-right: 10px"></span>
        剩余翻译字符：<span v-text="setting.translate_number" style="color: #ff2020;margin-right: 10px"></span>
        消耗翻译字符：<span v-text="remnant_translate_number" style="color: #ff2020"></span>
      </div>
      <div class="pull-right">
        {!! Form::model($where, ['method'=>'get', 'class'=>'form-inline', 'id' => 'form-select']) !!}
        <div class="form-group pr-10">
          {!! Form::select('status', ['' => '选择进度'] + $translateStatus, null, ['id'=>'status', 'class'=>'form-control autosize selectpicker show-tick']) !!}
        </div>
        <button type="submit" class="btn btn-primary"><i class="iconfont icon-sousuo2"></i></button>
        {!! Form::close() !!}
      </div>
    </div>
  </div>
@endsection

@section('content')
  {!! Form::open(['route' => 'admin.translate-log.index', 'method' => 'post', 'class'=>'form-ajax']) !!}
  <table class="table table-hover">
    <thead>
    <tr>
      <th>ID</th>
      <th>消耗字符</th>
      <th>进度</th>
      <th>翻译类型</th>
      <th>提交时间</th>
      <th>完成时间</th>
      <th>异常信息</th>
      <th>提交人</th>
    </tr>
    </thead>
    <tbody>
    @if(!$data->isEmpty())
      @foreach ($data as $value)
        <tr>
          <td>{{ $value->id }}</td>
          <td>{{ $value->contents->sum('translate_number') }}</td>
          <td><span class="translateStatus translateStatus{{ $value->status }}">{{ config('system.translateStatus')[$value->status + 1] }}</span></td>
          <td>{{ $value->type == 1 ? '覆盖' : '跳过' }}</td>
          <td><span class="text-success">{{ $value->created_at }}</span></td>
          <td><span class="text-success">{{ $value->end_at }}</span></td>
          <td>{{ $value->exception }}</td>
          <td>{{ $value->user->username }}</td>
        </tr>
      @endforeach
      <tr>
        <td class="text-left" colspan="9">
          <div class="foot-wrap">
            <div class="pull-left">
              共[ <b class="text-warning">{{ $data->total() }}</b> ]条记录&nbsp;&nbsp;
            </div>
            @if(!empty($data->render()))
              <div class="pull-right">
                <div class="pagination-info">共有{{ $data->total() }}条</div>
                {!! $data->appends($where)->render() !!}
              </div>
            @endif
          </div>
        </td>
      </tr>
    @endif
    </tbody>
  </table>
  @if($data->isEmpty())
    <div class="nodata-wrap">
      <div class="notice-null"><span class="inline-block">暂无数据</span></div>
    </div>
  @endif
  {!! Form::close() !!}

@endsection

@section('notice')
  <!-- Modal -->
  <div class="modal fade" id="dialog" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="true"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">提示</h4>
        </div>
        <div class="modal-body clearfix">
          <div class='col-sm-1 text-center'><i class='iconfont icon-zhuyi'></i></div>
          <div class='col-sm-11 breakall'><p>删除后您将无法恢复，请谨慎操作</p><p class='text-muted'>您确认要删除吗？</p></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary btn-modal-ok">确定</button>
          <button class="btn btn-default btn-modal-cancel btn-modal-close" data-dismiss="modal">取消</button>
        </div>
      </div>
    </div>
  </div>
  <!-- Modal End -->
  <!-- Modal -->
  <div class="modal fade" id="dialog-tip" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="true"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">提示</h4>
        </div>
        <div class="modal-body clearfix">
          <div class='col-sm-1 text-center'><i class='fa fa-times-circle text-size-32 text-warning'></i></div>
          <div class='col-sm-11 breakall'><p class="delete-error">删除失败！</p></div>
        </div>
        <div class="modal-footer">
          <button class="btn btn-primary btn-modal-close" data-dismiss="modal">确定</button>
        </div>
      </div>
    </div>
  </div>
  <!-- Modal End -->
  <!-- Modal -->
  <div class="modal fade" id="dialog-common" tabindex="-1" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="true"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">提示</h4>
        </div>
        <div class="modal-body clearfix">
          <div class='col-sm-1 text-center'><i class='iconfont icon-zhuyi'></i></div>
          <div class='col-sm-11 breakall'><p class="common">请选择要删除的内容!</p></div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary btn-modal-common">确定</button>
        </div>
      </div>
    </div>
  </div>
  <!-- Modal End -->
@endsection

@section('foot')
  <script>
      var apiUrl  = "{{ config('setting.api_url') }}";
      new Vue({
          el: '#translate-number',
          data: {
              setting: {},
              remnant_translate_number: 0
          },
          created: function() {
              this.load();
          },
          methods: {
              load: function() {
                  var _this = this;
                  $.get(apiUrl + '/setting', function(res) {
                      if (res.status == 'success') {
                          _this.setting = res.data;
                          _this.remnant_translate_number = parseInt(res.data.total_translate_number) - parseInt(res.data.translate_number)
                      }
                  });
              },
          }
      })
  </script>
  <script type="text/javascript">
      // 删除
      $('.delete-item').on('click', function(e) {
          var deleteAction = location.pathname;
          var id = $(this).data('id');
          url = deleteAction + '/' + id;

          $('#dialog').modal('show');
          return false;
      });

      //批量删除
      $('.delet-select').on('click', function(){
          var ids = [];
          $('.id:checked').each(function(){
              ids.push(this.value);
          });
          ids = ids.join('-');
          if (!ids) {
              $('.common').text('请选择要删除的内容!');
              $('#dialog-common').modal('show');
              return false;
          }
          url = location.pathname + '/' + ids;

          $('#dialog').modal('show');
          return false;
      });

      $('body').on('click', '.btn-modal-common', function(){
          $(this).parents('#dialog-common').modal('hide');
      });

      $('body').on('click', '.btn-modal-ok', function() {
          $(this).parents('#dialog').modal('hide');
          $.ajax({
              type: "DELETE",
              url: url,
              success: function(data) {
                  if (data.status == 'success') {
                      swal({
                          title: "",
                          text: "信息删除成功！",
                          type: "success",
                          showConfirmButton: false,
                          timer: 1200,
                      });
                      setTimeout("location.reload()", 1200);
                  } else {
                      $('.delete-error').html('删除失败！');
                      $('#dialog-tip').modal('show');
                  }
              }
          });
      });

      $('#perPage').change(function(){
          $('#form-select').submit();
      })
  </script>
@endsection