/**
 * [OElove] (C)2010-2099 oelove.com Webapp
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2020/04/01 by CL $
*/
$(function () {

    function OEMyMarry() {
        this.api_url = WAP_APPFILE +'&c=mymarry';
        this._this_dialog = null;
        this.slide_time = 300;
        
        this.id = '';
        this.obj_id = '';
        this.clickAdd();
        this.clickEdit();
        this.clickPay();
        this.clickDel();
        
    }
    OEMyMarry.prototype = {
        
        clickAdd:function() {
            var _this = this;
            
            //选择时间
            $(document).on("click", "[f='but_sel_validtime']", function(){
                var $days = $(this).attr("data-days");
                var $fee = $(this).attr("data-fee");
                
                $("[f='but_sel_validtime']").removeClass('bg_main color_f').addClass('bg_f2');
                $(this).addClass('bg_main color_f');
                
                $("[f='fee_tips']").find('i').html($fee);
                $("#endtimes").val($days);
            });
            
            //发布
            $(document).on("click", "[f='but_submit_add']", function(){
                _this.obj_id = $(this).attr("id");
                
                var $title = $("#title").val();
                var $endtimes = $("#endtimes").val();
                
                var $ta_age1 = $("#ta_age1").val();
                var $ta_age2 = $("#ta_age2").val();
                var $ta_area1 = $("#ta_area1").val();
                var $ta_area2 = $("#ta_area2").val();
                var $ta_height1 = $("#ta_height1").val();
                var $ta_height2 = $("#ta_height2").val();
                var $ta_weight1 = $("#ta_weight1").val();
                var $ta_weight2 = $("#ta_weight2").val();
                var $ta_marry = $("#ta_marry").val();
                
                var $content = $("#content").val();
                
                var $thumbimg1 = $("#img1_thumb").val();
                var $img1 = $("#img1_url").val();
                var $thumbimg2 = $("#img2_thumb").val();
                var $img2 = $("#img2_url").val();
                var $thumbimg3 = $("#img3_thumb").val();
                var $img3 = $("#img3_url").val();
                var $thumbimg4 = $("#img4_thumb").val();
                var $img4 = $("#img4_url").val();
                var $thumbimg5 = $("#img5_thumb").val();
                var $img5 = $("#img5_url").val();
                var $thumbimg6 = $("#img6_thumb").val();
                var $img6 = $("#img6_url").val();
                
                if ($title == '') {
                    OEUI.toast({
                        type: 'bottom',
                        text: '请填写标题'
                    });
                    return;
                }
                if ($endtimes == '' || $endtimes == '0') {
                    OEUI.toast({
                        type: 'bottom',
                        text: '请选择有效时间'
                    });
                    return;
                }
                if ($ta_age1 == '' || $ta_age1 == '0') {
                    OEUI.toast({
                        type: 'bottom',
                        text: '请选择年龄要求'
                    });
                    return;
                }
                if ($ta_age2 == '' || $ta_age2 == '0') {
                    OEUI.toast({
                        type: 'bottom',
                        text: '请选择年龄要求'
                    });
                    return;
                }
                
                var $send_data = {
                    'a':'saveadd', 'title':$title, 'endtimes':$endtimes, 
                    'ta_age1':$ta_age1, 'ta_age2':$ta_age2, 'ta_area1':$ta_area1, 'ta_area2':$ta_area2, 
                    'ta_height1':$ta_height1, 'ta_height2':$ta_height2, 'ta_weight1':$ta_weight1, 
                    'ta_weight2':$ta_weight2, 'ta_marry':$ta_marry, 'content':$content,
                    'thumbimg1':$thumbimg1, 'img1':$img1, 'thumbimg2':$thumbimg2, 'img2':$img2, 
                    'thumbimg3':$thumbimg3, 'img3':$img3, 'thumbimg4':$thumbimg4, 'img4':$img4, 
                    'thumbimg5':$thumbimg5, 'img5':$img5, 'thumbimg6':$thumbimg6, 'img6':$img6
                };
                _this.sendData($send_data, 'add');
            });
            
        },
        
        clickEdit:function() {
            var _this = this;
            //编辑
            $(document).on("click", "[f='but_submit_edit']", function(){
                var $id = $(this).attr("data-id");
                _this.obj_id = $(this).attr("id");
                
                var $title = $("#title").val();

                var $ta_age1 = $("#ta_age1").val();
                var $ta_age2 = $("#ta_age2").val();
                var $ta_area1 = $("#ta_area1").val();
                var $ta_area2 = $("#ta_area2").val();
                var $ta_height1 = $("#ta_height1").val();
                var $ta_height2 = $("#ta_height2").val();
                var $ta_weight1 = $("#ta_weight1").val();
                var $ta_weight2 = $("#ta_weight2").val();
                var $ta_marry = $("#ta_marry").val();
                
                var $content = $("#content").val();
                
                var $thumbimg1 = $("#img1_thumb").val();
                var $img1 = $("#img1_url").val();
                var $thumbimg2 = $("#img2_thumb").val();
                var $img2 = $("#img2_url").val();
                var $thumbimg3 = $("#img3_thumb").val();
                var $img3 = $("#img3_url").val();
                var $thumbimg4 = $("#img4_thumb").val();
                var $img4 = $("#img4_url").val();
                var $thumbimg5 = $("#img5_thumb").val();
                var $img5 = $("#img5_url").val();
                var $thumbimg6 = $("#img6_thumb").val();
                var $img6 = $("#img6_url").val();
                
                if ($title == '') {
                    OEUI.toast({
                        type: 'bottom',
                        text: '请填写标题'
                    });
                    return;
                }
                if ($ta_age1 == '' || $ta_age1 == '0') {
                    OEUI.toast({
                        type: 'bottom',
                        text: '请选择年龄要求'
                    });
                    return;
                }
                if ($ta_age2 == '' || $ta_age2 == '0') {
                    OEUI.toast({
                        type: 'bottom',
                        text: '请选择年龄要求'
                    });
                    return;
                }
                
                var $send_data = {
                    'a':'saveedit', id:$id, 'title':$title, 'ta_age1':$ta_age1, 
                    'ta_age2':$ta_age2, 'ta_area1':$ta_area1, 'ta_area2':$ta_area2, 
                    'ta_height1':$ta_height1, 'ta_height2':$ta_height2, 'ta_weight1':$ta_weight1, 
                    'ta_weight2':$ta_weight2, 'ta_marry':$ta_marry, 'content':$content,
                    'thumbimg1':$thumbimg1, 'img1':$img1, 'thumbimg2':$thumbimg2, 'img2':$img2, 
                    'thumbimg3':$thumbimg3, 'img3':$img3, 'thumbimg4':$thumbimg4, 'img4':$img4, 
                    'thumbimg5':$thumbimg5, 'img5':$img5, 'thumbimg6':$thumbimg6, 'img6':$img6
                };
                _this.sendData($send_data, 'edit');
            });
        },
        
        clickPay:function() {
            var _this = this;
            
            //支付
            $(document).on("click", "[f='but_pay_marry']", function(){
                _this.id = $(this).attr("data-id");
                _this.obj_id = $(this).attr("id");
                
                var $send_data = {
                    'a':'confirmpay', 'id':_this.id
                };         
                _this.sendData($send_data, 'pay');
            });
        }, 
        
        clickDel:function() {
            var _this = this;
            //删除
            $(document).on("click", "[f='but_del_marry']", function(){
                _this.id = $(this).attr("data-id");
                _this.obj_id = $(this).attr("id");
                
                var $send_data = {
                    'a':'del', 'id':_this.id
                };
                OEUI.confirm({
                    title: '温馨提示',
                    text: '确定要删除吗？',
                    confirm: function () {
                        _this.sendData($send_data, 'del');
                    }
                });          
                
            });
        },
        
        //发送保存
        sendData:function($send_data, $do) {
            var _this = this;
            if (!$("#"+_this.obj_id).hasClass("forbid_submit")) {
                
                var loading = null;
                var timer = setTimeout(function () {
                    loading = OEUI.showLoading({
                        type:2
                    });
                }, 200);
                $("#"+_this.obj_id).addClass("forbid_submit");
                
                $.ajax({
                    type: "POST",
                    url: _this.api_url,
                    cache: false,
                    data:$send_data,
                    dataType: "json",
                    success: function($data) {
                        var $json = eval($data);
                        var $response = $json.response;
                        var $result = $json.result;
                        clearTimeout(timer);
                        loading && loading.hideLoading();
                        timer = null;
                        
                        if ($response == '1') {
                            if ($do == 'add') {
                                if ($result == 'approved') {
                                    OEUI.toast({
                                        type: 'bottom', 
                                        text: '发布成功，请等待网站审核'
                                    });
                                } 
                                else {
                                    OEUI.toast({
                                        type: 'bottom', 
                                        text: '发布成功'
                                    });
                                }
                                setTimeout(function(){
                                    window.location.reload();
                                }, 800);
                            }
                            else if ($do == 'edit') {
                                OEUI.toast({
                                    type: 'bottom', 
                                    text: '编辑成功'
                                });
                                setTimeout(function(){
                                    window.location.reload();
                                }, 800);
                                
                            }
                            else if ($do == 'del') {
                                OEUI.toast({
                                    type: 'bottom', 
                                    text: '删除成功'
                                });
                                $("#list_"+_this.id).remove();
                            }
                            else if ($do == 'pay') {
                                OEUI.toast({
                                    type: 'bottom', 
                                    text: '支付成功'
                                });
                                setTimeout(function(){
                                    window.location.reload();
                                }, 800);
                            }
                        }
                        else if ($response == '2') {
                            window.location.href = WAP_APPFILE+"&c=pay&paynum="+$result;
                        }
                        else {
                            if ($result.length == 0 || $result == '') {
                                $result = '操作失败，请检查';
                            }
                            OEUI.toast({
                                text: $result
                            }); 
                        }
                        
                        $("#"+_this.obj_id).removeClass("forbid_submit");
                    },
                    error: function() {
                        $("#"+_this.obj_id).removeClass("forbid_submit");
                        clearTimeout(timer);
                        loading && loading.hideLoading();
                        timer = null;
                        OEUI.toast({
                            text: '网络繁忙，请稍后再试！'
                        });
                    }
                });
            }
        }
        
    }
    new OEMyMarry();

});
