


CooWorkApp.controller('list_576_Controller', function ($rootScope, $scope, $location, $sce, locdbServices, cooConfig, g,ngDialog) {

    $scope.hello = cooConfig.configObj.url;

});

 

CooWorkApp.controller('edit_5687_Controller', function ($scope, $location, $routeParams, $sce, locdbServices, userSetting, cooConfig, g, rest, ngDialog, _) {
    var openAlert = function (msg) {
        $scope.message = msg;
        $scope.cDlgId = ngDialog.open({
            template: 'tmpl/alert.html',
            className: 'ngdialog-theme-default-dialog',
            controller: 'dialogController',
            overlay: false,
            scope: $scope
        });
    };




    $scope.hello = cooConfig.configObj.url;

    var fs = require('fs');

    window.ondragover = function (e) { e.preventDefault(); return false };
    window.ondrop = function (e) { e.preventDefault(); return false };

    var categoryId = $scope.categoryId;
    $scope.categoryId = categoryId;
    var pageId = $scope.editPageId;
    var currentPage = g.pageFilterById(pageId)[0];
    $scope.title = currentPage.UIname + ' - 编辑';


    var itemId = $scope.itemId;
    var pattern = g.patternFilterByIndexId(categoryId);

    $scope.elements = [];
    $scope.options = [];
    $scope.indexId = 0;
    $scope.result = [];
    var source = [];
    var prefix = '.createTable ';

    $scope.editConfigElements = [];
    var oeElements = g.patternFilterByPage(pageId);




    //custom code
    $scope.skuImages = [];


    //custom code

    $.when(userSetting.getSettingElementByPage(pageId)).done(function (a) {


        //config
        var eleStr = ',';
        $(a).each(function (i, item) {
            eleStr += item.ID + ',';
        });

        $(oeElements).each(function (i, o) {
            if (eleStr.indexOf(',' + o.ID.toString() + ',') > -1) {
                $scope.editConfigElements.push({ item: o, select: true });
            }
            else {
                $scope.editConfigElements.push({ item: o, select: false });
            }
        });
        //config




        $.when(rest.detail(pageId, itemId)).done(function (result) {

            $scope.result = result;
            $scope.indexId = result.data['__c' + pattern[0].ID];
            //result.data ,.join,.related
            $(a).each(function (i, o) {
                source = g.makeSource(o);
                var vv = result.data['__c' + o.ID];
                if (o.pTypeId == 10) {

                    var imgReg = /\.(bmp|jpg|jpeg|png|gif)"?$/i;
                    $(vv).each(function (j, k) {
                        k['isImage'] = imgReg.test(k.Filename);
                    });

                    //  console.log(vv);
                }
                if (o.pTypeId == 6) {
                    vv = g.dateFormat(vv);
                }
                $scope.elements.push({ p: o, v: vv, s: source });
            });

            var lsc = [];
            lsc.push({ patternId: 13859, val1: result.data['__c13859'], pTypeId: 5, operation: 1 });
            var listPageId = 5688;   //hard coding

            console.log(lsc);

            $.when(rest.search(categoryId, listPageId, lsc, 0, 20)).done(function (result2) {

                $(result2.data).each(function (i, o) {

                    $scope.skuImages = _.union($scope.skuImages, o.__c13873);
                });

                $scope.$apply();
                g.jqueryInit(prefix, $scope.indexId);


            });
        });


        $scope.sib = function (id) {
            $('#__u' + id).prop('checked', true);
        };


        $scope.chooseImg = function (id) {

            var chks = $('.chk:checked');
            if (chks.length > 0) {
                if (chks.length > 1) chks = $('.chk:checked').eq(0);
                $('#' + id).val(chks.attr('id').replace('__u', ''));
                $('#' + id).attr('v', 'e');
                $('.chk').prop('checked', false);
            }

        };



        $scope.fileSelect = function ($id) {
            $('#file_' + $id).trigger('click');

        };

        $scope.fileNameChanged = function (element) {
            $scope.$apply(function ($scope) {
                if (element.files.length > 0)
                    if (fs.existsSync(element.files[0].path)) {
                        var formData = {
                            key: cooConfig.configObj.uploadKey,
                            itemId: itemId,
                            cateId: categoryId,
                            my_file: fs.createReadStream(element.files[0].path)
                        };

                        var id = $(element).attr('id').replace(/\D/ig, '');
                        $.when(g.postFile(formData)).done(function (obj) {
                            $("#__img" + id + " ul").html("<li tag='" + obj.ID + "'><img src='" + obj.FilePath + "' width='100px' /><span>  </span><a href='javascript:void(0);' class='del' pat='" + id + "' att='" + obj.ID + "' cat='" + categoryId + "' >删除</a></li>")
                            $("#__c" + id).val(obj.ID);
                            $("#__c" + id).attr("v", "e");
                        });
                    }
            });
        }


        $scope.fileNameChanged2 = function (element) {
            $scope.$apply(function ($scope) {
                if (element.files.length > 0)
                    var id = $(element).attr('id').replace(/\D/ig, '');
                $(element.files).each(function (i, o) {

                    if (fs.existsSync(element.files[i].path)) {
                        var formData = {
                            key: cooConfig.configObj.uploadKey,
                            itemId: itemId,
                            cateId: categoryId,
                            my_file: fs.createReadStream(element.files[i].path)
                        };
                        $.when(g.postFile(formData)).done(function (obj) {

                            var objv = "<a href='" + obj.FilePath + "' >" + obj.Filename + "</a>";
                            var imgReg = /\.(bmp|jpg|jpeg|gif|png)$/ig;
                            if (imgReg.test(obj.FilePath)) {
                                objv = "<img src='" + obj.FilePath + "' width='100px' />";
                            }

                            $("#__img" + id + " ul").append("<li tag='" + obj.ID + "'>" + objv + "<br/><a href='javascript:void(0);' class='del' pat='" + id + "' att='" + obj.ID + "' cat='" + categoryId + "' >删除</a></li>")
                            var val = $("#__c" + id).val();
                            if (val != "") val += ",";
                            val += obj.ID;
                            $("#__c" + id).val(val);
                            $("#__c" + id).attr("v", "e");
                        });
                    }

                });
            });
        }



        $scope.openSelect = function (element) {

            $scope.element = element;

            var dlg1 = ngDialog.open({
                template: 'tmpl/select.html',
                controller: 'selectController',
                scope: $scope
            });


            $scope.currentDlg = dlg1.id;

        };


        $scope.rowSelect = function (index, name) {

            $('#view__j' + $scope.element.ID).val(name);
            $('#__j' + $scope.element.ID).val(index);
            $('#__j' + $scope.element.ID).attr('v', 'e');
            ngDialog.close($scope.currentDlg);
        };





        $scope.doSubmit = function () {



            var check = true;
            var authCheck = true;
            var parentCheck = true;
            var reqCheck = true;
            var uniCheck = true;
            $(".createTable [tag='edit']").each(function (i) {

                if ($(this).attr("vad") && $(this).attr("vad") != '' && $(this).attr("vad") != 'null' && $(this).val() != '') {
                    var regx = new RegExp(decodeURIComponent($(this).attr("vad")));
                    if (!regx.test($.trim($(this).val()))) {
                        $(this).addClass("fieldInvalid");
                        openAlert($(this).parent('div').find('span').text() + " 格式不对");
                        check = false;
                        return false;
                    }
                }
            });


            $(".createTable [req='true']").each(function () {
                if ($(this).val() == '') {
                    reqCheck = false;
                    openAlert("必填项未填!" + $(this).parent('div').find('span').text());
                    //    window.external.MessageBox("必填项未填!");
                    return false;
                }
            });




            $(".createTable [unq='true']").each(function (i) {

                if ($(this).val() != '') {
                    var cid = $scope.indexId;

                    var etor = {
                        val: $(this).val(),
                        name: $(this).attr('id'),
                        itemId: cid
                    };

                    $.when(rest.uniqueCheck(etor)).done(function (data) {
                        if (data != '') {
                            $('.createTable #' + etor.name).addClass("fieldInvalid");
                            openAlert("已存在," + data);
                            uniCheck = false;
                            return false;
                        }
                    });
                }
            });




            if (check && reqCheck && uniCheck) {
                var RetArray = [];
                $(".createTable [v='e']").each(function (i) {

                    var tempV = $.trim($(this).val());
                    if ($(this).is("div"))
                        tempV = $.trim($(this).html());

                    if ($(this).is(':checkbox'))
                        tempV = $(this).is(":checked");

                    var o = { _Key: $(this).attr('id'), _Value: tempV };
                    RetArray.push(o);
                });

                $(".createTable .jqtxt").each(function (i) {
                    var tempj = $.trim($(this).val());

                    var o = { _Key: $(this).attr('id'), _Value: tempj };

                    RetArray.push(o);

                });

                $(".createTable [multiple='multiple']").each(function (i) {
                    var tempV = $("#" + $(this).attr("id")).val();

                    if (tempV != undefined && tempV.length > 0) {
                        var tempStr = tempV.join(',');
                        var o = { _Key: $(this).attr('id'), _Value: tempStr };
                        RetArray.push(o);
                    }
                });



                var RArray = [];
                $(".createTable .childTable").each(function (i, t) {
                    $rows = $(t).find("tr");
                    $rows.each(function (i, tr) {
                        var erow = [];
                        $(tr).find("input[tag='redit']").each(function (i) {
                            var o = { _Key: $(this).attr('name'), _Value: $.trim($(this).val()) };
                            erow.push(o);
                        });
                        if (erow.length > 0) {
                            RArray.push({
                                key: $(t).attr("id"),
                                value: erow
                            });
                        }
                    });
                });

                var d1 = JSON.stringify(RetArray);
                var d2 = JSON.stringify(RArray);


                var iid = $scope.indexId;

                var etor = {
                    jsonStr: d1,
                    relatedSTR: d2,
                    pageId: pageId,
                    itemId: iid
                };


                console.log(etor);



                $.when(rest.edit(etor)).done(function (data) {

                    g.resetUserInput(prefix);
                    $scope.pageNumber($scope.startIndex);
                    $scope.closeDialog();
                    $scope.$apply();
                });

            }

        };




        $scope.editConfig = function () {
            if (!$("#editConfig").is(":visible")) {
                $("#editConfig").show();
            } else {
                $("#editConfig").hide();

                $.when(userSetting.pageConfig(a, 'oe', pageId)).done(function (f) {
                    if (f) {

                        $.when(locdbServices.fetchAllUserElements()).done(function (result) {
                            cooConfig.alluserelement = result;
                            $scope.closeDialog();
                            $scope.$apply();
                        });
                    }
                });
            }
        };

    });



});


 