@extends('vip.layouts')
@section('content')
<form class="layui-form" action="/Admin/MoveData/MovedataView" id="submitForm" name="submitForm" method="post" eventType=eventAjaxForm>
{!! csrf_field() !!}

  <div class="layui-form-item">
    迁移数据
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">原站用户自增ID</label>
    <div class="layui-input-block">
      <input type="text" name="id"   id="id"  required  lay-verify="" placeholder="" autocomplete="off" value="all" class="layui-input">
    </div>
  </div>
  
   <div class="layui-form-item">
    <label class="layui-form-label">{{ trans('sixlottery.rpassword') }}:</label>
    <div class="layui-input-block">
      <input type="password" name="rpassword" class="layui-input" lay-verify="username" id="form_rpassword" placeholder="请输入二次密码">
    </div>
  </div>

  <input type="hidden" name="page" id="page" value="1">
  <input type="hidden" name="limit" id="limit" value="10">
  <input type="hidden" name="allnum" value="@isset($data){{ $data['unum'] }}@endisset">

  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn updateres" lay-submit lay-filter="submitForm">开始迁移</button>
    </div>
  </div>
</form>
<br>
<div id="parper" class="hide" style="display: none;text-align: center;line-height: 150px;">共<span class="allnum">@isset($data){{ $data['unum'] }}@endisset</span>条数据待迁移<p></p>已迁移<span class="percent">0</span>条</div>
<script>
var index = 0;
var index1 = 0;
$(".updateres").mouseup(function(){
	  index = layer.load(1, {shade: [0.71,'#eee']});
    index1 = layer.open({
          type: 1,
          title: '迁移进度',
          closeBtn: 0,
          area: ['300px','300px'],
          skin: 'layui-layer-demo', //没有背景色
          shadeClose: true,
          content: $('#parper')
    });
});

layui.use(['form'], function(){
    var form = layui.form;

    form.verify({
        password:function(value,item){
            if(value==null||value==''||value==undefined){
                $('.updateres').show();
                return '密码不能为空';
            }
  	   }
    });

    //监听提交
    form.on('submit(submitForm)', function(data){
        return true;
    });
});

function cbFormSuccess(data){
    if(data.status==1 && data.data.curnum < data.data.allnum){
        $('.percent').text(data.data.curnum);
        $('.allnum').text(data.data.allnum);
        layer.alert('正在迁移，请等待');
        gomove();
    }else{
        layer.close(index1);
        layer.close(index);
        layer.alert(data.msg);
        setTimeout(function(){
          window.location.href = '/admin/MoveData/MovedataView';
        },2500);
    }
}
 
function gomove(){
  var url = '/admin/MoveData/MovedataView';

  var page = $('#page').val();
  var percent = $('.percent').text();
  $('#page').val(parseInt(page)+1);
	$.ajax({
		url:url,
		type:"post",  
        dataType:"json",  
        data:$('form').serialize(),  
        success:function(data){
            layer.close(index);
            var curnum = parseInt(percent)+data.data.curnum;
            if(data.status==1 && curnum < data.data.allnum){
                $('.percent').text(curnum);
                setTimeout(function(){
                  gomove();
                },5000);
      			}else if(curnum >= data.data.allnum){
                $('.percent').text(curnum);
                layer.close(index1);
                layer.close(index);
                layer.alert('成功',function(){
                  window.location.href='/admin/MoveData/MovedataView';
                });
            }else if(data.status<0){
                layer.close(index1);
                layer.close(index);
                layer.alert(data.msg,function(){
                  window.location.reload();
                });
      			}else{
                $('.percent').text(curnum);
                setTimeout(function(){
                  gomove();
                },5000);
            }
        },  
	})
}

function  cbFormFail(data) {
  if(data.status==2){
    layer.alert(data.msg);
    setTimeout(function(){
      window.location.href = '/admin/MoveData/MovedataView';
    },2500);
  }else{
    parent.layer.msg(data.msg); 
    setTimeout(function(){
        window.location.reload();
      },2500);
  }  
}

</script>
@endsection
