{extend name="default/base/base_seller" /}
{block name="seller_main"}
<div class="dssc-form-default">
    {empty name="wholesale_info"}
    <form id="add_form" action="{:url('Sellerpromotionwholesale/wholesale_save')}" method="post">
    {else /}
    <form id="add_form" action="{:url('Sellerpromotionwholesale/wholesale_edit_save')}" method="post">
            <input type="hidden" name="wholesale_id" value="{$wholesale_info.wholesale_id}">
    {/empty}

            {empty name="wholesale_info"}
            <dl>
                <dt><i class="required">*</i>{$Think.lang.start_time}{$Think.lang.ds_colon}</dt>
                <dd>
                    <input id="start_time" name="start_time" type="text" class="text w130"/>
                    <em class="add-on"><i class="iconfont">&#xe8d6;</i></em><span></span>
                    <p class="hint">
                        {notpresent name="isPlatformStore"}{if $current_wholesale_quota.wholesalequota_starttime > 1}
                        {$Think.lang.wholesale_add_start_time_explain}{$current_wholesale_quota.wholesalequota_starttime|date="Y-m-d H:i:s"}
                        {/if}{/notpresent}
                    </p>
                </dd>
            </dl>
            <dl>
                <dt><i class="required">*</i>{$Think.lang.end_time}{$Think.lang.ds_colon}</dt>
                <dd>
                    <input id="end_time" name="end_time" type="text" class="text w130"/>
                    <em class="add-on"><i class="iconfont">&#xe8d6;</i></em><span></span>
                    <p class="hint">
                        {notpresent name="isPlatformStore"}
                        {$Think.lang.wholesale_add_end_time_explain}{$current_wholesale_quota.wholesalequota_endtime|date="Y-m-d H:i:s"}
                        {/notpresent}
                    </p>
                </dd>
            </dl>
            
            <dl>
                <dt><i class="required">*</i>{$Think.lang.wholesale_name}{$Think.lang.ds_colon}</dt>
                <dd>
                    <a href="javascript:void(0);" id="btn_show_search_goods" class="dssc-btn dssc-btn-acidblue">{$Think.lang.modified_discount_price}</a>
                    <input id="goods_commonid" name="goods_commonid" type="hidden" value=""/>
                    <span></span>
                    <div id="div_search_goods" class="div-goods-select mt10" style="display: none;">
                        <table class="search-form">
                            <tr>
                                <th class="w150">
                                    <strong>{$Think.lang.search_store_items}</strong>
                                </th>
                                <td class="w160">
                                    <input id="search_goods_name" type="text w150" class="text" name="goods_name" value=""/>
                                </td>
                                <td class="w70 tc">
                                    <a href="javascript:void(0);" id="btn_search_goods" class="dssc-btn"/><i class="iconfont">&#xe718;</i>{$Think.lang.ds_search}</a></td>
                                <td class="w10"></td>
                                <td>
                                    <p class="hint">{$Think.lang.special_goods_not_allowed}</p>
                                </td>
                            </tr>
                        </table>
                        <div id="div_goods_search_result" class="search-result" style="width:739px;"></div>
                        <a id="btn_hide_search_goods" class="close" href="javascript:void(0);">X</a>
                    </div>
                    <p class="hint">{$Think.lang.wholesale_goods_explain}</br><span class="red">{$Think.lang.implement_uniform_purchase_prices}</span></p>
                </dd>
            </dl>
            {/empty}
            <dl>
                <dt><i class="required">*</i>{$Think.lang.wholesale_if_mix}{$Think.lang.ds_colon}</dt>
                <dd>
                    <ul class="dssc-form-radio-list">
                    <li><label onclick="$('*[dstype=if_mix_0]').hide();$('*[dstype=if_mix_1]').show()"><input type="radio" name="wholesale_if_mix" {if !isset($wholesale_info) || $wholesale_info['wholesale_if_mix'] == '1'}checked="checked"{/if} value="1" />{$Think.lang.ds_yes}</label></li>
                    <li><label onclick="$('*[dstype=if_mix_1]').hide();$('*[dstype=if_mix_0]').show()"><input type="radio" name="wholesale_if_mix" {if isset($wholesale_info) && $wholesale_info['wholesale_if_mix'] == '0'}checked="checked"{/if} value="0" />{$Think.lang.ds_no}</label></li>
                    </ul>
                    <p class="hint">{$Think.lang.wholesale_if_mix_notice}</p>
                </dd>
            </dl>
            <dl>
                <dt><i class="required">*</i>{$Think.lang.wholesale_set}{$Think.lang.ds_colon}</dt>
                <dd>
                    <div id="wholesale_goods_item"></div>
                    
                </dd>
            </dl>
            <div class="bottom">
                <input type="hidden" name="wholesale_json" id="wholesale_json">
                <input id="submit_button" type="submit" class="submit" value="{$Think.lang.ds_submit}">
            </div>
    </form>
