@extends('admin.master')

@section('top')
	<div class="top-breadcrumb">
		<h5>语言版本管理</h5>
	</div>
@endsection

@section('ordersearch')
<div class="order-search mb-10">
	<div class="clearfix">
	@if(auth()->user()->isMaster())
		<div class="top-btn-group pull-left">
			<a href="{{ url('admin/lang/create') }}" class="btn btn-primary">
				<i class="iconfont icon-plus"></i><span>添加</span>
			</a>
			<a id="manager-lang-id" href="javascript:void(0)" class="manager-lang-id btn btn-outline-primary ml-10">
				<span>批量管理</span>
			</a>
		</div>
		<div class="pull-right">
			<span class="btn btn-primary" id="change-main-lang-btn"><i class="iconfont icon-yuyan1"></i><span>切换主语言</span></span>
		</div>
	@endif
	</div>
</div>
@endsection

@section('content')
{!! Form::open(['route' => 'admin.lang.index', 'method' => 'post', 'class'=>'form-ajax']) !!}
<table class="table table-hover">
	<thead>
		<tr>
			<th width="100" class="text-center">排序</th>
			<th>语言名称</th>
			<th>是否启用</th>
			<th>状态</th>
			@if(auth()->user()->isMaster())
			    <th>是否购买</th>
			@endif
			<th>域名</th>
			<th>创建时间</th>
			@if(auth()->user()->isMaster())
			    <th width="110" class="text-center">操作</th>
			@endif
		</tr>
	</thead>
	<tbody>
	@if(!$langs->isEmpty())
		@foreach ($langs as $value)
			<tr>
				<td align="center">
				    <input name="orders[{{ $value->id }}]" id="orders" value="{{ $value->orders }}" class="form-control text-center text-warning">
				</td>
				<td>
					{{ $value->name }} 
					@if($value->slug == $defaultLang)
						<span class="btn-link pl-10">[主语言版本]</span>
					@endif
				</td>
				<td>
					@if($value->slug == $defaultLang)
						<span class="changegreen" style="cursor: not-allowed;">启用</span>
					@else
						<span id="enabled{{ $value['id'] }}" class="{{ $value['enabled'] == 1 ? 'changegreen' : 'changegray'}}" data-operating="{{ $value['enabled'] == 1 ? 'close' : 'open'}}" onclick="fetch_ajax('enabled', '{{ $value['id'] }}', 'lang');" data-open-text="启用" data-close-text="不启用" data-slug="{{ $value['slug'] }}" style="cursor:pointer;">{{ $value['enabled'] == 1 ? '启用' : '不启用'}}</span>
					@endif
			        
				</td>
				<td>
					@if($value->slug == $defaultLang)
						<span class="changegreen" style="cursor: not-allowed;">发布</span>
					@else
						<span id="flag{{ $value['id'] }}" class="{{ $value['flag'] == 1 ? 'changegreen' : 'changegray'}}" data-operating="{{ $value['flag'] == 1 ? 'close' : 'open'}}" onclick="fetch_ajax('flag', '{{ $value['id'] }}', 'lang');" data-open-text="发布" data-close-text="不发布" style="cursor:pointer;">{{ $value['flag'] == 1 ? '发布' : '不发布'}}</span>
					@endif
			        
				</td>
				@if(auth()->user()->isMaster())
				    <td>
				        @if($value->slug == $defaultLang)
							<span class="changegreen" style="cursor: not-allowed;">已购买</span>
						@else
							<span id="buy{{ $value['id'] }}" class="lang-buy {{ $value['buy'] == 1 ? 'changegreen' : 'changegray'}}" data-operating="{{ $value['buy'] == 1 ? 'close' : 'open'}}" data-id="{{ $value['id'] }}" data-open-text="已购买" data-close-text="未购买" data-slug="{{ $value['slug'] }}" style="cursor:pointer;">{{ $value['buy'] == 1 ? '已购买' : '未购买'}}</span>
						@endif
					</td>
				@endif
				<td><a class="btn-link" href="{{ $value->url }}" target="_blank">{{ $value->url }}</a></td>
				<td>
					<span class="text-success">{{ $value->created_at }}</span>
				</td>
				@if(auth()->user()->isMaster())
					<td class="text-center">
						<div class="toolbar">
							<a href="{{ route('admin.lang.edit', $value->id) }}" class="btn-link">修改</a>
							<span class="text-explode ">|</span>
							<a href="javascript:void(0);" data-id="{{ $value->id }}" class="btn-link delete-item">删除</a>
						</div>
					</td>
				@endif
			</tr>
		@endforeach
		<tr>
			<td class="text-left" colspan="8">
				<div class="foot-wrap">
					<div class="pull-left pl-20">
						<input name="btn_order" type="button" value="更新排序" onclick="update_orders('lang');" class="button button-order all-order btn-primary">
					</div>
				</div>
			</td>
		</tr>
	@endif
	</tbody>
