<?php
use \common\libs\MyLib;
?>
<?php $this->beginBlock('header_css'); ?>
<link href="/assets/css/plugins/clockpicker/clockpicker.css" rel="stylesheet">
<?php $this->endBlock(); ?>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="ibox float-e-margins">
        <div class="ibox-title fixed">
            <h5>营销策略 - 编辑 - <?=$info->celuo_name?></h5>
            <div class="ibox-tools">
                <a class="btn btn-primary btn-xs btn-fanxian" href="javascript:void(0);" onclick="fanxian()">
                    <i class="fa fa-code-fork"></i> 返现
                </a>
                <a class="btn btn-primary btn-xs btn-save" href="javascript:void(0);">
                    <i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-primary btn-xs edit-btn" href="javascript:void(0);" onclick="history.go(-1)">
                    <i class="fa fa-backward"></i> 返回
                </a>
            </div>
        </div>
        <div class="ibox-content">
            <div class="row">
                <div class="col-sm-12">
                    <form role="form" class="form-horizontal data-from" id="theFrm">
                        <input type="hidden" name="op" value="edit" />
                        <input type="hidden" name="id" value="<?=$info->id?>" />
                        <div class="form-group">
                            <label class="col-sm-1 control-label">名称</label>
                            <div class="col-sm-3">
                                <input type="text" name="name" value="<?=$info->celuo_name?>" class="form-control">
                            </div>
                            <label class="col-sm-1 control-label">开始</label>
                            <div class="col-sm-3">
                                <div class="input-group">
                                    <span class="input-group-addon">日期</span>
                                    <input type="text" class="input-date form-control" id="begin_date" name="begin_date" autocomplete="off" value="<?=$info->begin_date?>" />
                                </div>
                            </div>
                            <label class="col-sm-1 control-label">结束</label>
                            <div class="col-sm-3">
                                <div class="input-group">
                                    <span class="input-group-addon">日期</span>
                                    <input type="text" class="input-date form-control" id="end_date" name="end_date" autocomplete="off" value="<?=$info->end_date?>" />
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-1 control-label">备注</label>
                            <div class="col-sm-11">
                                <textarea class="form-control" name="remark" id="remark"><?=$info->remark?></textarea>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div id="fanxians"></div>
    <div class="ibox">
        <div class="ibox-title">
            <label>员工范围</label>
            <div class="ibox-tools">
                <a class="btn btn-primary btn-xs btn-users" data-id="0" href="javascript:void(0);" onclick="users()">
                    <i class="fa fa-users"></i> 添加员工
                </a>
            </div>
        </div>
        <div class="ibox-content">
            <div class="row">
                <div class="col-sm-12" id="users">
                </div>
            </div>
        </div>
    </div>
</div>