</div>
<link rel="stylesheet" href="{$Think.PLUGINS_SITE_ROOT}/js/jquery-ui-timepicker/jquery-ui-timepicker-addon.min.css">
<script src="{$Think.PLUGINS_SITE_ROOT}/js/jquery-ui-timepicker/jquery-ui-timepicker-addon.min.js"></script>
<script src="{$Think.PLUGINS_SITE_ROOT}/js/jquery-ui-timepicker/i18n/jquery-ui-timepicker-zh-CN.js"></script>
<script src="{$Think.PLUGINS_SITE_ROOT}/template.min.js"></script>
<script id="wholesale_goods_template" type="text/html">
    <table class="layui-table"> 
        <colgroup> 
            <col width="80" /> 
            <col width="300" /> 
            <col width="200" /> 
            <col width="100" /> 
            <col dstype="if_mix_0" <%if(wholesale_if_mix==1){%>style="display:none"<%}%> width="400" /> 
            <col dstype="if_mix_1" <%if(wholesale_if_mix==0){%>style="display:none"<%}%> width="400" /> 
        </colgroup> 
        <thead> 
            <tr> 
                <th>{$Think.lang.wholesale_enable}</th> 
                <th>{$Think.lang.wholesale_goods_info}</th> 
                <th>{$Think.lang.wholesale_goods_price}</th> 
                <th>{$Think.lang.wholesale_goods_storage}</th> 
                <th>{$Think.lang.wholesale_price}</th> 
            </tr> 
        </thead> 
        <tbody> 
            <%for(var i in goods_list){%>
            <tr <%if(goods_list[i].goods_lock || goods_list[i].inactive){%>class="inactive"<%}%>> 
                <td><input type="checkbox" dstype="enable" value="<%=goods_list[i].goods_id%>" <%if(goods_list[i].goods_lock){%>disabled<%}else{%><%if(!goods_list[i].inactive){%>checked<%}%> onchange="$(this).parent().parent().toggleClass('inactive')"<%}%>></td>
                <td> 
                    <div class="ds-table-title"> 
                        <div class="ds-title-pic"> 
                            <img src="<%=goods_list[i].goods_image%>" /> 
                        </div> 
                        <div class="ds-title-content"> 
                            <a href="javascript:;"><%=goods_list[i].goods_name%></a> 
                        </div> 
                    </div> </td> 
                <td> ￥<%=goods_list[i].goods_price%> </td> 
                <td> <%=goods_list[i].goods_storage%> </td> 
                <td dstype="if_mix_0" <%if(wholesale_if_mix==1){%>style="display:none"<%}%>> 
                    <div class="ds-table-btn"> 
                        <table class="layui-table sku-block" sku_id="<%=goods_list[i].goods_id%>"> 
                            <colgroup> 
                                <col width="10%" /> 
                                <col width="28%" /> 
                                <col width="28%" /> 
                                <col width="27%" /> 
                                <col width="7%" /> 
                            </colgroup> 
                            <thead> 
                            </thead>
                            <tbody> 
                                <tr> 
                                    <td style="min-width:40px;">{$Think.lang.wholesale_price_num}</td> 
                                    <%if(!goods_list[i].wholesale_info){%>
                                    <td><input type="number" class="layui-input wholesale-inut-len num" /></td> 
                                    <td><input type="number" class="layui-input wholesale-inut-len num" /></td> 
                                    <td><input type="number" class="layui-input wholesale-inut-len num" /></td>
                                    <%}else{%>
                                    <%for(var j in goods_list[i].wholesale_info){%>
                                    <td><input type="number" class="layui-input wholesale-inut-len num" value="<%=goods_list[i].wholesale_info[j].num%>" /></td> 
                                    <%}%>
                                    <%}%>
                                    <td rowspan="3"><a href="javascript:void(0);" class="iconfont" style="font-size:20px" onclick="addPricemodel(this);">&#xe6e8;</a></td> 
                                </tr> 
                                <tr> 
                                    <td style="min-width:40px;">{$Think.lang.wholesale_price_price}</td> 
                                    <%if(!goods_list[i].wholesale_info){%>
                                    <td><input type="number" class="layui-input wholesale-inut-len price" /></td> 
                                    <td><input type="number" class="layui-input wholesale-inut-len price" /></td> 
                                    <td><input type="number" class="layui-input wholesale-inut-len price" /></td> 
                                    <%}else{%>
                                    <%for(var j in goods_list[i].wholesale_info){%>
                                    <td><input type="number" class="layui-input wholesale-inut-len price" value="<%=goods_list[i].wholesale_info[j].price%>" /></td> 
                                    <%}%>
                                    <%}%>
                                </tr> 
                                <tr> 
                                    <td style="min-width:40px;">{$Think.lang.ds_handle}</td> 
                                    <%if(!goods_list[i].wholesale_info){%>
                                    <td><a href="javascript:;" class="layui-btn" onclick="deletePricemodel(this)">{$Think.lang.ds_del}</a></td> 
                                    <td><a href="javascript:;" class="layui-btn" onclick="deletePricemodel(this)">{$Think.lang.ds_del}</a></td> 
                                    <td><a href="javascript:;" class="layui-btn" onclick="deletePricemodel(this)">{$Think.lang.ds_del}</a></td> 
                                    <%}else{%>
                                    <%for(var j in goods_list[i].wholesale_info){%>
                                    <td><a href="javascript:;" class="layui-btn" onclick="deletePricemodel(this)">{$Think.lang.ds_del}</a></td> 
                                    <%}%>
                                    <%}%>
                                </tr> 
                            </tbody> 
                        </table> 
                    </div> 
                </td>
                <%if(i==0){%>
                <td rowspan="<%=goods_list.length%>" dstype="if_mix_1" <%if(wholesale_if_mix==0){%>style="display:none"<%}%>> 
                    <div class="ds-table-btn"> 
                        <table class="layui-table sku-block" sku_id="0"> 
                            <colgroup> 
                                <col width="10%" /> 
                                <col width="28%" /> 
                                <col width="28%" /> 
                                <col width="27%" /> 
                                <col width="7%" /> 
                            </colgroup> 
                            <thead> 
                            </thead>
                            <tbody> 
                                <tr> 
                                    <td style="min-width:40px;">{$Think.lang.wholesale_price_num}</td> 
                                    <%if(!goods_list[0].wholesale_info){%>
                                    <td><input type="number" class="layui-input wholesale-inut-len num" /></td> 
                                    <td><input type="number" class="layui-input wholesale-inut-len num" /></td> 
                                    <td><input type="number" class="layui-input wholesale-inut-len num" /></td>
                                    <%}else{%>
                                    <%for(var j in goods_list[0].wholesale_info){%>
                                    <td><input type="number" class="layui-input wholesale-inut-len num" value="<%=goods_list[0].wholesale_info[j].num%>" /></td> 
                                    <%}%>
                                    <%}%>
                                    <td rowspan="3"><a href="javascript:void(0);" class="iconfont" style="font-size:20px" onclick="addPricemodel(this);">&#xe6e8;</a></td> 
                                </tr> 
                                <tr> 
                                    <td style="min-width:40px;">{$Think.lang.wholesale_price_price}</td> 
                                    <%if(!goods_list[0].wholesale_info){%>
                                    <td><input type="number" class="layui-input wholesale-inut-len price" /></td> 
                                    <td><input type="number" class="layui-input wholesale-inut-len price" /></td> 
                                    <td><input type="number" class="layui-input wholesale-inut-len price" /></td> 
                                    <%}else{%>
                                    <%for(var j in goods_list[0].wholesale_info){%>
                                    <td><input type="number" class="layui-input wholesale-inut-len price" value="<%=goods_list[0].wholesale_info[j].price%>" /></td> 
                                    <%}%>
                                    <%}%>
                                </tr> 
                                <tr> 
                                    <td style="min-width:40px;">操作</td> 
                                    <%if(!goods_list[0].wholesale_info){%>
                                    <td><a href="javascript:;" class="layui-btn" onclick="deletePricemodel(this)">{$Think.lang.ds_del}</a></td> 
                                    <td><a href="javascript:;" class="layui-btn" onclick="deletePricemodel(this)">{$Think.lang.ds_del}</a></td> 
                                    <td><a href="javascript:;" class="layui-btn" onclick="deletePricemodel(this)">{$Think.lang.ds_del}</a></td> 
                                    <%}else{%>
                                    <%for(var j in goods_list[0].wholesale_info){%>
                                    <td><a href="javascript:;" class="layui-btn" onclick="deletePricemodel(this)">{$Think.lang.ds_del}</a></td> 
                                    <%}%>
                                    <%}%>
                                </tr> 
                            </tbody> 
                        </table> 
                    </div> 
                </td> 
                <%}%>
            </tr> 
            <%}%>
        </tbody> 
    </table>
