/*
 * autoTemp 扩展  
 * Date   : 2020-08-31
 * Version: 2.0
 * author : Lith
 * email  : LithWang@outlook.com

 */
; (function (scope) {


    function getOptHtml(row, controller) {
        var html = '';
        var id = row[controller.controllerConfig.idField];
        for (var t in controller.rowButtons) {
            var item = controller.rowButtons[t];
            html += " <a  href=\"javascript: controller.rowButtons['" + t + "'].onclick('" + id + "');\" >" + String.toXmlStr(item.text) + "</a> ";
        }
        return html;
    }


    function buildGridConfig(controller) {

        //self.gridConfig = {
        //    "title":"xxxx",
        //    "idField":"id",
        //    "treeField":"name",  //若指定 则代表为树形控件
        //    "toolbar":[   {text:'刷新',  iconCls:'icon-reload',  handler:function(){}   },   '-' ]
        //
        //    "frozenColumns": [[
        //        //{ field: 'ck', checkbox: true },
        //        { field: 'username', title: '装修商', align: "center", width: 250, formatter: outSpan, sortable: true }

        //    ]],
        //    "columns": [[
        //        { field: 'askCount', title: '询单数', align: "center", width: 80, formatter: outSpan, sortable: true }
        //    ]]
        //};


        var controllerConfig = controller.controllerConfig;

        var gridConfig = {};

        // #1 idField treeField
        gridConfig.idField = controllerConfig.idField;
        gridConfig.treeField = controllerConfig.treeField;


        // #2 title
        if (controllerConfig.listConfig?.title) {
            gridConfig.title = controllerConfig.listConfig.title;
        }


        // #3 toolbar (from listButtons)
        if (controllerConfig.listConfig?.listButtons) {

            var toolbar = gridConfig.toolbar = [];

            var buttons = controllerConfig.listConfig?.listButtons;
            for (var t in buttons) {
                buildButton(buttons[t]);
            }


            function buildButton(button) {
                toolbar.push({
                    text: button.text, handler: function () {

                        // ##1 jsHandler
                        if (button.jsHandler) {
                            if (typeof (button.jsHandler) == 'string') {
                                button.handler = eval('(' + button.jsHandler + ')');
                            }
                            //theme.progressStart(button.title);

                            let arg = { ...button, progressStart: theme.progressStart, progressStop: theme.progressStop };
                            let rows = template.getRows() || [];
                            button.handler(arg, rows);
                            return;
                        }


                        // ##2 ajax
                        if (button.ajax) {

                            theme.confirm(button.title + '?', function () {

                                autoTemp.dataProvider.ajax({
                                    url: button.ajax.url,
                                    type: button.ajax.type || 'GET',
                                    //header: {},
                                    //data: {},
                                    onSuc: function (apiRet) {
                                        theme.progressStop();
                                        if (theme.alertApiReturn(apiRet, '操作成功')) {
                                            location.reload();
                                        }
                                    }
                                });

                                theme.progressStart(button.text);

                            });
                        }
                    }
                });
            }
        }


        // #4 row buttons
        if (controllerConfig.listConfig?.rowButtons) {
            var rowButtons = controllerConfig.listConfig.rowButtons;
            $.each(rowButtons, function (i, button) {

                controller.addRowButton({
                    text: button.title,
                    onclick: function (id) {
                        // ##1 jsHandler
                        if (button.jsHandler) {
                            if (typeof (button.jsHandler) == 'string') {
                                button.handler = eval('(' + button.jsHandler + ')');
                            }
                            //theme.progressStart(button.title);

                            let arg = { ...button, progressStart: theme.progressStart, progressStop: theme.progressStop };
                            let row = template.getRow(id) || {};
                            button.handler(arg, id, row);
                            return;
                        }


                        // ##2 ajax
                        if (button.ajax) {

                            theme.confirm(button.title + '?', function () {

                                autoTemp.dataProvider.ajax({
                                    url: button.ajax.url.replace('{id}', id),
                                    type: button.ajax.type || 'GET',
                                    //header: {},
                                    //data: {},
                                    onSuc: function (apiRet) {
                                        theme.progressStop();
                                        theme.alertApiReturn(apiRet, '操作成功');
                                    }
                                });

                                theme.progressStart(button.text);

                            });
                        }

                    }
                });
            });
        }


        // #5 fields
        if (controller.atFields) {
            var atFields = controller.atFields;
            var frozenColumns = [
                //{ field: 'ck', checkbox: true }
            ];
            var columns = [];
            // ##1 column
            gridConfig.frozenColumns = [frozenColumns];
            gridConfig.columns = [columns];
            for (var t in atFields) {
                var atField = atFields[t];
                var item = atField.list_init();
                if (item) columns.push(item);
            }

            // ##2 opt column
            frozenColumns.push({
                field: 'opt', title: '操作', width: 250, align: "center", formatter: function (value, row, index) {
                    return getOptHtml(row, controller);
                }
            });
        }


        return gridConfig;
    }



    scope.Controller = function (controllerConfig) {
        var self = this;

        //(x.1) fire event list.controller.beforeCreate
        autoTemp.eventListener.fireEvent('list.controller.beforeCreate', [self, controllerConfig]);

        self.controllerConfig = controllerConfig;

        self.rowButtons = []


        if (!controllerConfig.idField) controllerConfig.idField = 'id';
        if (!controllerConfig.pidField) controllerConfig.pidField = 'pid';

        //创建 atFields
        self.atFields = scope.Controller.createAtFields(self.controllerConfig.fields);


        self.addRowButton = function (btnConfig) {
            self.rowButtons.push(btnConfig);
        };

        self.getPermit = function (opt) {
            if (!self.controllerConfig.permit) return true;
            var value = self.controllerConfig.permit[opt];
            return value != false && value != 'false';
        };



        self.list_init = function () {

            //(x.1)添加row-opt 查看
            if (self.getPermit('show')) {
                self.addRowButton({
                    text: '查看',
                    onclick: function (id) {
                        var search = location.search;
                        search += search ? '&' : '?';
                        search += 'mode=show&id=' + id;
                        theme.popDialog('item.html' + search, '查看');
                    }
                });
            }

            //(x.2)添加row-opt 修改
            if (self.getPermit('update')) {
                self.addRowButton({
                    text: '修改',
                    onclick: function (id) {
                        var dialogParam = {
                            init: function () {
                                var iframe = this.iframe.contentWindow;
                                iframe.window.event_afterSave = function () { template.refreshAfterChange(id); };
                            }
                        };

                        var search = location.search;
                        search += search ? '&' : '?';
                        search += 'mode=update&id=' + id;
                        theme.popDialog('item.html' + search, '修改', null, null, dialogParam);
                    }
                });
            }

            //(x.3)添加row-opt 删除
            if (self.getPermit('delete')) {
                self.addRowButton({
                    text: '删除',
                    onclick: function (id) {

                        theme.confirm('删除之后不可还原，确认继续吗？', function () {
                            theme.progressStart('删除');
                            dataProvider.delete(id, function (apiRet) {
                                theme.progressStop();
                                if (theme.alertApiReturn(apiRet, '删除成功')) {
                                    template.refreshAfterChange(id);
                                }
                            });
                        });
                    }
                });
            }

            //(x.4)buildGridConfig
            return buildGridConfig(self);
        };
    };




    scope.Controller.createAtFields = function createAtFields(fieldConfigs) {
        if (!fieldConfigs || fieldConfigs.length == 0) return [];

        var atFields = [];
        for (var t in fieldConfigs) {
            var config = fieldConfigs[t];
            try {
                var field = new autoTemp.Field(config);
                atFields.push(field);
            } catch (error) {
                console.log(error);
            }
        }
        return atFields;
    };


})(autoTemp);