<?php $this->beginBlock('footer_js'); ?>
<script src="/assets/js/plugins/layer/laydate/laydate.js"></script>
<script type="text/javascript" language="javascript">
    function fanxian() {
        title = '返现条件';
        layer_show(title, '/marketing-strategy/fanxian?celuo_id=<?=$info->id?>');
    }
    function readFanXians() {
        $('#fanxians').html('');
        $.get('fanxian-read?celuo_id=<?=$info->id?>',function(obj){
            if(obj.success) {
                obj.fanxians.forEach(function(item, index) {
                    var html = '<div class="ibox">';
                    html += '<div class="ibox-title">';
                    html += '<label>客户返现：' + item.fanxian_rate + '%，最高返现：' + item.max_cashback + '元</label>';
                    html += '<div class="ibox-tools">';
                    html += '<a class="btn btn-primary btn-xs btn-users" data-id="0" href="javascript:void(0);" onclick="gifts(' + item.id + ',1)">';
                    html += '<i class="fa fa-users"></i> 添加礼品';
                    html += '</a>';
                    html += '<a class="btn btn-primary btn-xs btn-users" data-id="0" href="javascript:void(0);" onclick="gifts(' + item.id + ',2)">';
                    html += '<i class="fa fa-users"></i> 添加礼券';
                    html += '</a>';
                    html += '<button class="btn btn-danger btn-xs" style="margin-left: 8px;" onclick="deleteFanxian(' + item.id + ')">';
                    html += '<i class="fa fa-trash"></i> 删除返现';
                    html += '</button>';
                    html += '</div>';
                    html += '</div>';
                    html += '<div class="ibox-content">';
                    html += '<div class="row">';
                    html += '<div class="col-sm-12">';
                    html += '<h5>条件：</h5>';
                    html += '<p>保费：' + item.tiaojian.baofei + '元</p>';
                    if(item.tiaojian.car_type != '')
                        html += '<p>车牌类型：' + item.tiaojian.car_type + '</p>';
                    if(item.tiaojian.car_use != '')
                        html += '<p>使用性质：' + item.tiaojian.car_use + '</p>';
                    if(item.tiaojian.company_name != '')
                        html += '<p>保费公司：' + item.tiaojian.company_name + '</p>';
                    if(item.tiaojian.type_name != '')
                        html += '<p>数据类型：' + item.tiaojian.type_name + '</p>';
                    html += '<hr>';
                    html += '<h5>礼品：</h5>';
                    item.gifts.forEach(function(gift_item, sub_index) {
                        var gift_html = '<p><button class="btn btn-danger btn-xs" onclick="deleteGift(' + gift_item.id + ')">删除</button>' + gift_item.free_type + '：';
                        gift_item.gifts.forEach(function(sub_gift, thr_index) {
                            gift_html += '<button type="button" class="btn btn-primary btn-xs btn-rounded">' + sub_gift.name + '</button>';
                        });
                        html += gift_html;
                    });
                    html += '<hr>';
                    html += '<h5>礼券</h5>';
                    item.quans.forEach(function(gift_item, sub_index) {
                        var gift_html = '<p><button class="btn btn-danger btn-xs" onclick="deleteGift(' + gift_item.id + ')">删除</button>' + gift_item.free_type + '：';
                        gift_item.gifts.forEach(function(sub_gift, thr_index) {
                            gift_html += '<button type="button" class="btn btn-primary btn-xs btn-rounded">' + sub_gift.name + '</button>';
                        });
                        html += gift_html;
                    });
                    html += '</div>';
                    html += '</div>';
                    html += '</div>';
                    html += '</div>';
                    $('#fanxians').html($('#fanxians').html() + html);
                });
            }
        },'json');
    }
    function deleteFanxian(fanxian_id) {
        layer.confirm('是否确认删除返现？', {
            btn: ['确认','取消'], //按钮
            shade: false //不显示遮罩
        }, function(){
            $.post('/marketing-strategy/fanxian-delete',{celuo_id:<?=$info->id?>, fanxin_id: fanxian_id},function(obj){
                layer.msg(obj.msg);
                if(obj.success) {
                    readFanXians();
                }
            },'json');
        }, function(){
            //
        });
    }
    function gifts(fanxian_id, type) {
        title = '添加礼品';
        if(type == 2) title = '添加礼券';
        layer_show(title, '/marketing-strategy/gifts?celuo_id=<?=$info->id?>&fanxian_id=' + fanxian_id + '&type=' + type);
    }
    function deleteGift(id) {
        layer.confirm('是否确认删除礼品/礼券？', {
            btn: ['确认','取消'], //按钮
            shade: false //不显示遮罩
        }, function(){
            $.post('/marketing-strategy/gift-delete',{celuo_id:<?=$info->id?>, id: id},function(obj){
                layer.msg(obj.msg);
                if(obj.success) {
                    readFanXians();
                }
            },'json');
        }, function(){
            //
        });
    }
    function users() {
        title = '员工管理';
        layer_show(title, '/marketing-strategy/users?id=<?=$info->id?>');
    }
    function readUsers() {
        $('#users').html('');
        $.get('users-read?id=<?=$info->id?>',function(obj){
            if(obj.success) {
                obj.users.forEach(function(item, index) {
                    var html = '<button class="btn btn-info btn-rounded btn-sm" style="margin-left: 8px; margin-bottom: 8px;" onclick="deleteUser(' + item.id + ')">';
                    html += item.name;
                    html += '</button>';
                    $('#users').html($('#users').html() + html);
                });
            }
        },'json');
    }
    function deleteUser(user_id) {
        layer.confirm('是否确认删除员工？', {
            btn: ['确认','取消'], //按钮
            shade: false //不显示遮罩
        }, function(){
            $.post('/marketing-strategy/users-delete',{id:<?=$info->id?>, user_id: user_id},function(obj){
                layer.msg(obj.msg);
                if(obj.success) {
                    readUsers();
                }
            },'json');
        }, function(){
            //
        });
    }
    $(function () {
        laydate({
            elem:'#begin_date',
            format: 'YYYY-MM-DD hh:mm:ss',
            istime: true
        });
        laydate({
            elem:'#end_date',
            format: 'YYYY-MM-DD hh:mm:ss',
            istime: true
        });
        readFanXians();
        readUsers();
        //提交按钮
        $(".btn-save").click(function(){
            layer.confirm('是否确认提交？', {
                btn: ['确认','取消'], //按钮
                shade: false //不显示遮罩
            }, function(){
                var params = $("#theFrm").serialize();
                $.post('/marketing-strategy/save',params,function(obj){
                    if(obj.success) {
                        layer.msg('保存成功');
                        location.href = '/marketing-strategy/index';
                    } else {
                        layer.msg(data.msg);
                    }
                },'json');
            }, function(){
                //
            });
        });
    })
</script>
<?php $this->endBlock('footer_js'); ?>