</script>
<script>
    $(function () {
            /*{empty name = "wholesale_info"}*/
            $('#start_time').datetimepicker({dateFormat: 'yy-mm-dd'});
            $('#end_time').datetimepicker({dateFormat: 'yy-mm-dd'});
            /*{else}*/
            var data={}
            data.wholesale_if_mix={$wholesale_info.wholesale_if_mix};
            data.goods_list={$wholesalegoods_list|raw};
            var wholesale_goods_item = template.render('wholesale_goods_template', data);
            $('#wholesale_goods_item').html(wholesale_goods_item);
            /*{/empty}*/

            $('#btn_show_search_goods').on('click', function() {
                $('#div_search_goods').show();
            });

            $('#btn_hide_search_goods').on('click', function() {
                $('#div_search_goods').hide();
            });

            //搜索商品
            $('#btn_search_goods').on('click', function() {
                var url = "{:url('Sellerpromotionwholesale/goods_select')}";
                 var  datas  =  $.param({goods_name: $('#search_goods_name').val()});
                $('#div_goods_search_result').load(url,datas);
            });

            $('#div_goods_search_result').on('click', '.pagination li a', function() {
                $('#div_goods_search_result').load($(this).attr('href'));
                return false;
            });

            //选择商品
            $('#div_goods_search_result').on('click', '[dstype="btn_add_wholesale_goods"]', function() {
                var goods_commonid = $(this).attr('data-goods-commonid');
                $.get("{:url('Sellerpromotionwholesale/goods_info')}", {goods_commonid: goods_commonid}, function(data) {
                    if(data.result) {
                        $('#goods_commonid').val(goods_commonid);
                        data.wholesale_if_mix=$('input[name=wholesale_if_mix]:checked').val()
                        var wholesale_goods_item = template.render('wholesale_goods_template', data);
                        $('#wholesale_goods_item').html(wholesale_goods_item);
                        $('#div_search_goods').hide();
                    } else {
                        layer.msg(data.message);
                    }
                }, 'json');
            });
            jQuery.validator.methods.greaterThanDate = function (value, element, param) {
                var date1 = new Date(Date.parse(param.replace(/-/g, "/")));
                var date2 = new Date(Date.parse(value.replace(/-/g, "/")));
                return date1 < date2;
            };
            jQuery.validator.methods.lessThanDate = function (value, element, param) {
                var date1 = new Date(Date.parse(param.replace(/-/g, "/")));
                var date2 = new Date(Date.parse(value.replace(/-/g, "/")));
                return date1 > date2;
            };
            jQuery.validator.methods.greaterThanStartDate = function (value, element) {
                var start_date = $("#start_time").val();
                var date1 = new Date(Date.parse(start_date.replace(/-/g, "/")));
                var date2 = new Date(Date.parse(value.replace(/-/g, "/")));
                return date1 < date2;
            };
            //页面输入内容验证
            $("#add_form").validate({
                errorPlacement: function (error, element) {
                    var error_td = element.parent('dd').children('span');
                    error_td.append(error);
                },
                onfocusout: false,
                submitHandler: function (form) {
                    var item_list;
                    var price_array = [];
                    var wholesale_if_mix=$('input[name=wholesale_if_mix]:checked').val()
                    if(wholesale_if_mix==0){
                        item_list = $(".sku-block[sku_id!=0]");
                        item_list.each(function() {
                            if (!$(this).parents('tr').find('input[dstype=enable]').is(':checked')) {
                                return
                            }
                            var str=getWholesale($(this))
                            var sku_id = $(this).attr('sku_id');
                            price_array.push(sku_id+'|'+str.join('_'));
                        });
                    }else{
                        var str=getWholesale($(".sku-block[sku_id=0]"))
                        $("input[dstype=enable]:checked").each(function() {
                            var sku_id = $(this).val();
                            price_array.push(sku_id+'|'+str.join('_'));
                        })
                    }

                    var price_json = price_array.join(',');
                    $('#wholesale_json').val(price_json)
                    ds_ajaxpost('add_form', 'url', "{:url('Sellerpromotionwholesale/index')}", 1000);
                },
                rules: {
                    start_time: {
                        required: true,
                        greaterThanDate: "{:date('Y-m-d H:i',@$current_wholesale_quota['wholesalequota_starttime'])}"
                    },
                    end_time: {
                        required: true,
                        /*{if !$isPlatformStore}*/
                        lessThanDate : "{present name='current_wholesale_quota.wholesalequota_starttime'}{$current_wholesale_quota.wholesalequota_endtime|date='Y-m-d H:i'}{/present}",
                        /*{/if}*/
                        greaterThanStartDate : true
                    },
                },
                messages : {
                    start_time : {
                        required : '<i class="iconfont">&#xe64c;</i>' +
                        '<?php echo sprintf(lang("wholesale_add_start_time_explain"),@date("Y-m-d H:i",$current_wholesale_quota["wholesalequota_starttime"]));?>',
                        greaterThanDate : '<i class="iconfont">&#xe64c;</i>' +
                        '<?php echo sprintf(lang("wholesale_add_start_time_explain"),@date("Y-m-d H:i",$current_wholesale_quota["wholesalequota_starttime"]));?>'
                    },
                    end_time : {
                        required : '<i class="iconfont">&#xe64c;</i>' +
                        '<?php echo sprintf(lang("wholesale_add_end_time_explain"),@date("Y-m-d H:i",$current_wholesale_quota["wholesalequota_endtime"]));?>',
                        /*{if !$isPlatformStore}*/
                        lessThanDate : '<i class="iconfont">&#xe64c;</i>' +
                        '<?php echo sprintf(lang("wholesale_add_end_time_explain"),@date("Y-m-d H:i",$current_wholesale_quota["wholesalequota_endtime"]));?>',
                        /*{/if}*/
                        greaterThanStartDate : '<i class="iconfont">&#xe64c;</i>{$Think.lang.greater_than_start_time}'
                    },
                }
            });
    });
    /**
     * 获取阶梯价
     */
