@extends('Public.index')
@section('admin')
<!-- 新增 -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
                <form class="form-horizontal">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-3 control-label">分类名称</label>
                            <div class="col-sm-12">
                            <input type="text" class="form-control goodsname" id="inputEmail3" placeholder="分类名称">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputPassword3" class="col-sm-3 control-label">排序</label>
                            <div class="col-sm-12">
                            <input type="number" class="form-control goodsorders" id="inputPassword3" placeholder="排序" value="1">
                            </div>
                        </div>

                </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary" onclick="create()">添加</button>
      </div>
    </div>
  </div>
</div>
<!-- 修改 -->
<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModal2Label">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body">
                <form class="form-horizontal">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-3 control-label">分类名称</label>
                            <div class="col-sm-12">
                            <input type="hidden" class="id2" value="">
                            <input type="text" class="form-control goodsname2" id="inputEmail3" placeholder="分类名称">
                            </div>
                        </div>

                </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
        <button type="button" class="btn btn-primary" onclick="update()">修改</button>
      </div>
    </div>
  </div>
</div>
<!-- 莫泰结束 -->

<div class="content-wrapper" style="min-height: 810px;">
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h4>积分商城分类</h4>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="/admin/index">首页</a></li>
              <li class="breadcrumb-item active">积分管理</li>
              <li class="breadcrumb-item active">积分商城</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>
    <div class="col-xs-3">
        <button class="btn btn-primary" data-toggle="modal" data-target="#myModal" style="margin-left:17px;margin-bottom:15px">添加分类</button>
        <button class="btn btn-success look" style="margin-left:17px;margin-bottom:15px">查看商品</button>
    </div>
    
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-md-12">

            <div class="card">
              <!-- /.card-header -->
              <div class="card-body p-0">
                <table class="table table-condensed">
                  <tbody>

                  <tr style="text-align:center">
                    <th>商城分类</th>
                    <th>排序</th>
                    <th>状态</th>
                    <th style="width: 100px">操作</th>
                  </tr>
                @foreach($cate as $key => $val)
                  <tr style="text-align:center">
                    <input type="hidden" value="{{ $val -> id }}">
                    <td><a href="/admin/jifen/goods_list/{{ $val -> id }}">{{ $val -> cate_name }}</a></td>
                    <td onclick="orders({{ $val -> id }},{{ $val -> orders }})">{{ $val -> orders }}</td>
                    <td>
                        @if($val -> status == 1)
                        <a href="javascript:void(0)" onclick="status({{ $val -> id }})"><span class="badge bg-success">上架中</span></a>
                        @else
                        <a href="javascript:void(0)" onclick="status({{ $val -> id }})"><span class="badge bg-danger">未上架</span></a>
                        @endif
                    </td>
                    <td>
                        <a href="javascript:void(0)" onclick="edit(this)"><i class="fa fa-edit" style="font-size: 16px;" title="编辑"></i></a>
                        <a href="javascript:void(0)" onclick="del({{ $val -> id }})"><i class="fa fa-trash" style="font-size: 16px;" title="删除"></i></a>
                    </td>
                  </tr>
                @endforeach
                </tbody></table>
              </div>
            </div>
          </div>
        </div>
    </section>
  </div>
<script src="/admin/plugins/jquery/jquery.min.js"></script>
<script src="/admin/layer-v3.1.1/layer/layer.js"></script>
<script>
    function status(id)
    {
        if(!id){
            layer.msg('参数错误');
            return false;
        }
        layer.confirm('确认操作吗？该分类下所有商品都将同步改变状态', {
            btn: ['确认','取消'] //按钮
            }, function(){
                $.get('/admin/jifen/cate_status',{id:id},function(res){
                    if(res.code == '1000'){
                        layer.msg(res.msg);
                        window.location.reload();
                    }else{
                        layer.msg(res.msg);
                    }
                })
        });   
    }
    function orders(id,orders)
    {
        layer.prompt({ value:orders,title:'输入排序序号', formType: 3}, function(text, index){
            if(!id){
                layer.msg('参数错误');
                return false;
            }
            $.get('/admin/jifen/cate_orders',{id:id,text:text},function(res){
                if(res.code == '1000'){
                    layer.msg(res.msg);
                    window.location.reload();
                }else{
                    layer.msg(res.msg);
                }
            })
            layer.close(index);
        });
    }
    function create()
    {
        var goodsName = $('.goodsname').val();
        var goodsOrders = $('.goodsorders').val();
        if(!goodsName || !goodsOrders) {
            layer.msg('参数不全');
            return false;
        }
        $.get('/admin/jifen/cate_create',{goodsName:goodsName,goodsOrders:goodsOrders},function(res){
            if(res.code == '1000'){
                layer.msg(res.msg);
                window.location.reload();
            }else{
                layer.msg(res.msg);
            }
        })
    }
    function del(id)
    {
        if(!id) {
            layer.msg('参数不全');
            return false;
        }
        layer.confirm('确认删除分类吗? 不会删除该分类下面的商品', {
            btn: ['确认','取消'] //按钮
            }, function(){
                $.get('/admin/jifen/cate_delete',{id:id},function(res){
                    if(res.code == '1000'){
                        layer.msg(res.msg);
                        window.location.reload();
                    }else{
                        layer.msg(res.msg);
                    }
                })
        });
    }
    function edit(obj)
    {
        var cate_name = $(obj).parent().prev().prev().prev().html();
        var id = $(obj).parent().prev().prev().prev().prev().val()
        $('.id2').val(id);
        $('.goodsname2').val(cate_name);
        $('#myModal2').modal();
    }
    function update()
    {
        var name = $('.goodsname2').val();
        var id = $('.id2').val();
        if(!name || !id) {
            layer.msg('参数不全');
            return false;
        }
        $.get('/admin/jifen/cate_update',{id:id,name:name},function(res){
            if(res.code == '1000'){
                layer.msg(res.msg);
                window.location.reload();
            }else{
                layer.msg(res.msg);
            }
        }) 
    }
    $('.look').click(function(){
        window.location.href="/admin/jifen/goods_list";
    })
</script>
@endsection
@section('title','积分商城分类')
@section('jifen','menu-open')
@section('jifen2','active')
@section('jifen-shop','active')