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

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'manystore/index/index'  + location.search,
                    add_url: 'manystore/index/add'  + location.search,
                    edit_url: 'manystore/index/edit'  + location.search,
                    del_url: 'manystore/index/del',
                    url_url: 'manystore/index/url',
                    free_url: 'manystore/index/free',
                    multi_url: 'manystore/index/multi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},

                        {field: 'id', title: '账号ID'},
                        {field: 'shop_id', title: '机构ID'},
                        {field: 'shop.name', title: __('申请姓名|机构名'), operate: 'LIKE'},
                        {field: 'check_full', title: __('是否完善展示信息'),
                            searchList: {"false":__('未完善'),"true":__('已完善')}, formatter: Table.api.formatter.normal

                            // formatter:function (value, row, index) {
                            //     if(row.check_full){
                            //         return '<span style="color: green">已完善</span>';
                            //     }else{
                            //         return '<span style="color: red">未完善</span>';
                            //     }
                            // }
                            },

                        {field: 'shop.logo', title: __('Logo'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},


                        {field: 'username', title: __('Username')},
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'email', title: __('Email')},
                        {field: 'status', title: __("Status"), formatter: Table.api.formatter.status},
                        {field: 'shop.status', title: __('Auth_status'), searchList: {"0":__('Auth_status 0'),"1":__('Auth_status 1'),"2":__('Auth_status 2')}, formatter: Table.api.formatter.status},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'shop.type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},

                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'user.mobile', title: __('User.mobile'), operate: 'LIKE'},
                        {field: 'user.avatar', title: __('User.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},

                        {field: 'shop.shop_star', title: __('机构评分'),sortable:true},
                        {field: 'shop.star_number', title: __('评价数'),sortable:true},


                        // {field: 'user_id', title: __('申请姓名|机构名'), operate: 'LIKE'},
                        {field: 'shop.image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'shop.address_city', title: __('Address_city'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'shop.province', title: __('Province'),visible:false},
                        {field: 'shop.city', title: __('City'),visible:false},
                        {field: 'shop.district', title: __('District'),visible:false},
                        {field: 'shop.address', title: __('Address'), operate: 'LIKE',visible:false},
                        {field: 'shop.address_detail', title: __('Address_detail'), operate: 'LIKE',visible:false},


                        // {field: 'shop.yyzz_images', title: __('Yyzz_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'shop.front_idcard_image', title: __('身份证正面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'shop.reverse_idcard_image', title: __('身份证反面'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'shop.tel', title: __('Tel'), operate: 'LIKE'},




                        {field: 'logintime', title: __('Login time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},


                        {field: 'operate', title: __('Operate'),width:180, table: table , buttons: [
                                {name: 'url',
                                    text: '查看小程序链接',
                                    icon: 'fa fa-chain',
                                    classname: 'btn btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.url_url,
                                    confirm: '查看小程序链接',
                                    dropdown : '更多',
                                    success: function (data, ret) {
                                        //JS将内容放入复制板
                                        try {
                                            navigator.clipboard.writeText(ret.msg);
                                            console.log('Text copied to clipboard');
                                            Layer.alert("已自动复制内容 : " + ret.msg );
                                        } catch (err) {
                                            console.error('Failed to copy: ', err);
                                            Layer.alert("浏览器不支持复制,请自行复制 : " + ret.msg );
                                        }

                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    // visible: function (row) {
                                    //     //显示条件 只能待入住订单
                                    //     if(row.status == '3'){
                                    //         return true;
                                    //     }
                                    //     return false;
                                    // }
                                },

                            {name: 'free',
                                    text: '免登录进入机构后台',
                                    icon: 'fa fa-sign-in',
                                    classname: 'btn btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.free_url,
                                    confirm: '确认免登录进入机构后台?如果已登录其他机构,对方账号将被强制退出。',
                                    dropdown : '更多',
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg );
                                        $(".btn-refresh").trigger("click");
                                        //js获取当前域名


                                        window.open(shop_backend_url, '_blank');
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        //显示条件 只能待入住订单
                                        // if((row.status == 2 || row.status == 3) && row.mock_type == "base" && row.add_mock_status == "1" ){
                                        return true;
                                        // }
                                        // return false;
                                    }},
                                {
                                    name: 'classes_spec',
                                    text: __('机构课程'),
                                    title: __('机构课程'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-leanpub',
                                    dropdown : '更多',
                                    url: classes_spec_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'activity',
                                    text: __('机构线上活动'),
                                    title: __('机构线上活动'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-leanpub',
                                    dropdown : '更多',
                                    url: activity_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.paytime;
                                    // }
                                },

                                {
                                    name: 'teacher',
                                    text: __('主讲老师信息'),
                                    title: __('主讲老师信息'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-user',
                                    dropdown : '更多',
                                    url: teacher_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'verification',
                                    text: __('核销员信息'),
                                    title: __('核销员信息'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-user',
                                    dropdown : '更多',
                                    url: verification_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'virtual_user',
                                    text: __('机构虚拟参与者'),
                                    title: __('机构虚拟参与者'),
                                    classname: 'btn  btn-dialog',
                                    icon: 'fa fa-user',
                                    dropdown : '更多',
                                    url: virtual_user_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'order',
                                    text: __('机构课程购买订单'),
                                    title: __('机构课程购买订单'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-cart-arrow-down',
                                    dropdown : '更多',
                                    url: order_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'hourorder',
                                    text: __('机构课时预约记录'),
                                    title: __('机构课时预约记录'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-calendar',
                                    dropdown : '更多',
                                    url: hourorder_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'activity_order',
                                    text: __('机构活动订单'),
                                    title: __('机构活动订单'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-cart-arrow-down',
                                    dropdown : '更多',
                                    url: activity_order_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'user_auth',
                                    text: __('机构授权用户'),
                                    title: __('机构授权用户'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-user',
                                    dropdown : '更多',
                                    url: user_auth_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.status == '2'||row.status == '3';
                                    // }
                                },
                                {
                                    name: 'evaluate',
                                    text: __('查看用户评价'),
                                    title: __('查看用户评价'),
                                    classname: 'btn btn-dialog',
                                    icon: 'fa fa-handshake-o',
                                    dropdown : '更多',
                                    url: evaluate_url,
                                    callback: function (data) {

                                    },
                                    // visible: function (row) {
                                    //     return row.classes_evaluate_id;
                                    // }
                                },
                                //
                                // {name: 'unsetmockauth',
                                //     text: '取消加圈资格',
                                //     icon: 'fa fa-sign-in',
                                //     classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                //     url: $.fn.bootstrapTable.defaults.extend.unsetmockauth_url,
                                //     confirm: '(分车版本)确认取消用户加圈资格!',
                                //     success: function (data, ret) {
                                //         Layer.alert(ret.msg );
                                //         $(".btn-refresh").trigger("click");
                                //     },
                                //     error: function (data, ret) {
                                //         Layer.alert(ret.msg);
                                //         return false;
                                //     },
                                //     visible: function (row) {
                                //         //显示条件 只能待入住订单
                                //         if((row.status == 2 || row.status == 3) && row.mock_type == "base" &&  row.add_mock_status == "2"){
                                //             return true;
                                //         }
                                //         return false;
                                //     }},

                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate},


                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#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);
                }
                $(this).blur();

            });


                // $(".city-select-tab a").removeClass('active');
                $('[data-count="district"]').tab('show');



            $(document).on('click', '.btn-changeuser', function (event) {
                var url = $(this).attr('data-url');
                if(!url) return false;
                var title = $(this).attr('title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                // var ids = $(this).attr('data-id');
                var area = [$(window).width() > 800 ? (width?width:'800px') : '95%', $(window).height() > 600 ? (height?height:'600px') : '95%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(ret){//回调方法,需要在本页面Controller中增加方法监听且调用Fast.api.close(ret)传递结果;
                        Fast.api.close(ret);
                    }
                };
                Fast.api.open(url,title,options);
            });

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


            Form.api.bindevent($("form[role=form]"));
        },
        edit: function () {
            $("#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).on('click', '.btn-changeuser', function (event) {
                var url = $(this).attr('data-url');
                if(!url) return false;
                var title = $(this).attr('title');
                var width = $(this).attr('data-width');
                var height = $(this).attr('data-height');
                // var ids = $(this).attr('data-id');
                var area = [$(window).width() > 800 ? (width?width:'800px') : '95%', $(window).height() > 600 ? (height?height:'600px') : '95%'];
                var options = {
                    shadeClose: false,
                    shade: [0.3, '#393D49'],
                    area: area,
                    callback:function(ret){//回调方法,需要在本页面Controller中增加方法监听且调用Fast.api.close(ret)传递结果;
                        Fast.api.close(ret);
                    }
                };
                Fast.api.open(url,title,options);
            });


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

            });

            Form.api.bindevent($("form[role=form]"));
        }
    };


    var classes_spec_url = function (row,dom) {
        return 'school/classes/classes_lib/index?shop_id='+row.shop_id;
    }

    var virtual_user_url = function (row,dom) {
        return 'school/classes/virtual_user/index?shop_id='+row.shop_id;
    }
    var teacher_url = function (row,dom) {
        return 'school/classes/teacher/index?shop_id='+row.shop_id;
    }


    var verification_url = function (row,dom) {
        return 'school/classes/verification/index?shop_id='+row.shop_id;
    }


    var order_url = function (row,dom) {
        return 'school/classes/order/order/index?shop_id='+row.shop_id;
    }

    //hourorder_url
    var hourorder_url = function (row,dom) {
        return 'school/classes/hourorder/order/index?shop_id='+row.shop_id;
    }

    var user_auth_url = function (row,dom) {
        return 'manystore/user_auth/index?shop_id='+row.shop_id;
    }

    var evaluate_url= function (row,dom) {
        return 'school/classes/evaluate/index?shop_id='+row.shop_id;
    }


    var activity_order_url = function (row,dom) {
        return 'school/classes/activity/order/order/index?shop_id='+row.shop_id;
    }

    var activity_url = function (row,dom) {
            return 'school/classes/activity/activity/index?shop_id='+row.shop_id;
        }


    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;
});