</table>
@if($langs->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 -->
<!-- Modal -->
<div class="modal fade" id="manager-lang" tabindex="-1" role="managerLang">
	<div class="modal-dialog">
		<div class="modal-content" style="width: 860px;">
			<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="pt-10 pb-10">
					@if(!$langs->isEmpty())
						<div class="edition-cont clearfix">
							@foreach ($langs as $val)
								@if($val->slug != $defaultLang)
									<div class="pretty p-default p-smooth mt-10 mb-10 col-xs-2" style="margin-right: 0;">
										<input name="manager-lang[]" class="platform-id" type="checkbox" value="{{ $val['slug'] }}" @if($val->buy == 1) checked @endif onclick="checkItem(this, 'checkManagerLangAll')">
										<div class="state p-success ml-5"><label style="text-indent: 10px;">{{ $val->name }}</label></div>
									</div>
								@endif
							@endforeach
						</div>
					@endif
				</div>
			</div>
			<div class="modal-footer clearfix">
				<div class="pull-left">
					<div class="pretty-box clearfix">
						<div class="pretty p-default p-smooth" >
							<input name="checkManagerLangAll" id="checkManagerLangAll" type="checkbox" @if($langs->where('buy', 1)->count() == $langs->count()) checked="checked" @endif onclick="checkAll(this, 'manager-lang[]')">
							<div class="state p-success ml-10"><label>全选</label></div>
						</div>
					</div>
				</div>
				<div class="pull-right">
					<button type="button" class="btn btn-primary btn-manager-lang-ok">确定</button>
					<button class="btn btn-default btn-modal-cancel btn-modal-close" data-dismiss="modal">取消</button>
				</div>
			</div>
		</div>
	</div>
</div>
<!-- Modal End -->
<!-- Modal -->
<div class="modal fade" id="change-main-lang-modal" tabindex="-1">
	<div class="modal-dialog">
		<div class="modal-content" style="width: 860px;">
			<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>
			<?php $buyLangArr = $langs->where('buy', 1)  ?>
			<div class="modal-body clearfix">
				<form class="change-main-lang-form pt-10 pb-10" action="{{url('admin/lang/prime')}}" method="post">
					<input type="hidden" name="lang" value="1">
					{{ csrf_field() }}
					@if(!$buyLangArr->isEmpty())
						<div class="edition-cont clearfix">
							@foreach ($buyLangArr as $val)
								@if($val->slug != $defaultLang)
									<div class="pretty p-default p-round p-smooth mt-10 mb-10 col-xs-2" style="margin-right: 0;">
										<input name="slug" class="platform-id" type="radio" value="{{ $val['slug'] }}">
										<div class="state p-success ml-5"><label style="text-indent: 10px;">{{ $val->name }}</label></div>
									</div>
								@endif
							@endforeach
						</div>
					@endif
				</form>
			</div>
			<div class="modal-footer clearfix">
				<div class="pull-left">
					<div class="pretty-box clearfix">
						<div class="pretty p-default p-smooth" >
							<input name="changeLangTips" id="changeLangTips" type="checkbox">
							<div class="state p-success ml-10"><label>切换主语言后部分数据不会被更新，请知晓后操作。</label></div>
						</div>
					</div>
				</div>
				<div class="pull-right">
					<button type="button" class="btn btn-primary" id="confirm-change-main-lang" disabled>确定</button>
					<button class="btn btn-default btn-modal-cancel btn-modal-close" data-dismiss="modal">取消</button>
				</div>
			</div>
		</div>
	</div>
</div>
<!-- Modal End -->
@endsection
@section('foot')
@if(auth()->user()->isMaster())
<script>
// 删除
$('.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(){	
	url = location.pathname + '/' + $(this).data('id');
	$('#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 == 'sucess') {
				swal({
					title: "", 
					text: "信息删除成功！",
					type: "success",
					showConfirmButton: false,
					timer: 1200,
				});
				setTimeout("location.reload()", 1200);
			} else {
				$('.delete-error').html('删除失败！');
				$('#dialog-tip').modal('show');
			}
		}
	});
});
	
