systemConfigurationModule.controller('TaxCalendarConfigController', ['$scope', '$log', 'uiGridConstants', 'SweetAlert', '$translate',
    'stdAccountService', '$interval', 'orgService', '$timeout', '$q', 'commonWebService', 'loginContext',
    'userService', 'taxCalendarService', '$state',
    function ($scope, $log, uiGridConstants, SweetAlert, $translate, stdAccountService, $interval, orgService,
        $timeout, $q, commonWebService, loginContext, userService, taxCalendarService, $state) {
        'use strict';

        $scope.test = function () {
            taxCalendarService.test().success(function () { });
        };

        //分页对象初始化-->start
        $scope.pagingOptions = {
            pageIndex: 1, //当前页码
            totalItems: 0, //总数据
            pageSize: constant.page.pageSizeArrary[1] //每页多少条数据
        };

        //分页对象初始化-->end

        //查询对象初始化-->start
        $scope.companyOptions = {
            bindingOptions: {
                dataSource: 'entityList'
            },
            displayExpr: "name",
            valueExpr: "id",
            showSelectionControls: true,
            applyValueMode: "instantly",
            multiline: false,
            placeholder: $translate.instant('PleaseSelect')
        };
        $scope.taskTypeOptions = {
            bindingOptions: {
                dataSource: 'taskTypeList'
            },
            displayExpr: "name",
            valueExpr: "id",
            showSelectionControls: true,
            applyValueMode: "instantly",
            multiline: false,
            placeholder: $translate.instant('PleaseSelect')
        };
        $scope.statusOptions = {
            dataSource: [{ id: 0, displayText: $translate.instant('Jinyong') }, { id: 1, displayText: $translate.instant('Qiyong') }],
            displayExpr: "displayText",
            valueExpr: "id",
            showClearButton: true,
            placeholder: $translate.instant('PleaseSelect')
        };
        $scope.configMonthOptions = {
            dataSource: [{ id: 1, month: $translate.instant('January') }, { id: 2, month: $translate.instant('February') }, { id: 3, month: $translate.instant('March') },
            { id: 4, month: $translate.instant('April') }, { id: 5, month: $translate.instant('May') }, { id: 6, month: $translate.instant('June') },
            { id: 7, month: $translate.instant('August') }, { id: 8, month: $translate.instant('July') }, { id: 9, month: $translate.instant('September') },
            { id: 10, month: $translate.instant('October') }, { id: 11, month: $translate.instant('November') }, { id: 12, month: $translate.instant('December') }],
            displayExpr: "month",
            valueExpr: "id",
            showSelectionControls: true,
            applyValueMode: "instantly",
            multiline: false,
            placeholder: $translate.instant('PleaseSelect')
        };
        $scope.configDateOptions = {
            dataSource: [{ id: 1, day: '1'+$translate.instant('Day') }, { id: 2, day: '2'+$translate.instant('Day') }, { id: 3, day: '3'+$translate.instant('Day') }, { id: 4, day: '4'+$translate.instant('Day') }, { id: 5, day: '5'+$translate.instant('Day') }, { id: 6, day: '6'+$translate.instant('Day') },
            { id: 7, day: '7'+$translate.instant('Day') }, { id: 8, day: '8'+$translate.instant('Day') }, { id: 9, day: '9'+$translate.instant('Day') }, { id: 10, day: '10'+$translate.instant('Day') }, { id: 11, day: '11'+$translate.instant('Day') }, { id: 12, day: '12'+$translate.instant('Day') },
            { id: 13, day: '13'+$translate.instant('Day') }, { id: 14, day: '14'+$translate.instant('Day') }, { id: 15, day: '15'+$translate.instant('Day') }, { id: 16, day: '16'+$translate.instant('Day') }, { id: 17, day: '17'+$translate.instant('Day') }, { id: 18, day: '18'+$translate.instant('Day') },
            { id: 19, day: '19'+$translate.instant('Day') }, { id: 20, day: '20'+$translate.instant('Day') }, { id: 21, day: '21'+$translate.instant('Day') }, { id: 22, day: '22'+$translate.instant('Day') }, { id: 23, day: '23'+$translate.instant('Day') }, { id: 24, day: '24'+$translate.instant('Day') },
            { id: 25, day: '25'+$translate.instant('Day') }, { id: 26, day: '26'+$translate.instant('Day') }, { id: 27, day: '27'+$translate.instant('Day') }, { id: 28, day: '28'+$translate.instant('Day') }, { id: 29, day: '29'+$translate.instant('Day') }, { id: 30, day: '30'+$translate.instant('Day') },
            { id: 31, day: '31' + $translate.instant('Day') }, { id: 32, day: $translate.instant('TheLastDayOfMonth') }],
            displayExpr: "day",
            valueExpr: "id",
            showSelectionControls: true,
            applyValueMode: "instantly",
            multiline: false,
            placeholder: $translate.instant('PleaseSelect')
        };
        // $scope.addressOptions = {
        //     bindingOptions: {
        //         dataSource: 'workPlaceList'
        //     },
        //     displayExpr: "name",
        //     valueExpr: "id",
        //     searchEnabled:true,
        //     showClearButton: true,
        //     placeholder: $translate.instant('PleaseSelect')
        // };
        $scope.operatorOptions = {
            bindingOptions:{
                dataSource: 'userList'
            },
            displayExpr: "name",
            valueExpr: "name",
            searchEnabled: true,
            showClearButton: true,
            placeholder: $translate.instant('PleaseSelect')
        };
        $scope.numberOptions = {
            placeholder: $translate.instant('PleaseInput')
        };

        $scope.gridSearchRadioOptions = {
            dataSource: [{ displayType: 2, displayText: $translate.instant('CompanyCalendarList') }, { displayType: 1, displayText: $translate.instant('CalendarConfigList') }],
            displayExpr: 'displayText',
            valueExpr: 'displayType',
            value: 1,
            layout: 'horizontal'
        };
        //查询对象初始化-->end

        $scope.taxCalendarConfigurationList = [];
        $scope.taxDataGridOptions = {
            columns: [
                { dataField: 'orderIndex', caption: $translate.instant('CalendarNumber'), fixed: true },
                {
                    dataField: 'entityNames', caption: $translate.instant('Company'), fixed: true, width: '280px'
                    //, cellTemplate: function (ele, info) {
                    //    if (info.data.companyNames && info.data.companyNames.length > 20) {
                    //        $('<span title="' + info.data.companyNames + '"/>').text(info.data.companyNames.substr(0, 20) + "...").appendTo(ele);
                    //    } else {
                    //        $('<span title="' + info.data.companyNames + '"/>').text(info.data.companyNames).appendTo(ele);
                    //    }

                    //}
                },
                { dataField: 'taskTypeNames', caption: $translate.instant('TaxEvent') },
                { dataField: 'projectMonths', caption: $translate.instant('EventMonth') },
                {
                    dataField: 'projectDays', caption: $translate.instant('EventDay'), cellTemplate: function (ele, info) {
                        $('<span/>').text(info.data.projectDays.replace('32', $translate.instant('TheLastDayOfMonth'))).appendTo(ele);
                    }
                },
                // {
                //     dataField: 'isRelatedTaxMoney', caption: $translate.instant('IsRelatedTaxAmount'), cellTemplate: function (ele,info) {
                //         $('<span/>').text(info.data.isRelatedTaxMoney == 0 ? $translate.instant('Fou') : $translate.instant('Shi')).appendTo(ele);
                //     }
                // },
                // { dataField: 'workPlaceNames', caption: $translate.instant('EventAddress') },
                { dataField: 'staff', caption: $translate.instant('EventPerson') },
                { dataField: 'validDate', caption: $translate.instant('ValidDate') },
                {
                    dataField: 'status', caption: $translate.instant('CalendarStatus'), cellTemplate: function (ele, info) {
                        $('<span/>').text(info.data.status == 0 ? $translate.instant('Jinyong') : $translate.instant('Qiyong')).appendTo(ele);
                    }
                },
                {
                    dataField: 'k', caption: $translate.instant('OrderOperation'), cellTemplate: function (ele, info) {
                        // if (window.PWC.isHavePermission(constant.adminPermission.systemConfiguration.taxCalendarConfig.queryCode, $scope.userPermissions)) {
                            $('<a/>').css({ 'color': '#d00', 'cursor': 'pointer' }).html($translate.instant('View') + '&nbsp;|&nbsp;').on('dxclick', function () {
                                goToEditConfig(info.data.id, 1);
                            }).appendTo(ele);
                        // }
                        // if (window.PWC.isHavePermission(constant.adminPermission.systemConfiguration.taxCalendarConfig.editConfig, $scope.userPermissions)) {
                            $('<a/>').css({ 'color': '#d00', 'cursor': 'pointer' }).html($translate.instant('Edit')).on('dxclick', function () {
                                goToEditConfig(info.data.id);
                            }).appendTo(ele);
                        // }

                        // $('<a/>').css({ 'color': '#d00', 'cursor': 'pointer' }).html($translate.instant('SetTixing') + '&nbsp;|&nbsp;').on('dxclick', function () { }).appendTo(ele);

                        // $('<a/>').css({ 'color': '#d00', 'cursor': 'pointer' }).html($translate.instant('OtherConfig')).on('dxclick', function () { }).appendTo(ele);
                    }
                }
            ],
            keyExpr: 'id',
            showRowLines: true,
            showColumnLines: true,
            showBorders: true,
            allowColumnResizing: true,
            columnAutoWidth: true,
            loadPanel: {
                enabled: false // or false | "auto"
            },
            allowColumnReordering: true,
            columnFixing: {
                enabled: true
            },
            paging: {
                enabled: false
            },
            bindingOptions: {
                height: 'dataGridHeight',
                dataSource: 'taxCalendarConfigurationList'
            },
            remoteOperations: {
                sorting: true
            },
        };

        //The define of Grid options. -->start
        $scope.companyCalendarList = [];
        $scope.companyCalendarGridOptions = {
            bindingOptions: {
                dataSource: 'companyCalendarList',
                height: 'dataGridHeight'
            },
            noDataText: $scope.noDataText,
            columnAutoWidth: true,
            showRowLines: true,
            showBorders: true,
            paging: {
                enabled: false
            },
            columns: [
                { dataField: 'entityName', caption: $translate.instant('Company'), allowHeaderFiltering: false },
                { dataField: 'calendarCount', caption: $translate.instant('TaxCalendarCount'), allowHeaderFiltering: false },
                // { dataField: 'taskTypeCount', caption: $translate.instant('TaxProjectCount'), allowHeaderFiltering: false },
            ],
            masterDetail: {
                enabled: true,
                template: "companyCalendarTemplate"
            },
            companyView: function (companyCalendars) {
                return {
                    keyExpr: 'id',
                    dataSource: companyCalendars,
                    noDataText: $scope.noDataText,
                    columnAutoWidth: true,
                    showBorders: true,
                    columns: [
                        { dataField: 'orderIndex', caption: $translate.instant('CalendarNumber'), fixed: true },
                        { dataField: 'taskTypeNames', caption: $translate.instant('TaxEvent') },
                        { dataField: 'projectMonths', caption: $translate.instant('EventMonth') },
                        {
                            dataField: 'projectDays', caption: $translate.instant('EventDay'), cellTemplate: function (ele, info) {
                                $('<span/>').text(info.data.projectDays.replace('32', $translate.instant('TheLastDayOfMonth'))).appendTo(ele);
                            }
                        },
                        // {
                        //     dataField: 'isRelatedTaxMoney', caption: $translate.instant('IsRelatedTaxAmount'), cellTemplate: function (ele, info) {
                        //         $('<span/>').text(info.data.isRelatedTaxMoney == 0 ? $translate.instant('Fou') : $translate.instant('Shi')).appendTo(ele);
                        //     }
                        // },
                        // { dataField: 'workPlaceNames', caption: $translate.instant('EventAddress') },
                        { dataField: 'staff', caption: $translate.instant('EventPerson') },
                        { dataField: 'validDate', caption: $translate.instant('ValidDate') },
                        {
                            dataField: 'status', caption: $translate.instant('CalendarStatus'), cellTemplate: function (ele, info) {
                                $('<span/>').text(info.data.status == 0 ? $translate.instant('Jinyong') : $translate.instant('Qiyong')).appendTo(ele);
                            }
                        },
                        {
                            dataField: 'k', caption: $translate.instant('OrderOperation'), cellTemplate: function (ele, info) {
                                // if (window.PWC.isHavePermission(constant.adminPermission.systemConfiguration.taxCalendarConfig.queryCode, $scope.userPermissions)) {
                                    $('<a/>').css({ 'color': '#d00', 'cursor': 'pointer' }).html($translate.instant('View') + '&nbsp;|&nbsp;').on('dxclick', function () {
                                        goToEditConfig(info.data.id, 1);
                                    }).appendTo(ele);
                                // }

                                // if (window.PWC.isHavePermission(constant.adminPermission.systemConfiguration.taxCalendarConfig.editConfig, $scope.userPermissions)) {
                                    $('<a/>').css({ 'color': '#d00', 'cursor': 'pointer' }).html($translate.instant('Edit')).on('dxclick', function () {
                                        goToEditConfig(info.data.id);
                                    }).appendTo(ele);
                                // }


                                // $('<a/>').css({ 'color': '#d00', 'cursor': 'pointer' }).html($translate.instant('SetTixing') + '&nbsp;|&nbsp;').on('dxclick', function () { }).appendTo(ele);

                                // $('<a/>').css({ 'color': '#d00', 'cursor': 'pointer' }).html($translate.instant('OtherConfig')).on('dxclick', function () { }).appendTo(ele);
                            }
                        }
                    ]
                };

            }
        };

        var goToEditConfig = function (id,isReadOnly) {
            $state.go('taxCalendarConfigurationEdit', {
                id: id,
                isReadOnly:isReadOnly
            });
        };

        $scope.isShowMoreSearchCondition = false;
        $scope.taggoleSearchCondition = function () {
            $scope.isShowMoreSearchCondition = !$scope.isShowMoreSearchCondition;
            $timeout(function () {
                $scope.setTaxGridHeight();
            }, 100);
        };

        $scope.resetQueryParam = function () {
            $scope.queryEntity = {
                queryType: 2
            };

            $scope.pagingOptions = {
                pageIndex: 1, //当前页码
                totalItems: 0, //总数据
                pageSize: constant.page.pageSizeArrary[1], //每页多少条数据 
            };

            queryList();
        };

        $scope.queryEntity = {
            queryType: 2
        };

        var queryList = function () {
            $scope.queryEntity.pagingParam = $scope.pagingOptions;
            $scope.queryEntity.pagingParam.totalCount = $scope.queryEntity.pagingParam.totalItems;
            taxCalendarService.getCalendarConfigList($scope.queryEntity).success(function (result) {
                if (result && result.result) {
                    if ($scope.queryEntity.queryType == 2) {
                        $scope.companyCalendarList = result.data.list;
                        $scope.taxCalendarConfigurationList = [];
                    } else {
                        $scope.taxCalendarConfigurationList = result.data.list;
                        $scope.companyCalendarList = [];
                    }
                    $scope.pagingOptions.totalItems = result.data.pageInfo.totalCount;
                }
            });
        };

        $scope.$watch('queryEntity.queryType', function (newValue, oldValue) {
            if (oldValue) {
                queryList();
            }
        });

        (function () {
            $scope.userList = [];
            $scope.entityList = [];
            // $scope.workPlaceList = [];
            $scope.goToEditConfig = goToEditConfig;
            $scope.queryList = queryList;
            $timeout(function () {
                $scope.setTaxGridHeight();
            }, 100);
            userService.getAllAvalideUserList().success(function (result) {
                if (result) {
                    $scope.userList = _.map(result, function (item) {
                        var obj = { id: item.id, name: item.userName };
                        return obj;
                    });
                }
            });

            taxCalendarService.getActiveEntityList().success(function (result) {
                if(result.result) {
                    $scope.entityList = result.data;
                }
            });

            // taxCalendarService.getWorkPlaceList().success(function (result) {
            //     if (result) {
            //         $scope.workPlaceList = result;
            //     }
            // });

            taxCalendarService.getTaskTypeList().success(function (result) {
                if (result && result.result && result.data) {
                    $scope.taskTypeList = result.data;
                }
            });

            userService.getUserPermissionNew(loginContext.userName, function (data) {
                $scope.userPermissions = data;
            });

            queryList();
        })();
    }]);