index.js 58.2 KB
1 2 3 4 5 6 7 8 9 10 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 925 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 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
require.config({
    paths: {
        'columntoggle': '../addons/facrm/js/columntoggle',
    }
});
define(['jquery', 'bootstrap', 'backend', 'table', 'form','columntoggle'], function ($, undefined, Backend, Table, Form,Columntoggle) {
//本地缓存定义
    var Controller = {
        index: function () {
            var scene_id="";
            var toggle_show_key="commonSearch";
            var toggle_show=localStorage.getItem(toggle_show_key)==0?false:true;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/customer/index/index',
                    add_url: 'facrm/customer/index/add',
                    edit_url: 'facrm/customer/index/edit',
                    del_url: 'facrm/customer/index/del',
                    multi_url: '',
                    table: '',
                }
            });
            var table = $("#customer");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                if (from_type == "need") {
                    //如果是需要跟进的客户
                    $(".commonsearch-table").removeClass("hidden");
                    $("input[name='next_time']").val(Moment().year(Moment().year() - 10).startOf('year').format('YYYY-MM-DD H:mm:ss') + ' - ' + Moment().endOf('day').format('YYYY-MM-DD H:mm:ss'));
                } else if (from_type == "expire") {
                    $(".commonsearch-table select[name=expire_type]").val(1);
                    $(".commonsearch-table").removeClass("hidden");
                }
                $(".commonsearch-table select[name=scene_id]").val(1);
            });
            var deal_status={1: __('已成交'), 0: __('未成交')};
            var expire_type={'1': "将要过期客户"};
            var columnss = [
                {checkbox: true},
                {
                    field: 'scene_id',
                    title: __('客户场景'),
                    visible: false,
                    formatter: function (v, r) {
                        return scene_list[v];
                    }, searchList: scene_list
                },
                {
                    field: 'city', title: __('城市检索'), searchList: function (column) {
                        return Template('categorytpl', {});
                    }, formatter: function (value, row, index) {
                        return '无';
                    }, visible: false
                },
                {
                    field: 'expire_type',
                    title: __('将要过期'),
                    visible: false,
                    searchList: {'1': "将要过期客户"}
                },

                {
                    field: 'operate',
                    title: __('Operate'),
                    table: table,
                    width: "180px",
                    events: Table.api.events.operate,
                    formatter: Table.api.formatter.operate,
                    buttons: [
                        {
                            name: 'record',
                            text: '跟进',
                            title: function ( row) {
                                return '跟进客户-'+row.name;
                            },
                            icon: 'fa fa-commenting-o',
                            classname: 'btn btn-xs btn-danger btn-addtabs',
                            url: 'facrm/customer/record/add',
                        },
                        {
                            name: 'discard',
                            text: '公海',
                            title: '公海',
                            url: 'facrm/customer/index/discard',
                            confirm: "确认把该客户放入公海?",
                            icon: 'fa fa-close',
                            classname: 'btn btn-xs btn-danger  btn-ajax',
                            success: function (data, ret) {
                                $("a.btn-refresh").trigger("click");
                            },
                            hidden:function (row) {
                                if ($.inArray(scene_id,[13,14])!=-1) return true;
                                return  false;
                            }

                        },
                        {
                            name: 'del',
                            url:"facrm/customer/index/del",
                            confirm:"确认把该客户删除?",
                            icon: 'fa fa-trash',
                            classname: 'btn btn-xs btn-danger  btn-ajax',
                            success: function (data, ret) {
                                $("a.btn-refresh").trigger("click");
                            },
                            hidden:function (row) {
                                if ($.inArray(scene_id,[13,14])!=-1) return true;
                                return  false;
                            }
                        },

                        {
                            name: 'share',
                            title: '分享客户',
                            url:"facrm/customer/index/share",
                            icon: 'fa fa-share-square-o',
                            classname: 'btn btn-xs btn-success  btn-dialog',
                            success: function (data, ret) {
                                $("a.btn-refresh").trigger("click");
                            },
                            hidden:function (row) {
                                if ($.inArray(scene_id,[2,3])==-1) return true;
                                return  false;
                            }
                        },

                        {
                            name: 'sdel',
                            url: function (row) {
                                var stype="shared";//被分享
                                if ($.inArray(scene_id,[13])!=-1) {
                                    stype="share";//分享
                                }
                                return 'facrm/customer/index/sdel/ids/'+row.id+'/stype/'+stype;
                            },
                            confirm:"确认把该客户从分享删除?",
                            icon: 'fa fa-close',
                            classname: 'btn btn-xs btn-danger  btn-ajax',
                            success: function (data, ret) {
                                $("a.btn-refresh").trigger("click");
                            },
                            hidden:function (row) {
                                if ($.inArray(scene_id,[13,14])!=-1) return false;
                                return  true;
                            }
                        },
                    ]
                },

            ];

            if (Config.fields && Object.keys(Config.fields).length > 0) {
                var fields = JSON.parse(Config.fields);
                var start = columnss.length-1;
                for (var i = 0; i < fields.length; i++) {
                    if (fields[i].hasOwnProperty('formatter'))
                        fields[i].formatter = eval(fields[i].formatter);
                    if (fields[i].hasOwnProperty('events'))
                        fields[i].events = eval(fields[i].events);
                    if (fields[i].hasOwnProperty('searchList'))
                        fields[i].searchList = eval(fields[i].searchList);
                    if (fields[i].hasOwnProperty('cellStyle'))
                        fields[i].cellStyle = eval(fields[i].cellStyle);

                    columnss.splice(start + i, 0, fields[i]);
                }
            }
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    columnss
                ],
                onLoadSuccess: function () {
                    // 载入成功时执行
                    loadSuccess("customer")
                },
                onColumnSwitch: function () {
                    // 切换列时执行columnSwitch方法
                    columnSwitch("customer","customer_table")
                },
                showExport: Config.export,//工具栏上显示导出按钮
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                searchFormVisible:toggle_show, //打开搜索
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var province= $(".form-commonsearch select[name='province']").val();
                    if (province){
                        filter.province =province;
                        op.province = "=";
                        params.filter = JSON.stringify(filter);
                        params.op = JSON.stringify(op);
                    }

                    return params;
                },
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            // 绑定TAB事件
            $('.panel-heading ul[data-field] li a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                $(".commonsearch-table input[name=" + field + "]").val(value);
                scene_id=value;
                return false;
            });
            // 批量放入公海事件
            $("#toolbar").on('click', '.btn-discard', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                Layer.confirm(
                    __('确定放入公海 %s 项?', ids.length),
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                    function (index) {
                        Table.api.multi("del1", ids, table, that);
                        Layer.close(index);
                    }
                );
            });

            // 批量删除事件
            $("#toolbar").on('click', '.btn-dels', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                Layer.confirm(
                    __('确定删除 %s 项?', ids.length),
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                    function (index) {
                        Table.api.multi("del1", ids, table, that);
                        Layer.close(index);
                    }
                );
            });

            // 批量转移
            $("#toolbar").on('click', '.btn-divert', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                if (ids.length <=0) {
                    return;
                }
                var title = $(that).data('title') || $(that).attr("title") || __('Edit');
                var url = $(that).data('url');
                var data = $(that).data() || {};
                delete data.title;
                Fast.api.open(url + "/ids/" + ids, title, data);
            });

            //批量发邮件
            $(".btn-send-emails").click(function () {
                var that = this;
                var title = $(that).data('title') || $(that).attr("title") || __('批量发邮件');
                var url = $(that).data('url');
                var data = $(that).data() || {};
                delete data.title,data.url;
                var ids = Table.api.selectedids(table);
                if (ids.length <=0) {
                    return;
                }
                data.typesid=ids;
                parent.Fast.api.open(url+"?data="+encodeURI(JSON.stringify(data)), title, data);
            });
			// 合并
            $("#toolbar").on('click', '.btn-merge', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                if (ids.length <=0) {
                    return;
                }
                var title = $(that).data('title') || $(that).attr("title") || __('Edit');
                var url = $(that).data('url');
                var data = $(that).data() || {};
                delete data.title;
                Fast.api.open(url + "/ids/" + ids, title, data);
            });

            $("button[name='commonSearch']").bind("click",function(){
                localStorage.setItem(toggle_show_key,(toggle_show?0:1));
            });

        },

        reduplicate:function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/customer/index/reduplicate',
                    add_url: 'facrm/customer/index/add',
                    edit_url: '',
                    del_url: '',
                    multi_url: '',
                    table: '',
                }
            });
            var table = $("#customer");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
            });
            var columnss = [
                {checkbox: true},
                {field: 'id', title: 'ID'},

                {
                    field: 'name',
                    title: __('客户名称'),
                    operate: "LIKE",
                    align: 'left',
                    customField: 'tags',
                    formatter: function (value, row, index) {
                        return value +
                            '<div >' + Table.api.formatter.flag.call(this, row['tags'], row, index) + '</div>';
                    }
                },
                {
                    field: 'mobile',
                    title: __('手机'),
                    operate: "LIKE",
                    align: 'left'
                },
                {
                    field: 'tags',
                    title: __('标签'),
                    operate: 'find_in_set',
                    visible: false,
                    formatter: Table.api.formatter.flag
                },

                {field: 'create_user.nickname', title: __('创建人'), operate: false, search: false},
                {field: 'create_user_id', title: __('创建人'), visible: false,addclass: 'selectpage',
                    extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                {field: 'owner_user.nickname', title: __('负责人'), operate: false, search: false},
                {field: 'owner_user_id', title: __('负责人'), visible: false,addclass: 'selectpage',
                    extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},

                {
                    field: 'create_time',
                    title: __('创建时间'),
                    formatter: Table.api.formatter.datetime,
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    sortable: true,
					extend: 'autocomplete="off"'
                },
                {
                    field: 'follow_time',
                    title: __('跟进时间'),
                    formatter: Table.api.formatter.datetime,
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    sortable: true,
					extend: 'autocomplete="off"'
                },


                {
                    field: 'deal_status',
                    title: __('Status'),
                    formatter: Table.api.formatter.status,
                    searchList: {1: __('已成交'), 0: __('未成交')}
                }

            ];

        
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
				pagination: false,
                columns: [
                    columnss
                ],
                onLoadSuccess: function () {
                    // 载入成功时执行
                    loadSuccess("customer")
                },
                onColumnSwitch: function () {
                    // 切换列时执行columnSwitch方法
                    columnSwitch("customer","customer_table")
                },
                searchFormVisible:true, //打开搜索
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                searchFormVisible:true, //打开搜索
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 绑定TAB事件
            $('.panel-heading ul[data-field] li a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                $(".commonsearch-table input[name=" + field + "]").val(value);
                return false;
            });


        },

        add: function () {
            Controller.api.bindevent();
        },
        divert: function () {
            Controller.api.bindevent();
        },
		merge: function () {
            Controller.api.bindevent();
            $(document).on('click', ".btn-submit", function () {
               var index= Layer.confirm(
                    __('您确定要合并吗?数据不可撤回!请谨慎使用!'),
                    {icon: 3, title: __('Warning'), shadeClose: true},
                    function (index) {
                        Form.api.submit($("form[role=form]"),function () {
                            parent.$("#customer").bootstrapTable("refresh");
                            parent.Layer.closeAll();
                            return true;
                        },function () {
                            index && Layer.close(index);
                        });
                    }
                );

            });
        },
        edit: function () {
            Controller.api.bindevent();
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/customer/record/index/customer_id/' + customer_id,
                    contact_url: 'facrm/customer/contacts/index/customer_id/' + customer_id,
                    business_url: 'facrm/business/index/index/customer_id/' + customer_id,
                    contract_url: 'facrm/customer/index/contractlist/customer_id/' + customer_id,
                    receivables_url: 'facrm/customer/index/receivableslist/customer_id/' + customer_id,
                    files_url: 'facrm/customer/record/files/customer_id/' + customer_id,
                }
            });
            var table = $("#tableflow");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
           
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar1',
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'content', title: __('content'), operate: 'LIKE'},
                        {field: 'create_user.nickname', title: __('创建人'), operate: false, search: false},
                        {field: 'create_user_id', title: __('创建人'), visible: false,addclass: 'selectpage',
							extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'next_time',
                            title: __('下次跟进'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'record_type', title: __('跟进类型'), formatter: function (v, r) {
                                return record_type_list[v];
                            }, searchList: record_type_list
                        },

                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            //联系人
            var contact = $("#contact");
            contact.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("#forum select[name=scene_id]").val(4);
            });
            contact.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.contact_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar2',
                columns: [
                    [
                        {
                            field: 'operate', title: __('Operate'), table: contact,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'edit1',
                                    text: '',
                                    title: '修改联系人',
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-success  btn-dialog',
                                    url: 'facrm/customer/contacts/edit',

                                },
                                {
                                    name: 'del1',
                                    text: '',
                                    title: '删除',
                                    confirm: "确认删除联系人?",
                                    icon: 'fa fa-trash',
                                    classname: 'btn btn-xs btn-danger  btn-ajax',
                                    url: 'facrm/customer/contacts/del',
                                    success: function (data, ret) {
                                        $("a.btn-refresh").trigger("click");
                                    },
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        },
                        {field: 'scene_id', title: __('场景'), visible: false, searchList: {4: "全部"}},
                        {field: 'name', title: __('姓名')},
                        {field: 'mobile', title: __('手机'), operate: 'LIKE', formatter: function (value, row, index) {
                                if (!value) return '-';
                                return '<a href="javascript:;" class="btn btn-success cloudcall btn-xs" data-field="mobile" data-typeid="'+
                                    row.id+'" data-type="customer_contacts" data-name="'+row.name+'"><i class="fa fa-phone"></i> ' +value + '</a>';
                            }},
                        {field: 'telephone', title: __('电话'), operate: 'LIKE', formatter: function (value, row, index) {
                                if (!value) return '-';
                                return '<a href="javascript:;" class="btn btn-success cloudcall btn-xs" data-field="telephone" data-typeid="'+
                                    row.id+'" data-type="customer_contacts" data-name="'+row.name+'"><i class="fa fa-phone"></i> ' +value + '</a>';
                            }},
                        {field: 'email', title: __('邮箱')},
                        {field: 'wechat', title: __('微信')},
                        {field: 'post', title: __('职务')},
                        {field: 'sex', title: __('性别'), searchList: {'-1':"未知",0:"女",1:"男"},formatter: Table.api.formatter.status}, 
                        {field: 'decision', title: __('决策人'), searchList: {'-1':"未知",0:"不是",1:"是"},formatter: Table.api.formatter.status},
                        {field: 'detail_address', title: __('地址')},
                        {field: 'remark', title: __('备注')},
                        {
                            field: 'next_time',
                            title: __('下次联系'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        }


                    ]
                ],
            });
            // 初始化表格
            contact.bootstrapTable();
            Table.api.bindevent(contact);//当内容渲染完成后

            //商机
            var business = $("#t-business");
            business.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("#business select[name=scene_id]").val(7);
            });

            business.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.business_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar-business',
                columns: [
                    [

                        {field: 'scene_id', title: __('场景'), visible: false, searchList: {7: "全部"}},
                        {
                            field: 'operate', title: __('Operate'), table: business,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'record',
                                    text: '跟进',
                                    title: '跟进',
                                    icon: 'fa fa-commenting-o',
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    url: 'facrm/business/record/add',
                                },
                                {
                                    name: 'edit1',
                                    text: '',
                                    title: '修改商机',
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-success  btn-dialog',
                                    url: 'facrm/business/index/edit',

                                }
                            ],
                            formatter: Table.api.formatter.operate
                        },
                        {
                    field: 'name',
                    title: __('商机名称'),
                    operate: "LIKE",
                    align: 'left',
                    formatter: function (value, row, index) {
                        value='<a href="javascript:void(0);" data-url="facrm/business/index/edit/ids/' + row.id
                            +'" data-area="[&quot;98%&quot;,&quot;98%&quot;]" class="btn-dialog" data-title="'+ row.name+'">'+ row.name+'</a>';
                        return value;
                    }},
                        {field: 'money', title: __('客户预算'), operate: 'BETWEEN', sortable: true},
                        {field: 'total_price', title: __('产品价格'), operate: 'BETWEEN', sortable: true},
                        {field: 'remark', title: __('备注'), operate: 'LIKE'},
                        {
                            field: 'deal_time',
                            title: __('预计成交'),
                            formatter: Table.api.formatter.datetime,
                            datetimeFormat: "YYYY-MM-DD",
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'next_time',
                            title: __('下次跟进'),
                            formatter: Table.api.formatter.datetime,
                            datetimeFormat: "YYYY-MM-DD",
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {field: 'create_user.nickname', title: __('创建人'), operate: false, search: false},
                        {field: 'create_user_id', title: __('创建人'), visible: false,addclass: 'selectpage',
								 extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                        {
                            field: 'update_time',
                            title: __('更新时间'),
                            formatter: Table.api.formatter.datetime,
                            datetimeFormat: "YYYY-MM-DD",
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'is_end',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {1: __('已成交'), 0: __('未成交'), 2: __('失败'), 3: __('无效')}

                        },

                    ]
                ],
            });
            // 初始化表格
            business.bootstrapTable();
            Table.api.bindevent(business);//当内容渲染完成后

            //合同
            var contract = $("#t-contract");
            contract.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.contract_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar-contract',
                columns: [
                    [
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: contract,
                            buttons: [
                                {
                                    name: 'payurl',
                                    text: '',
                                    title: __('线上续费收款码'),
                                    icon: 'fa fa-qrcode',
                                    classname: 'btn btn-xs btn-danger  btn-dialog',
                                    url: 'facrm/contract/index/payurl',
                                    hidden:function (row) {
                                        if (row.check_status == undefined||row.check_status!=2){
                                            return true;
                                        }
                                        if (!Config.payConfig||Config.payConfig.online_pay!='1'){
                                            return true;
                                        }
                                        //判断是否逾期
                                        var newtime = Math.round(new Date().getTime()/1000).toString();
                                        if (newtime<(row.end_time-(Config.payConfig.renew_day*86400))){
                                            return true;
                                        }
                                    },
                                    success:function () {
                                        $(".btn-refresh").trigger('click');
                                    }
                                },
                                {
                                    name: 'receivables',
                                    text: '详情',
                                    title: __('合同详情'),
                                    icon: '',
                                    classname: 'btn btn-info btn-dialog btn-xs',
                                    url: function(row){
                                        return 'facrm/customer/index/contract/customer_id/'+row.customer_id+'/ids/'+row.id;
                                    },
                                },
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                        },
                        {field: 'number', title: __('合同编号'), operate: "LIKE"},
                        {field: 'name', title: __('合同名称'), operate: "LIKE"},
                        {field: 'money', title: __('合同金额'), operate: 'BETWEEN', sortable: true},
                        {field: 'remark', title: __('备注'), operate: 'LIKE'},
                        {
                            field: 'start_time',
                            title: __('开始时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            datetimeFormat: "YYYY-MM-DD",
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'end_time',
                            title: __('结束时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            datetimeFormat: "YYYY-MM-DD",
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {field: 'order_admin.nickname', title: __('签约人'), operate: false, search: false},
                        {field: 'order_admin_id', title: __('签约人'), visible: false},
                        {
                            field: 'check_status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {0: __('待审核'), 1: __('审核中'), 2: __('审核通过'), 3: __('审核未通过')}
                        },
                    ]
                ],
            });
            // 初始化表格
            contract.bootstrapTable();
            Table.api.bindevent(contract);//当内容渲染完成后

            //回款
            var receivables = $("#t-receivables");
            receivables.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.receivables_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar-receivables',
                columns: [
                    [
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: receivables,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [

                                {
                                    name: 'payurl',
                                    text: '',
                                    title: __('线上收款码'),
                                    icon: 'fa fa-qrcode',
                                    classname: 'btn btn-success btn-dialog btn-xs',
                                    url: 'facrm/contract/receivables/payurl',
                                    hidden:function (row) {
                                        if (row.pay_type == 1){
                                            return true;
                                        }
                                        if (row.pay_status == undefined||row.pay_status==1){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'receivables',
                                    text: '详情',
                                    title: __('回款详情'),
                                    icon: '',
                                    classname: 'btn btn-info btn-dialog btn-xs',
                                    url: function(row){
                                        return 'facrm/customer/index/receivables/customer_id/'+row.customer_id+'/ids/'+row.id;
                                    },
                                },

                            ],
                        },
                        {field: 'number', title: __('回款编号'), operate: "LIKE"},
                        {field: 'contract.number', title: __('合同编号'), operate: "LIKE"},
                        {field: 'contract.name', title: __('合同名称'), operate: "LIKE"},
                        {field: 'money', title: __('回款金额'), operate: 'BETWEEN', sortable: true},
                        {field: 'remark', title: __('备注'), operate: 'LIKE'},
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'check_status',
                            title: __('Status'),
                            formatter: function (value, row, index) {
                                var check_status={0: __('待审核'), 1: __('审核中'), 2: __('审核通过'),3: __('审核未通过')};
                                var pay_status={1:'已付款',0:'未付款'};
                                var retrunstr='';
                                if(row['pay_type']==2){
                                    retrunstr='(在线'+pay_status[row['pay_status']]+')';
                                }else if(row['pay_type']==3){
                                    retrunstr='(续费)';
                                }
                                return check_status[row['check_status']]+retrunstr;
                            }

                        },

                    ]
                ],
            });
            // 初始化表格
            receivables.bootstrapTable();
            Table.api.bindevent(receivables);//当内容渲染完成后

            //附件
            var files = $("#t-files");
            files.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.files_url,
                pk: 'id',
                sortName: 'id',
                toolbar: '#toolbar-files',
                //禁用默认搜索
                search: false,
                //启用普通表单搜索
                commonSearch: false,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false,
                //分页大小
                pageSize: 1,
                showColumns: false,
                showToggle: false,
                showExport: false,
                showSearch: false,
                pagination: false,
                columns: [
                    [
                        {field: 'url', title: __('预览'), formatter: Controller.api.formatter.thumb, operate: false},
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },

                    ]
                ],
            });


        },
        detail: function () {
            Controller.edit();
        },
        common: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/customer/index/common',
                    multi_url: '',
                    table: '',
                }
            });

            var table = $("#customer_common");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
               
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('客户名称'), operate: "LIKE",   align: 'left'},
                        {
                            field: 'level', title: __('客户级别'), formatter: function (v, r) {
                                return levelList[v];
                            }, searchList: levelList
                        },
                        {
                            field: 'industry', title: __('行业'), formatter: function (v, r) {
                                return industryList[v];
                            }, searchList: industryList
                        },
                        {
                            field: 'source', title: __('来源'), formatter: function (v, r) {
                                return sourceList[v];
                            }, searchList: sourceList
                        },
                        {field: 'telephone', title: __('电话'), operate: 'LIKE'},
                        {field: 'mobile', title: __('手机'), operate: 'LIKE'},

                        {field: 'remark', title: __('备注'), operate: 'LIKE'},
                        {field: 'create_user.nickname', title: __('创建人'), operate: false, search: false},
                        {field: 'create_user_id', title: __('创建人'), visible: false,addclass: 'selectpage',
							extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                        {
                            field: 'next_time',
                            title: __('下次跟进'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'create_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'update_time',
                            title: __('更新时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
							extend: 'autocomplete="off"'
                        },
                        {
                            field: 'deal_status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {1: __('已成交'), 0: __('未成交')}
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'receive',
                                    text: '领取',
                                    title: '领取',
                                    icon: 'fa fa-check',
                                    url: 'facrm/customer/index/receive',
                                    classname: 'btn btn-xs btn-success  btn-ajax',
                                    success: function (data, ret) {
                                        $("a.btn-refresh").trigger("click");
                                    },
                                },
                            ]
                        },

                    ]
                ],
                onLoadSuccess: function () {
                    // 载入成功时执行
                    loadSuccess("customer_common")
                },
                onColumnSwitch: function () {
                    // 切换列时执行columnSwitch方法
                    columnSwitch("customer_common","customer_common_table")
                },
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                searchFormVisible:true, //打开搜索
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 批量领取事件
            $("#toolbar").on('click', '.btn-receive', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                Layer.confirm(
                    __('确定领取 %s 项?', ids.length),
                    {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                    function (index) {
                        Table.api.multi("del1", ids, table, that);
                        Layer.close(index);
                    }
                );
            });
            // 批量转移
            $("#toolbar").on('click', '.btn-divert', function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                if (ids.length > 10) {
                    return;
                }
                var title = $(that).data('title') || $(that).attr("title") || __('Edit');
                var url = $(that).data('url');
                var data = $(that).data() || {};
                delete data.title;
                //循环弹出多个编辑框
                Fast.api.open(url + "/ids/" + ids, title, data);


            });

        },
        import: function () {
            Form.api.bindevent($("form[role=form]"),function (data) {
                if (data.errpath){
                    window.open(data.errpath)
                }
            });
        },
        //合同详情
        contract:function(){
            Form.api.bindevent($("form[role=form]"));
            if ("undefined" == typeof contract_id) {
                contract_id = 0;
            }
            Fast.api.ajax({
                type: 'GET',
                url: 'facrm/fields/get_fields_html',
                data: {source: 'contract', 'id': contract_id}
            }, function (data) {
                $("#extend").html(data.html);
                Form.api.bindevent($("#extend"));
                return false;
            },function () {
                return false;
            });
        },

        //回款详情
        receivables:function(){
            Form.api.bindevent($("form[role=form]"));
            if ("undefined" == typeof ids) {
                ids = 0;
            }
            Fast.api.ajax({
                type: 'GET',
                url: 'facrm/fields/get_fields_html',
                data: {source: 'contract_receivables', 'id': ids}
            }, function (data) {
                $("#extend").html(data.html);
                Form.api.bindevent($("#extend"));
                return false;
            },function () {
                return false;
            });
        },

        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/customer/index/recyclebin',
                }
            });

            var table = $("#table");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $(".commonsearch-table select[name=scene_id]").val(1);
            });
            var columnss = [
                {checkbox: true},
                {field: 'id', title: 'ID'},
                {field: 'name', title: __('客户名称'), operate: "LIKE"},

                {field: 'telephone', title: __('电话'), operate: 'LIKE'},
                {field: 'mobile', title: __('手机'), operate: 'LIKE'},

                {field: 'create_user.nickname', title: __('创建人'), operate: false, search: false},
                {field: 'create_user_id', title: __('创建人'), visible: false,addclass: 'selectpage',
						extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                {field: 'owner_user.nickname', title: __('负责人'), operate: false, search: false},
                {field: 'owner_user_id', title: __('负责人'), visible: false,addclass: 'selectpage',
						extend: 'data-source="facrm/common/selectpage/model/admin?type=children" data-field="nickname" data-orderBy="id desc"'},
                {
                    field: 'delete_time',
                    title: __('删除时间'),
                    formatter: Table.api.formatter.datetime,
                    operate: 'RANGE',
                    addclass: 'datetimerange',
                    sortable: true,
					extend: 'autocomplete="off"'
                },
                {
                    field: 'deal_status',
                    title: __('Status'),
                    formatter: Table.api.formatter.status,
                    searchList: {1: __('已成交'), 0: __('未成交')}
                },
                {
                    field: 'operate',
                    width: '130px',
                    title: __('Operate'),
                    table: table,
                    events: Table.api.events.operate,
                    buttons: [
                        {
                            name: 'Restore',
                            text: __('Restore'),
                            classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                            icon: 'fa fa-rotate-left',
                            url: 'facrm/customer/index/restore',
                            refresh: true
                        },
                        {
                            name: 'Destroy',
                            text: __('Destroy'),
                            classname: 'btn btn-xs btn-danger btn-ajax  btn-destroyit',
                            icon: 'fa fa-times',
                            url: 'facrm/customer/index/destroy',
                            refresh: true
                        }
                    ],
                    formatter: Table.api.formatter.operate
                }
            ];

            if (Config.fields && Object.keys(Config.fields).length > 0) {
                var fields = JSON.parse(Config.fields);
                var start = 20;
                for (var i = 0; i < fields.length; i++) {
                    columnss.splice(start + i, 0, fields[i]);
                }
            }

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    columnss
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            // 绑定TAB事件
            $('.panel-heading ul[data-field] li a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                $(".commonsearch-table input[name=" + field + "]").val(value);
                return false;
            });
        },
        share: function () {
            Controller.api.bindevent();
        },
        recover: function () {
            Controller.api.bindevent();
        },
        deal: function () {
            Controller.api.bindevent();
        },
        diyorder:function(){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                if ("undefined" == typeof customer_id) {
                    customer_id = 0;
                }
                Fast.api.ajax({
                    type: 'GET',
                    url: 'facrm/fields/get_fields_html',
                    data: {source: 'customer', 'id': customer_id,'tpl':'fields_6_2'}
                }, function (data) {
                    $("#extend").html(data.html);
                    Form.api.bindevent($("#extend"));
                    return false;
                });
                require.config({
                        paths: {
                            'tagsinput': '../addons/facrm/js/jquery.tagsinput',
                            'autocomplete': '../addons/facrm/js/jquery.autocomplete',
                        },
                        shim: {
                            'autocomplete': {
                                deps: ['jquery'],
                                exports: '$.fn.extend'
                            },
                            'tagsinput': {
                                deps: ['jquery', 'autocomplete', 'css!../addons/facrm/css/jquery.tagsinput.min.css'],
                                exports: '$.fn.extend'
                            }
                        }
                    }
                );
                require(['tagsinput'], function () {
                    //标签输入
                    var elem = "#c-tags";
                    var tags = $(elem);
                    tags.tagsInput({
                        width: 'auto',
                        defaultText: '输入后空格确认',
                        minInputWidth: 110,
                        height: '36px',
                        placeholderColor: '#999',
                        onChange: function (row) {
                            if (typeof callback === 'function') {

                            } else {
                                $(elem + "_addTag").focus();
                                $(elem + "_tag").trigger("blur.autocomplete").focus();
                            }
                        },
                        autocomplete: {
                            url: 'facrm/tag/autocomplete?type=customer',
                            minChars: 1,
                            menuClass: 'autocomplete-tags'
                        }
                    });
                });

               //云呼
                $(document).on('click', ".cloudcall", function () {
                    var that = this;
                    var type = $(that).data('type');
                    var typeid = $(that).data('typeid');
                    var name = $(that).data('name');
                    var field = $(that).data('field');

                    Fast.api.ajax({
                        url: 'facrm/setting/cloudcall/call',
                        data: {type: type, typeid: typeid,field:field,name:name}
                    }, function (data) {
                        Toastr.success("呼叫成功!");
                        return false;
                    });
                });


            },
            formatter: {
                thumb: function (value, row, index) {
                    if (!value) return '';
                    value = value === null ? '' : value.toString();
                    var arr = value.split(',');
                    var html = [];
                    $.each(arr, function (i, value) {

                        var reg = /\.(png|jpg|gif|jpeg|webp)$/;
                        if (reg.test(value)) {
                            html.push('<a href="' + Fast.api.cdnurl(value) + '" target="_blank"><img src="' + Fast.api.cdnurl(value) + '" alt="" style="max-height:90px;max-width:120px"></a>');
                        } else {
                            if (row.imagetype == undefined) row.imagetype = /[^\.]+$/.exec(value);
                            html.push('<a href="' + Fast.api.cdnurl(value) + '" target="_blank"><img src="' + Fast.api.fixurl("ajax/icon") + "?suffix=" + row.imagetype + '" alt="" style="max-height:90px;max-width:120px"></a>');
                        }
                    });
                    return html.join(' ');
                },
                url: function (value, row, index) {
                    return '<a href="' + row.fullurl + '" target="_blank" class="label bg-green">' + row.url + '</a>';
                },
                filename: function (value, row, index) {
                    return '<div style="width:180px;margin:0 auto;text-align:center;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;">' + Table.api.formatter.search.call(this, value, row, index) + '</div>';
                },
            }
        }
    };
    return Controller;
});