function getWholesale(obj) {
    var item_price_list = obj.find('tr');
    


    if (item_price_list.length == 0) {
        layer.msg('{$Think.lang.wholesale_price_empty}', {
            icon: 5,
            anim: 6
        });
        return;
    }

    var first_item = obj.find('tr:eq(0)');
    var second_item = obj.find('tr:eq(1)');
    var num_item_list = first_item.find('.num');
    if (num_item_list.length == 0) {
        layer.msg('{$Think.lang.wholesale_price_empty}', {
            icon: 5,
            anim: 6
        });
        return;
    }
    var str = [] 
    num_item_list.each(function(index, element) {
        var index = $(element).parent().index();
        var num = $(element).val();
        var price = second_item.find('td:eq(' + index + ')').find('.price').val();
        str.push(num + '-' + price)
    });
    return str
}
    /**
     * 添加阶梯价
     */
function addPricemodel(obj) {
    var index = $(obj).parent().index();
    if ($(obj).parent().parent().find('td').length == 5) {
        
        return false;
    }

    $(obj).parent().before('<td><input type="number" class="layui-input wholesale-inut-len num" lay-verify="required|gtzero" /></td>');
    var index = $(obj).parent().index();
    $(obj).parent().parent().next().append('<td><input type="number" class="layui-input wholesale-inut-len price" lay-verify="required|gtzero|price"/></td>');
    $(obj).parent().parent().next().next().append('<td><a href="javascript:;" class="layui-btn" onclick="deletePricemodel(this)">{$Think.lang.ds_del}</a></td>');
}

    /**
     * 删除阶梯价
     * @param e
     */
