region-manage.ctrl.js 37.7 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6 7 8 9
basicDataModule
    .controller('RegionManageController', ['$scope', '$log', 'SweetAlert', 'regionService', '$translate', 'areaRegionService', 'uiGridTreeViewConstants', '$timeout', 'areaService', '$q',
        function ($scope, $log, SweetAlert, regionService, $translate, areaRegionService, uiGridTreeViewConstants, $timeout, areaService, $q) {
            'use strict';

            var tree;
            $scope.my_tree = tree = {};
            $scope.defaultCity = {
                id: '',
10
                parentID: '',
eddie.woo's avatar
eddie.woo committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
                name: ''
            };

            $scope.regionTypeData = [{
                type: 0,
                name: $translate.instant('AdministrativeRegions')
            }, {
                type: 1,
                name: $translate.instant('CustomArea')
            }];

            var isActiveMap = {
                false: 'Enable',
                true: 'Disable'
            };

            $scope.awesomeCallback = function (node, tree) {
                // Do something with node or tree
                $log.info('callback');
            };

            $scope.otherAwesomeCallback = function (node, isSelected, tree) {
                // Do soemthing with node or tree based on isSelected
                $log.info('other call back', node);
                $scope.tree = tree;
            }


            var getProvinceAndCityList = function (branch) {
                var citys = [];
                if (branch.itemData.data.isArea) {
                    // 如果是自定义区域

                    if (branch.children && branch.children.length > 0) {
                        branch.children.forEach(function (row) {
                            if (!row.itemData.data.isArea) {
                                var tempList = getProvinceAndCityList(row);

                                citys = _.union(citys, tempList);
                            }
                        });
                    }


                } else {
                    // 如果是行政区域, 省
                    if (branch.children && branch.children.length > 0) {
                        var tempCities = [];
                        branch.children.forEach(function (row) {
                            citys.push({
                                id: row.itemData.id,
                                name: row.text,
                                level: 2,
                                status: 2,
                                parentId: row.itemData.parentId,
                            });
                        });

                        //var allCities = _.find($scope.provinceCityData, function(num) {
                        //    return num.id === branch.itemData.id
                        //});

                        //if (allCities && allCities.children && allCities.children.length === branch.children.length) {
                        //    citys.push({
                        //        id: branch.itemData.id,
                        //        name: branch.text,
                        //        level: 1,
                        //        status: 2
                        //    });
                        //} else {
                        //    citys.push({
                        //        id: branch.itemData.id,
                        //        name: branch.text,
                        //        level: 1,
                        //        status: 1
                        //    });
                        //}

                    } else {
                        // 市
                    }
                }

                return citys;
            };

            var colorPanel = {

                // 颜色列表
                // yellow,orange,gold
                colorList: ['#FFB300', '#FEE002', '#FFFA23'],

                createNew: function () {
                    var colorPanelInstance = {};
                    colorPanelInstance.index = 0;
                    colorPanelInstance.getNextColor = function () {
                        if (colorPanelInstance.index >= colorPanel.colorList.length - 1) {
                            colorPanelInstance.index = 0;
                        } else {
                            colorPanelInstance.index++;
                        }

                        return colorPanel.colorList[colorPanelInstance.index];
                    }

                    return colorPanelInstance;
                }
            };

            var colorPanelInstance = colorPanel.createNew();

            //var loadRegionTree = function () {
            //    $scope.doing_async = true;
            //    regionService.getSettingRegionTree().success(function (data) {
            //        initParam();
            //        setAreaRegionTreeColor(data);
            //        $scope.areaRegionTreeData = data;

            //        $timeout(function () {

            //            if ($scope.selectedBranch) {
            //                $scope.gridInstance.selectItem($scope.selectedBranch.key);
            //            } else {
            //                $scope.gridInstance.selectItem(data[0].id);
            //            }
            //        }, 20);

            //    });
            //};


            var setAreaRegionTreeColor = function (data) {
                if (data && data.length > 0) {

                    data.forEach(function (row) {

                        var color = colorPanelInstance.getNextColor();
                        row.data.color = color;

                        if (row.items && row.items.length > 0) {
                            row.items.forEach(function (row2) {
                                setColor(row2, color);
                            });
                        }
                    });

                    // if (data[0].items && data[0].items.length > 0) {
                    //     data[0].items.forEach(function(row) {
                    //         var color = colorPanelInstance.getNextColor();
                    //         row.data.color = color;

                    //         if (row.children && row.children.length > 0) {
                    //             row.children.forEach(function(row2) {
                    //                 setColor(row2, color);
                    //             });
                    //         }
                    //     });
                    // }
                }

            };

            var orders = new DevExpress.data.ODataStore({
                url: "http://localhost:20001/api/v1/region/getSettingRegionTree"
            });

            var dataSource = new DevExpress.data.DataSource({
                load: function (loadOptions) {
                    var d = new $.Deferred();

                    regionService.getSettingRegionTree().success(function (data) {


                        initParam();
                        setAreaRegionTreeColor(data);
                        //$scope.areaRegionTreeData = data;

                        $timeout(function () {

                            if ($scope.selectedBranch) {
                                $scope.gridInstance.selectItem($scope.selectedBranch.key);
                            } else {

                                // 菜单直接点进来就可以加一级
                                // $scope.gridInstance.selectItem(data[0].id);
                            }
                        }, 20);

                        d.resolve(data);

                    });

                    return d.promise();
                }
            });

            $scope.unSelectArea = function (event) {

                var className = event.target.className;
                if (className && (className.indexOf('cannot-click') > -1 || className.indexOf('material-icons') > -1)) {
                    return;
                }

                $scope.gridInstance.unselectAll();
                $scope.selectedBranch = null;
                initParam();
                $timeout(refreshMap, 20);
            };

            var loadRegionTree = function () {
                $scope.areaTreeViewOptions = {
                    // bindingOptions: {
                    //     dataSource: 'areaRegionTreeData',
                    //     searchValue: 'searchValue',
                    //     expandAllEnabled: 'expandAll',
                    // },
                    itemTemplate: 'itemAreaTreeTemplate',
                    dataStructure: "plain",
                    //dataSource: new DevExpress.data.DataSource({
                    //    store: new DevExpress.data.ODataStore({
                    //        url: "http://localhost:20001/api/v1/region/getSettingRegionTree"
                    //    })
                    //}),
                    dataSource: dataSource,
                    // remoteOperations: {
                    //     sorting: true,
                    //     //paging: true
                    // },
                    // selection: {
                    //     mode: "single"
                    // },
                    selectionMode: 'single', //单选
                    loadPanel: {
                        enabled: true
                    },
                    scrolling: {
                        mode: "virtual"
                    },
                    keyExpr: "uniqueId",
                    showRowLines: true,
                    showColumnLines: true,
                    rowAlternationEnabled: true,
                    showBorders: true,
                    // selectAllText: $translate.instant('SelectAll'),
                    selectByClick: true, // 点击选中
                    scrollDirection: 'vertical', //Accepted Values: 'vertical' | 'horizontal' | 'both'
                    selectNodesRecursive: false, //级联选择
                    onInitialized: function (e) {
                        $scope.gridInstance = e.component;
                    },
                    onItemClick: function (e) {

                        // $scope.gridInstance.selectItem(e.node.key);
                        // if ($scope.selectedBranch != null && $scope.selectedBranch.key === e.node.key){
                        //     //默认全部不选择
                        //     $scope.gridInstance.unselectAll();

                        //     $scope.selectedBranch = null;
                        //     $timeout(refreshMap, 20);
                        //     return;
                        // }

                        $scope.selectedBranch = e.node;

                        // 可以添加的条件
                        if ($scope.hasEditPermission) {
                            $scope.isActiveOperate = $scope.selectedBranch.itemData.data.isActive;
                            if ($scope.selectedBranch.itemData.data.isActive) {

                                if ($scope.selectedBranch.itemData.data.isArea) {
                                    $scope.canEdit = true;
                                    $scope.canAdd = true;
                                    $scope.isActiveStr = $translate.instant(isActiveMap[$scope.selectedBranch.itemData.data.isActive]);

                                } else {
                                    $scope.canEdit = false;
                                    $scope.canAdd = false;
                                    $scope.isActiveStr = '';
                                }

                            } else {
                                $scope.canAdd = false;
                                $scope.canEdit = false;
                                $scope.isActiveStr = $translate.instant(isActiveMap[$scope.selectedBranch.itemData.data.isActive]);
                            }
                        }

                        $timeout(refreshMap, 20);
                    }

                };
            };

            var setColor = function (branch, color) {
                branch.data.color = color;
                if (branch.items && branch.items.length) {
                    branch.items.forEach(function (row) {
                        setColor(row, color);
                    });
                }
            };

            var loadProvinceAndCity = function () {
                regionService.getProvinceAndCityTreeList().success(function (data) {

                    // 只取有效的数据
                    //data = _.filter(data, function (num) { return num.isActive });
                    //$scope.provinceList = _.filter(data, function (num) { return num.levelType === 1 });
                    //$scope.allCityList = _.filter(data, function (num) { return num.levelType === 2 });
                    $scope.provinceCityData = data;
                });
            };

            var selectRegion = function (region) {
                $scope.selectedBranch = region;
                $scope.selectedRegion = region.data;
                $scope.selectedRegion.isActiveStr = $translate.instant(isActiveMap[$scope.selectedRegion.isActive]);

                if (!$scope.selectedRegion.isActive) {
                    $scope.canAdd = false;
                } else {
                    if ($scope.selectedRegion.isArea) {
                        $scope.canAdd = true;
                    } else {
                        $scope.canAdd = false;
                    }
                }

                refreshMap();
            };

            var refreshMap = function () {
                $timeout(function () {
                    if ($scope.mychart) {
                        var option = angular.copy($scope.option);

                        var data = [];

                        if ($scope.selectedBranch && $scope.selectedBranch.parent === null) {

                            // 如果是根节点
                            if ($scope.selectedBranch.children && $scope.selectedBranch.children.length > 0) {
                                $scope.selectedBranch.children.forEach(function (row) {
                                    var tempCity = getAreaCityRecursive(row);
                                    var tempData = getMapData(tempCity, row);

                                    if (tempData && tempData.length > 0) {
                                        data = _.union(data, tempData);
                                    }
                                });
                            }

                        } else {
                            var areaCity = getAreaCityRecursive($scope.selectedBranch);

                            var tempData = getMapData(areaCity, $scope.selectedBranch);
                            if (tempData && tempData.length > 0) {
                                data = _.union(data, tempData);
                            }
                        }

                        if (data && data.length > 0) {
                            option.series[0].data = data;
                            $scope.mychart.setOption(option);
                        } else {
                            option.series[0].data = [];
                            $scope.mychart.setOption(option);
                        }
                    }
                }, 0);
            };

            var getMapData = function (areaCity, branch) {

                var data = [];
                if (areaCity && areaCity.length > 0) {

                    var reg = /省$/gi;
                    var cityreg = /市$/gi;
                    areaCity.forEach(function (row) {

                        //will replace with constanct variable
                        if (row === '香港特别行政区') {
                            row = '香港';
                        } else if (row === '澳门特别行政区') {
                            row = '澳门';
                        }

                        var item = {
                            name: row.replace(reg, '').replace(cityreg, ''),
                            value: randomData(),
                            itemStyle: {
                                normal: {
                                    areaColor: branch.itemData.data.color
                                }
                            }
                        };

                        data.push(item);
                    });
                }

                return data;
            };

            var getAreaCityRecursive = function (branch) {
                var citys = [];
                if (!branch || !branch.itemData || !branch.itemData.data) {
                    return citys;
                }

                if (branch.itemData.data.isArea) {
                    // 如果是自定义区域

                    if (branch.children && branch.children.length > 0) {
                        var tempCities = [];
                        branch.children.forEach(function (row) {
                            var temp = getAreaCityRecursive(row);
                            if (temp && temp.length > 0) {
                                tempCities = _.union(tempCities, temp);
                            }
                        });

                        citys = _.union(citys, tempCities);
                    }

                } else {
                    // 如果是行政区域
                    if (branch.children && branch.children.length > 0) {
                        var tempCities = [];

                        tempCities.push(branch.text);
                        citys = _.union(citys, tempCities);
                    } else { }
                }

                return citys;
            };

            var newRegion = function ($event) {

                if (!$scope.canAdd) {
                    return;
                    $event.stopPropagation();
                }

                $scope.editModel = {};
                if ($scope.selectedBranch) {
                    $scope.editModel.mergerName = $scope.selectedBranch.itemData.data.mergerName;
                }

                $scope.editModel.isfirstShow = true;
                $scope.isAdd = true;
                $scope.editModel.RegionNames = [];

                // 如果是选择行政省,则添加输入框中,行政省不能选
                if ($scope.canAdd && $scope.selectedBranch && $scope.selectedBranch.children && $scope.selectedBranch.children.length > 0 && !$scope.selectedRegion.isArea) {
                    $scope.selectProvince = _.find($scope.provinceList, function (num) {
                        return num.id + '' === $scope.selectedBranch.itemData.id
                    });
                    $scope.loadCity();
                } else {
                    $scope.selectProvince = null;
                    $scope.selectCity = null;
                }
                resetErrorStatus();

                $scope.editModel.SelectedCityList = [];

                $('#addRegionPop').modal('show');
                $event.stopPropagation();
            };

            var changeRegionName = function () {
                if (!$scope.editModel.name) {
                    $scope.editModel.name = '';
                }

                $scope.editModel.mergerName = $scope.selectedRegion.mergerName + ',' + $scope.editModel.name;
                $scope.editModel.shortName = $scope.editModel.name;
            };

            var save = function () {

                resetErrorStatus();
                if (!($('#addRegionForm').valid())) {
                    return;
                }

                var selectedCity = $scope.selectedNodeList;
                console.log('selectedCity', selectedCity);

                //var tree = $scope.editModel.MyTree;
                var areaId = $scope.selectedBranch ? $scope.selectedBranch.itemData.id : null;


                var parentId = $scope.selectedBranch ? $scope.selectedBranch.itemData.parentId : null;
                var name = $scope.editModel.areaName;

                //增加区域的情况
                if ($scope.isAdd) {

                    $scope.editModel.IsEdit = false;

                    var saveModel = {
                        parentId: areaId,
                        name: name,
                        cityList: selectedCity
                    };

                    console.log(saveModel);

                    areaRegionService.add(saveModel).success(function (data) {
                        if (!data.result) {
                            swal($translate.instant('SaveFail'), $translate.instant(data.resultMsg), 'warning');
                            return;
                        }

                        SweetAlert.success($translate.instant('SaveSuccess'));
                        refreshAreaRegionTree();

                        $('#addRegionPop').modal('hide');
                    });
                } else { //编辑区域的情况

                    var saveModel = {
                        id: areaId,
                        parentId: $scope.selectedBranch.itemData.parentId,
                        name: name,
                        cityList: selectedCity,
                        isActive: true
                    };

                    areaRegionService.update(saveModel).success(function (data) {
                        if (!data.result) {
                            swal($translate.instant('SaveFail'), $translate.instant(data.resultMsg), 'warning');
                            return;
                        }

                        SweetAlert.success($translate.instant('SaveSuccess'));
                        refreshAreaRegionTree();
                        $('#addRegionPop').modal('hide');
                    });
                }
            };

            var addCityToMap = function (model) {
                if (model.isArea) {
                    return;
                }

                if ($scope.mychart) {
                    var option = $scope.option;

                    var areaCity = [];
                    if (model.provinceRegionID && model.cityRegionID) {
                        // 指定市
                        areaCity.push(model.cityName);
                    } else {
                        // 某一个省的所有市
                        var cityNameList = _.pluck($scope.cityList, 'name');
                        cityNameList = _.filter(cityNameList, function (num) {
                            return num !== ''
                        });
                        areaCity = cityNameList;
                    }

                    var item = getMapData(areaCity, $scope.selectedBranch);
                    if (!option.series[0].data || option.series[0].data.length === 0) {
                        option.series[0].data = [];
                    }

                    // 两个数组union起来
                    option.series[0].data = _.union(option.series[0].data, item);

                    $timeout(function () {
                        $scope.mychart.setOption(option);
                    }, 100);
                }
            };

            var selectedCityList = [];
            var getCity = function (item) {

                if (item.children.length == 0) {
                    selectedCityList.push(item);
                    return;
                }

                item.children.forEach(function (itm) {
                    getCity(itm);
                });
            };

            var edit = function ($event) {
                $scope.isAdd = false;
                $scope.editModel = $scope.selectedRegion;
                selectedCityList = [];

                var selectItem = $scope.selectedBranch;
                var selectedProvinceCityInfo = getProvinceAndCityList(selectItem);

                $scope.editModel.SelectedCityList = selectedProvinceCityInfo;

                $scope.editModel.IsEdit = true;
                $scope.selectRegionType = $scope.regionTypeData[1];
                $scope.selectProvince = null;
                $scope.selectCity = null;
                $scope.editModel.areaName = $scope.selectedBranch.text;
                resetErrorStatus();
                $('#addRegionPop').modal('show');
                $event.stopPropagation();
            };

            var cancel = function () {

                if ($scope.selectedRegion.id) {
                    $scope.isEdit = false;
                    $scope.selectedRegion.isEditRegionItem = false;
                } else {
                    $scope.isAdd = false;
                    $scope.isEdit = false;
                    $scope.selectedRegion.parentNode.subRegionList = _.filter($scope.selectedRegion.parentNode.subRegionList, function (num) {
                        return num.id
                    });
                }
            };

            var regionItemDbClick = function (model) {

                if (!$scope.isEdit) {
                    $scope.selectedRegion = model;
                    $scope.selectedRegion.pinYin = '';
                    $scope.selectedRegion.isEditRegionItem = true;
                    $scope.selectedRegion.editName = $scope.selectedRegion.name;
                    $scope.isEdit = true;
                }
            };


            var resetErrorStatus = function () {
                var currentForm = $('#addRegionForm');
                $.each(currentForm.children(), function (index, element) {
                    // $(element).find('.has-error').removeClass('has-error');
                    $(element).find('.has-error label').remove();
                });
                validator.resetForm();
            };

            // 启用禁用
            var updateIsActive = function ($event) {
                var updateModel = {};
                updateModel.isActive = !$scope.selectedBranch.itemData.data.isActive;
                updateModel.id = $scope.selectedBranch.itemData.id;
                updateModel.text = $scope.selectedBranch.text;


                SweetAlert.swal({
                    title: $translate.instant('Confirm') + $scope.isActiveStr + '?',
                    text: $translate.instant('ComfirmRegionIsActive').formatObj({
                        isActiveStr: $scope.isActiveStr,
                        regionName: updateModel.text
                    }),
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: $translate.instant('Confirm'),
                    cancelButtonText: $translate.instant('Cancel'),
                    closeOnConfirm: false,
                    closeOnCancel: true
                },
                    function (isConfirm) {
                        if (isConfirm) {

                            areaService.isactive(updateModel).success(function (data) {
                                if (!data.result) {

                                    if (data.resultMsg) {
                                        swal($translate.instant('SaveFail'), $translate.instant(data.resultMsg), 'warning');
                                    } else {
                                        var errorList = [];
                                        var template = $translate.instant(data.resultMsg);

                                        var orgNameList = data.data.join(',');
                                        swal($translate.instant('SaveFail'), template.formatObj({
                                            orgNameList: orgNameList
                                        }), 'warning');
                                    }

                                    updateModel.isActive = !$scope.selectedRegion.isActive;
                                    return;
                                };

                                SweetAlert.success($translate.instant('SaveSuccess'));
                                refreshAreaRegionTree();
                                $event.stopPropagation();
                            });
                        }
                    });

            };

            var resources = {
                provinceRequired: $translate.instant('ProvinceRequired'),
                areaNameRequired: $translate.instant('AreaNameRequired')
            };

            var validator = $("#addRegionForm").validate({
                errorClass: "has-error",
                rules: {
                    province: {
                        required: true
                    },
                    areaName: {
                        required: true,
                        maxlength: 50
                    }
                },
                messages: {
                    province: {
                        required: resources.provinceRequired
                    },
                    areaName: {
                        required: resources.areaNameRequired,
                        maxlength: $translate.instant('AreaNameOutOfLengthNode'),
                    }
                },
                errorPlacement: function (error, element) {
                    setErrorStyle(error, element);
                }
            });

            var setErrorStyle = function (error, element) {
                if (element.hasClass('has-error')) {
                    element.parent().addClass('has-error');
                    error.insertAfter(element);
                    error.addClass('label');
                    // error.addClass('label-danger');
                }
            };

            var setCollapsedID = function (model) {
                if (model.parentNode) {
                    $scope.collapsedIDList.push(model.parentNode.id);
                    setCollapsedID(model.parentNode);
                }
            };

            var setParentNodeList = function (model) {
                if (model.subRegionList && model.subRegionList.length > 0) {
                    model.subRegionList.forEach(function (row) {
                        row.parentNode = model;
                        setParentNodeList(row);
                    });
                }
            };

            // id:主键ID
            // node:对应节点
            // model:添加的时候的数据
            var reloadById = function (id, node, model) {

                regionService.getAreRegionTreeByNodeID(id).success(function (data) {
                    if (data && data.length > 0) {
                        var oldData = node.data;
                        node.children = angular.copy(data[0].children);
                        node.label = data[0].label;
                        node.data = angular.copy(data[0].data);
                        node.data.isRoot = oldData.isRoot;
                        if (!oldData.color) {
                            oldData.color = colorPanelInstance.getNextColor();
                        }
                        setColor(node, oldData.color);
                        $scope.selectedRegion.isActiveStr = $translate.instant(isActiveMap[$scope.selectedRegion.isActive]);

                        selectRegion(node);
                        //if (model) {
                        //    addCityToMap(model);
                        //}
                    }
                });
            };

            var randomData = function () {
                return Math.round(Math.random() * 1000);
            };

            $scope.drawChinaMap = function () {
                //var mychart = echarts.init(document.getElementById('chinaMap'));

                $.get('/app-resources/map/china.json', function (chinaJson) {
                    $scope.chinaJson = chinaJson;

                    echarts.registerMap('china', chinaJson);
                    var mychart = echarts.init(document.getElementById('chinaMap'));
                    var option = {
                        //visualMap: {
                        //    min: 0,
                        //    max: 2500,
                        //    left: 'left',
                        //    top: 'bottom',
                        //    text: ['高', '低'],           // 文本,默认为数值文本
                        //    show: false,
                        //},
                        legend: {
                            orient: 'vertical',
                            left: 'left',
                            data: []
                        },
                        series: [{
                            type: 'map',
                            map: 'china',
                            nameMap: {
                                'China': '中国'
                            },
                            label: {
                                normal: {
                                    show: true,
                                    textStyle: {

                                        // #c12e34, 改label字体的颜色
                                        color: '#894A00'
                                    }
                                },
                                emphasis: {
                                    //textStyle: {
                                    //    color: '#fff'
                                    //}
                                }
                            },
                            itemStyle: {
                                normal: {
                                    areaColor: '#C9C9C9',
                                    borderColor: 'white',
                                    borderWidth: 0.5,
                                    label: {
                                        show: false
                                    }
                                },
                                emphasis: {
                                    areaColor: '#F2D997',
                                    label: {
                                        show: true
                                    }
                                }
                            },
                            scaleLimit: {
                                min: 1
                            },
                            selectedMode: false,
                            roam: true,
                            data: []
                        }]
                    };

                    $scope.option = option;
                    mychart.setOption(option);


                    //mychart.on('click', function (parmas) {

                    //});

                    //mychart.dispatchAction({
                    //    type: 'highlight',
                    //    // 可选,系列 index,可以是一个数组指定多个系列
                    //    seriesIndex: 0,
                    //    // 可选,系列名称,可以是一个数组指定多个系列
                    //    dataIndex: 1,
                    //    // 可选,数据的 名称
                    //});

                    //mychart.dispatchAction({
                    //    type: 'highlight',
                    //    // 可选,系列 index,可以是一个数组指定多个系列
                    //    seriesIndex: 0,
                    //    // 可选,系列名称,可以是一个数组指定多个系列
                    //    dataIndex: 29,
                    //    // 可选,数据的 名称
                    //});

                    //mychart.dispatchAction({
                    //    type: 'highlight',
                    //    // 可选,系列 index,可以是一个数组指定多个系列
                    //    seriesIndex: 0,
                    //    // 可选,系列名称,可以是一个数组指定多个系列
                    //    dataIndex: 28
                    //    // 可选,数据的 名称
                    //});

                    mychart.on('click', function (parmas) {

                    });

                    $scope.mychart = mychart;
                });



                //mychart.setOption(option);
            };

            $scope.changeRegionType = function () {
                if ($scope.selectRegionType.id == 0) {

                    $scope.editModel.areaName = '';
                } else {
                    $scope.selectProvince = null;
                    $scope.selectCity = null;
                }
            };

            $scope.loadCity = function () {
                $scope.cityList = _.filter($scope.allCityList, function (num) {
925
                    return num.parentID === $scope.selectProvince.id
eddie.woo's avatar
eddie.woo committed
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
                });

                if ($scope.cityList != null && $scope.cityList.length > 0) {
                    // 加上默认的城市
                    $scope.cityList.splice(0, 0, $scope.defaultCity);
                } else {
                    $scope.cityList.push($scope.defaultCity);
                }

                $scope.selectCity = $scope.defaultCity;
            };


            var havePermission = function () {
                $scope.$root.checkUserPermission(constant.adminPermission.basicData.areaManage.editCode).success(function (data) {
                    $scope.hasEditPermission = data;
                    initParam();
                });
            };


            var initParam = function () {
                $scope.isActiveStr = '';
                $scope.isAdd = true;
                $scope.isEdit = false;
                $scope.canEdit = false;

                if ($scope.hasEditPermission) {
                    $scope.canAdd = true;
                } else {
                    $scope.canAdd = false;
                }
            };

            var refreshAreaRegionTree = function () {
                dataSource.reload()
                //$scope.gridInstance.repaint();
            };

            (function initialize() {

                $log.debug('RegionManageController.ctor()...');
                $scope.regionList = [];

                $scope.selectedRegion = {};
                $scope.selectedRegion.isActiveStr = $translate.instant(isActiveMap[false]);;

                $scope.selectRegion = selectRegion;
                $scope.newRegion = newRegion;

                // $scope.newRegionNode = newRegionNode;
                $scope.changeRegionName = changeRegionName;

                $scope.save = save;
                $scope.edit = edit;
                $scope.updateIsActive = updateIsActive;

                $scope.hasEditPermission = false;
                havePermission();
                //loadAreaTree();
                loadRegionTree();
                $scope.drawChinaMap();
                //loadProvinceAndCity();
            })();
        }
    ]);