
$(document).ready(function(){
    $('#loadingContainer').ajaxStart(function(){$(this).show();$('#loadingbackground').show().css({"opacity": "0.5"});}).ajaxStop(function(){$(this).hide();});
    $('#ddlTopAreas').change(function(){
        $('#ddlSubAreas').remove();
        $('#ddlThirdAreas').remove();
        $('#btnGetList').val('显示门店');
        var suboption = $('#ddlTopAreas option[@selected]');
        if(suboption.attr('child') == 'true'){
            var subareas = GetSubArea(suboption.val());//获取二级地区
            var subareaoptions = $('<select id="ddlSubAreas" name="ddlSubAreas"></select>').append(subareas);
            if(subareaoptions.find('option').length > 1){
                $('#ddlTopAreas').after(subareaoptions);
                $('#ddlSubAreas').change(function(){
                    $('#ddlThirdAreas').remove();
                    $('#btnGetList').val('显示门店');
                    var thirdoption = $('option[@selected]',this);
                    if(thirdoption.attr('child') == 'true'){
                        var thirdareas = GetSubArea(thirdoption.val());//获取三级地区
                        var thirdareaoptions = $('<select id="ddlThirdAreas" name="ddlThirdAreas"></select>').append(thirdareas);
                        if(thirdareaoptions.find('option').length > 1){
                            $(this).after(thirdareaoptions);
                            $('#ddlThirdAreas').change(function(){
                                $('#btnGetList').val('显示门店');
                                $('#btnGetList').click();
                            });
                        };
                    };
                    $('#btnGetList').click();
                });//end change
            };//end if
        };//end if
        $('#btnGetList').click();
    });//end ddlTopAreas change
    
    $('#btnGetList').click(function(){
        var areaID = GetLastAreaID();
        if(areaID == '0'){alert('请先选择地区');return false;};
        $('#AreaID').val(areaID);
        GetList(areaID);
    });
    
    $('#btnShow').click(function(){
        var areaID = GetLastAreaID();
        if(areaID == '0'){alert('请先选择地区');return false;};
        $('#btnSubmit').data('action','Add');
        $('#popupContact').popup({width:'700',height:'400',popupHead:'popupHead',btnClose:'popupContactClose'});
        $('#txtName').val('');
        $('#txtNo').val('');
        $('#txtAddr').val('');
        $('#ftbRemark').val('');        
		ftbRemark_editor.focus();
		ftbRemark_editor.document.open();
		ftbRemark_editor.document.write("");
		ftbRemark_editor.document.close();
    });
    
    $('#btnSubmit').click(function(){
        var areaID = GetLastAreaID();
        if(areaID == '0'){alert('请先选择地区');return false;};
        $('#AreaID').val(areaID);
        
        var parentAreaIDs = '';
        parentAreaIDs = $('#ddlTopAreas').val();
        if($('#ddlSubAreas')[0] && $('#ddlSubAreas').val() != '0'){
            parentAreaIDs += ',' + $('#ddlSubAreas').val();
        };
        if($('#ddlThirdAreas')[0] && $('#ddlThirdAreas').val() != '0'){
            parentAreaIDs += ',' + $('#ddlThirdAreas').val()
        };
        $('#parentAreaIDs').val(parentAreaIDs);

        var datas = $('#txtName').serialize() + '&' + $('#txtNo').serialize() + '&' + $('#txtAddr').serialize() + '&' + $('#ftbRemark').serialize() + '&' + $('#AreaID').serialize() + '&' + $('#parentAreaIDs').serialize();
        $.ajax({
            url:'ztd.aspx?NoCopyRight=1&type=ajax&Action=' + $('#btnSubmit').data('action') + '&ZtdID=' + $('#btnSubmit').data('ZtdID'),
            type:'post',
            data:datas,
            cache:false,
            success:function(data){
                if(!checkLoginStatus(data)){return false;};
                switch(data){
                    case '1':
                        alert('操作成功');
                        $('#popupContactClose').click();
                        $('#btnGetList').val('刷新门店列表').click();
                    break;
                    case '2':
                        alert('门店编号最多50个字符');
                    break;
                    case '3':
                        alert('门店名称最多50个字符');
                    break;
                    case '4':
                        alert('门店地址最多200个字符');
                    break;
                    case '5':
                        alert('门店备注最多200个字符');
                    break;
                }
            },
            error:function(a,b){alert(b);}
        });//end ajax
    });//end click
});

function GetSubArea(areaID){
    var options = null
    $.ajax({
        url:'ztd.aspx?NoCopyRight=1&type=ajax&Action=GetSubArea&AreaID='+areaID,
        type:'get',
        async:false,
        success:function(data){
            if(!checkLoginStatus(data)){return false;};
            options = data;
        },
        error:function(){alert('error');}
    });
    return options;
};

function GetList(areaID){
    $.ajax({
        url:'ztd.aspx?NoCopyRight=1&type=ajax&Action=GetList&AreaID='+areaID,
        type:'get',
        dataType:'json',
        cache:false,
        success:function(msg){
            $('#template').nextAll().remove();
            var data = msg.Ztd;
            $.each(data,function(i,n){
                var row = $('#template').clone().show();
                row.find('.Name').html(n.ZtdName);
                row.find('.No').html(n.ZtdNo);
                row.find('.Addr').html(n.Address);
                row.find('.Modify').html('<span class="hand">修改</span>').find('span').click(function(){
                    $('#popupContact').popup({width:'700',height:'400',popupHead:'popupHead',btnClose:'popupContactClose'});

                    $('#btnSubmit').data('action','Update').data('ZtdID',n.ZtdID);
                    $('#txtName').val(n.ZtdName);
                    $('#txtNo').val(n.ZtdNo);
                    $('#txtAddr').val(n.Address);
                    $('#ftbRemark').val(n.Remark);                    
					ftbRemark_editor.focus();
					ftbRemark_editor.document.open();
					ftbRemark_editor.document.write("");
					ftbRemark_editor.document.close();
					var sel = ftbRemark_editor.document.selection.createRange();
					sel.pasteHTML(n.Remark);
                });
                row.find('.Delete').html('<span class="hand">删除</span>').find('span').click(function(){
                    if(confirm('确定要删除门店吗?') && confirm('请再次确定是否删除?')){
						$.ajax({
                            url:'ztd.aspx?NoCopyRight=1&type=ajax&Action=Delete&ZtdID='+n.ZtdID,
                            type:'post',
                            data:$('#parentAreaIDs').serialize(),
                            cache:false,
                            success:function(data){
                                if(!checkLoginStatus(data)){return false;};
								if(data > 0){row.remove()};
                            },
                            error:function(){alert('error')}
                        });
                    }
                });
                $('#dataContainer').append(row);
            });
        },
        error:function(XMLHttpRequest,textStatus){if(textStatus=='parsererror'){alert('身份已过期,请重新登陆!')}else{alert(textStatus)};}
    });
};

function GetLastAreaID(){
    var areaID = '0';
    var parentAreaIDs = '';
    if($('#ddlThirdAreas').length != 0 && $('#ddlThirdAreas').val() != '0'){
        areaID = $('#ddlThirdAreas').val();
    }else if($('#ddlSubAreas').length != 0 && $('#ddlSubAreas').val() != '0'){
        areaID = $('#ddlSubAreas').val();
    }else{
        areaID = $('#ddlTopAreas').val();
    };
    return areaID;
};

function checkLoginStatus(data){
    var result = true;
    if(data.indexOf('<!DOCTYPE') != -1){
        result = false;
        alert('身份已过期,请重新登陆!');
    };
    return result;
};