function deletePricemodel(e) {
    var index = $(e).parent().index();
    if ($(e).parent().parent().find('td').length == 2) {
        layer.msg('{$Think.lang.wholesale_price_less}', {
            icon: 5,
            anim: 6
        });
        return false;
    }
    $(e).parent().parent().parent().find('tr').each(function() {
        console.log($(this).find('td'));
        $(this).find('td:eq(' + index + ')').remove();
    });

}
</script>
<style>
.inactive .ds-title-pic{opacity: .5}    
.inactive a,.inactive {color:#999}
.inactive .sku-block{display: none}
.inactive .sku-block[sku_id='0']{display: block}
.layui-table {
    width: 100%;
    background-color: #fff;
    color: #666;
}
.layui-table thead tr{
    background-color: #f2f2f2;
}
.layui-table th {
    font-weight: bold;
    background-color: #F7F7F7;
    text-align: left;
}
.layui-table td, .layui-table th {
    position: relative;
    padding: 9px 15px;
    min-height: 20px;
    line-height: 20px;
    font-size: 14px;
    border-width: 1px;
    border-style: solid;
    border-color: #e6e6e6;
}
.sku-block td:nth-child(5){display: none}
.wholesale-inut-len {
    width: 60px !important;
}
.layui-input{
    height: 34px;
    line-height: 34px;
    border-color: #E6E6E6;
    display: block;
    width: 100%;
    padding-left: 10px;
    border-width: 1px;
    border-style: solid;
    background-color: #fff;
    border-radius: 2px;
}


.ds-table-title {
	display: flex;
	align-items: center;
}

.ds-table-title .ds-title-pic {
	flex-shrink: 0;
	display: inline-block;
	width: 50px;
	height: 50px;
	text-align: center;
	line-height: 50px;
	margin-left: 5px;
}

.ds-table-title .ds-title-pic img {
	max-width: 100%;
	max-height: 100%;
}

.ds-table-title .ds-title-content {
	overflow: hidden;
	margin-left: 10px;
	flex: 1;
	line-height: 1.8;
}
</style>
{/block}