$('body').on('click', '.lang-buy', function() {
	var deleteAction = location.pathname;
	var id = $(this).data('id');
	var requesturl = deleteAction + '/buy/' + id;
	$.get(requesturl, function(data) {
		if (data.status == 'success') {
			var operating = $('#buy' + id).attr('data-operating');
			var opt         = operating == 'open' ? 'close' : 'open';
			var removeClass = operating == 'open' ? 'changegray' : 'changegreen';
			var addClass    = operating == 'open' ? 'changegreen' : 'changegray';
			var text = $("#buy"+id).data(operating + '-text');
			$("#buy"+id).removeClass(removeClass).addClass(addClass).text(text);
			$("#buy"+id).attr('data-operating', opt); //重新赋值为 open
			if (data.buy == 1) {
				// swal({
				// 	title: "", 
				// 	text: "购买成功！",
				// 	type: "success",
				// 	confirmButtonText: '确定',
				// 	timer: 2000,
				// });
				swal({ 
					title: "数据填充中...", 
					text: "请不要关闭当前窗口！",
					imageUrl: "../data/assets/images/loading6.gif",
					showConfirmButton: false,
				});
				var lang = $("#buy"+id).data('slug');
				$.get(HOME_URL + '/admin/lang/add/' + lang, function(res) {
					if (res.status == 'success') {
						swal({ 
							title: "数据填充完成", 
							text: "2秒后自动关闭！",
							timer: 2000,
							type: "success",
							showConfirmButton: false
						});
						setTimeout("location.reload()", 2000);
					}
				});
			}
		}
	});
});

$('.manager-lang-id').on('click', function(e) {
	$('#manager-lang').modal('show');
	return false;
});

$('body').on('click', '.btn-manager-lang-ok', function() {
	$(this).parents('#manager-lang').modal('hide');
	var slugs = [];
	$('.platform-id:checked').each(function(){
		slugs.push(this.value);
	});
	swal({
		title: "批量操作站点中...",
		text: "请不要关闭当前窗口！",
		imageUrl: "../data/assets/images/loading6.gif",
		showConfirmButton: false
	});
	$.post(HOME_URL + '/admin/lang/manager-all', { slugs: slugs }, function(res) {
		if (res.status == 'success') {
			swal({
				title: "数据填充完成",
				text: "2秒后自动关闭！",
				timer: 2000,
				type: "success",
				showConfirmButton: false
			});
			setTimeout("location.reload()", 2000);
		}
	});
});

// 切换主语言弹窗
$('#change-main-lang-btn').click(function () {
	$('#change-main-lang-modal').modal(true)
})
$("#changeLangTips").change(function () {
	if ($(this)[0].checked) {
		$("#confirm-change-main-lang").removeAttr('disabled')
	} else {
		$("#confirm-change-main-lang").attr('disabled', true)
	}
})
$("#confirm-change-main-lang").click(function (e) {
	e.preventDefault()
	if (!$(".change-main-lang-form").find('input[name="slug"]:checked').val()) {
		alert('请选择主语言版本！');
		return
	}
	var lang = $('input[name="slug"]:checked').val()
	$(".change-main-lang-form").submit()
	$('#change-main-lang-modal').modal(false)
	window.top.topVue.changeLang(lang)
})
</script>				
@endif

@endsection