define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'upload'], function ($, undefined, Backend, Table, Form, Upload) {

    var Controller = {
        index: function () {

            // 给上传按钮添加上传成功事件
            $("#plupload-avatar").data("upload-success", function (data) {
                var url = Backend.api.cdnurl(data.url);
                $(".profile-user-img").prop("src", url);
                Toastr.success("上传成功！");
            });

            // 给表单绑定事件
            Form.api.bindevent($("#update-form"), function () {
                $("input[name='row[password]']").val('');
                var url = Backend.api.cdnurl($("#c-avatar").val());
                top.window.$(".user-panel .image img,.user-menu > a > img,.user-header > img").prop("src", url);
                return true;
            });


             $("#c-address_city").on("cp:updated", function() {
                var citypicker = $(this).data("citypicker");
                var province = citypicker.getCode("province");
                var city = citypicker.getCode("city");
                var district = citypicker.getCode("district");
                if(province){
                    $("#province").val(province);
                }
                if(city){
                    $("#city").val(city);
                }
                if(district){
                    $("#district").val(district);
                }
            });

            $(document).ready(function() {
                generrate.listen();
                generrate.setOfflineType($("#c-type").val());

            });


            Form.api.bindevent($("#shop-form"));
        },
    };


    var generrate = {
        listen:function () {
            this.offlineListen();
            this.priceListen();
        },
        offlineListen:function () {
            var that = this;
            // console.log($("input:radio[name='row[address_type]']").val())
            // this.setOfflineType($("input:radio[name='row[address_type]']").val());
            // $("input:radio[name='shop[type]']").change(function (){
            //     that.setOfflineType($(this).val());
            // });
            $(document).on("change", "#c-type", function(){
                //变更后的回调事件
                that.setOfflineType($(this).val());
            });


        },
        setOfflineType:function (val) {
            switch (val) { //1=个人,2=机构
                case '1': //1=个人
                    $('#c_person').show();
                    $('#c_institution').hide();
                    $('.c_institution').hide();



                    $(document).ready(function() {
                        $('#c-yyzzdm').removeAttr('data-rule');
                        $('#c-yyzz_images').removeAttr('data-rule');
                        $('#c-legal_entity').removeAttr('data-rule');
                    });


                    break;
                case '2': //2=机构
                    $('#c_person').hide();
                    //data-rule="required"
                    $('#c_institution').show();
                    $('.c_institution').show();

                    // $('#c-yyzzdm').attr('data-rule', 'required');
                    // $('#c-yyzz_images').attr('data-rule', 'required');

                    $(document).ready(function() {
                        $('#c-yyzzdm').attr('data-rule', 'required');
                        $('#c-yyzz_images').attr('data-rule', 'required');
                        $('#c-legal_entity').attr('data-rule', 'required');
                    });

                    break;
            }
            Form.api.bindevent($("form[role=form]"));
        },
        priceListen:function () {
            // $('#spec').bind('input propertychange', function(){
            //     var length = $("#spec").val().length;
            //     console.log(length);
            // });
        }
    }


    return Controller;
});
