@extends('admin.master')

@section('top')
    <div class="top-breadcrumb pull-left">
		<h5>发布分享</h5>
	</div>
@endsection


@section('content')
<div class="share-notice mb-30">
	<i class="iconfont icon-gantanhao"></i>
	<span>请确认一天只发一条分享信息，以保证不被社交平台当成恶意发布被封号风险！</span>
</div>
<div class="tab-content clearfix form-horizontal">
	<div class="form-group">
		<label class="col-sm-2 control-label label-width">平台：</label>
		<div class="col-sm-5">
            <select id="video-cate" class="form-control autosize selectpicker inline-block" name="platform" v-model="platform" @change="choose()">
                <option value="">请选择</option>
				@if(!$platforms->isEmpty())
					@foreach ($platforms as $value)
					<option value="{{ $value }}">{{ $value }}</option>
					@endforeach
				@endif
            </select>
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-2 control-label label-width">内容：</label>
		<div class="col-sm-5">
			<textarea id="content-area" class="form-control" rows="5" name="message" v-model="message" cols="50" placeholder="请输入内容"></textarea>
		</div>
	</div>
    <div class="form-group" v-show="isShow">
		<label class="col-sm-2 control-label label-width">网址：</label>
		<div class="col-sm-5">
			<input class="form-control len" name="link" v-model="link" type="text" placeholder="请输入网址">
		</div>
	</div>
	<div class="form-group">
		<label class="col-sm-2 control-label label-width"></label>
		<div class="col-sm-5">
			<button type="submit" class="btn btn-primary" @click="send()">发布分享</button>
		</div>
	</div>
</div>
@endsection

@section('foot')
<script type="text/javascript">
    var facebookUrl   = "{{ config('setting.facebook_share') }}";
	var twitter       = "{{ config('setting.twitter_share') }}";
    var access_token_facebook  = "{{ $access_token_facebook }}";
	var access_token_twitter   = "{{ $access_token_twitter }}";
	var refresh_token = "{{ $refresh_token }}";
    var shares = new Vue({
        el: '#main-view',
        data: {
            message: '',
			platform: '',
            isShow: false,
            link: '',
        },
        methods: {
            choose: function() {
                if (this.platform == "facebook") {
                    this.isShow = true;
                } else {
                    this.isShow = false;
                }
            },
            send: function() {
                var _this = this;
                var url   = _this.platform == "facebook" ? facebookUrl : twitter;
                if (_this.message == '') {
                    swal({
                        title: "", 
                        text: "内容不能为空！",
                        type: "error",
                        confirmButtonText: '确定',
                    });
                    
                    return false;
                }
				var shareData = {"message": _this.message, "platform": _this.platform};
				if (_this.platform == 'facebook') {
					shareData.link         =  _this.link;
					shareData.access_token = access_token_facebook;
				} else {
					shareData.access_token_secret = refresh_token;
					shareData.access_token        = access_token_twitter;
				}
                $.ajax({
                    type: "POST",
                    url: url,
                    data: shareData,
                    success: function(data) {
						if (data == 'success') {
							$.ajax({
								type: "POST",
								url: "{{ url('admin/social-share') }}",
								data: {"message": _this.message, "platform": _this.platform, "link": _this.link},
								success: function(data) {

								},
							});
							_this.message = '';
                            _this.link    = '';
							swal({
								title: "", 
								text: "分享成功！",
								type: "success",
								confirmButtonText: '确定',
								timer: 1500,
							});
						} else {
							swal({
								title: "", 
								text: "分享失败，请重新分享！",
								type: "error",
								confirmButtonText: '确定',
								timer: 1500,
							});
						}
                    }
                });
            },
        }
    });
</script>
@endsection