commonModule.
controller('invoiceRelevanceModalController', ['SweetAlert', '$scope', '$log', '$translate', '$location', '$timeout', '$interval', '$uibModal',
    function (SweetAlert, $scope, $log, $translate, $location, $timeout, $interval, $uibModal) {

        $scope.items = ['item1', 'item2', 'item3'];
        $scope.animationsEnabled = true;
        var templateUrl = '';
        $scope.isShow = false;
        $scope.selected = {
            item: $scope.items[0]
        };

        //当前选中的发票
        $scope.currentInvoice = '';

        //初始化NIS模板页面控件
        var initNISPageControl = function () {
            var simpleProducts = [
                "原料采购",
                "非原料集中采购",
                "报销"
            ];
            //发票来源
            $("#invoiceSourceNIS").dxSelectBox({
                items: simpleProducts
            });
            //收货单号输入框
            $("#acceptNOsNIS").dxTextBox({
                value: "",
                placeholder: "请输入收货单号",
            });
            //商品种类
            $("#productTypeNIS").dxSelectBox({
                items: ["原料"],
                value: '原料',
                readOnly: true
            });
            //是否计入进项税额
            $("#isIncludedInTaxAmountNIS").dxSelectBox({
                items: ['是', '否'],
                value: '是',
                readOnly: true
            });
        };

        //初始化CSSP-四家模板页面控件
        var initCSSPFourPageControl = function () {
            var sales =
                [{ "orderId": 10248 },
                { "orderId": 10249 },
                { "orderId": 10250 },
                { "orderId": 10251 }];
            var simpleProducts = [
                "原料采购",
                "非原料集中采购",
                "报销"
            ];
            //发票来源
            $("#invoiceSourceCSSPFour").dxSelectBox({
                items: simpleProducts
            });
            //采购单号输入框
            $("#purchaseOrderCSSPFour").dxTextBox({
                value: "",
                placeholder: "请输入采购单号",
            });
            //商品种类
            $("#productTypeCSSPFour").dxSelectBox({
                items: ["原料"],
                value: '原料',
                readOnly: true
            });
            //是否计入进项税额
            $("#isIncludedInTaxAmountCSSPFour").dxSelectBox({
                items: ['是', '否'],
                value: '是',
                readOnly: true
            });
            //初始化公司代码code
            var dataGrid = $("#company-code-container").dxDataGrid({
                dataSource: sales,
                selection: {
                    mode: "multiple",
                    showCheckBoxesMode: 'always'
                },
                paging: {
                    pageSize: 10
                },
                filterRow: {
                    visible: true
                },
                columns: [{
                    dataField: "orderId",
                    caption: "公司代码",
                    width: 150
                }]
            }).dxDataGrid("instance");
            //初始化成本中心code
            var dataGrid = $("#cost-center-container").dxDataGrid({
                dataSource: sales,
                selection: {
                    mode: "multiple",
                    showCheckBoxesMode: 'always'
                },
                paging: {
                    pageSize: 10
                },
                filterRow: {
                    visible: true
                },
                columns: [{
                    dataField: "orderId",
                    caption: "成本中心",
                    width: 150
                }]
            }).dxDataGrid("instance");
            
            //凭证号输入框
            $("#voucherNoCSSPFour").dxTextBox({
                value: "",
                placeholder: "请输入凭证号",
            });
        };

        //初始化CSSP模板页面控件
        var initCSSPPageControl = function () {
            var simpleProducts = [
                "原料采购",
                "非原料集中采购",
                "报销"
            ];
            //发票来源
            $("#invoiceSourceCSSP").dxSelectBox({
                items: simpleProducts
            });
            //采购单号输入框
            $("#purchaseOrderCSSP").dxTextBox({
                value: "",
                placeholder: "请输入采购单号",
            });
            //商品种类
            $("#productTypeCSSP").dxSelectBox({
                items: ["原料"],
                value: '原料',
                readOnly: true
            });
            //强制应税checkbox
            $("#forceTaxCheckboxCSSP").dxCheckBox({
                value: true,
                width: 80,
                text: "强制应税"
            });

            //强制应税原因
            $("#forceTaxReasonCSSP").dxTextArea({
                value: '',
                height: 90
            })

            //是否计入进项税额
            $("#isIncludedInTaxAmountCSSP").dxSelectBox({
                items: ['是', '否'],
                value: '是',
                readOnly: true
            });

            //凭证号输入框
            $("#voucherNoCSSP").dxTextBox({
                value: "",
                placeholder: "请输入凭证号",
            });
        };

        //初始化报销模板页面控件
        var initApplyForReimbursementPageControl = function () {
            var simpleProducts = [
                "原料采购",
                "非原料集中采购",
                "报销"
            ];
            //发票来源
            $("#invoiceSourceReibursement").dxSelectBox({
                items: simpleProducts
            });
            //员工编号输入框
            $("#purchaseOrderReibursement").dxTextBox({
                value: "",
                placeholder: "请输入员工编号",
            });
            //商品种类
            $("#productTypeReibursement").dxSelectBox({
                items: ["原料"],
                value: '原料',
                readOnly: true
            });

            //是否计入进项税额
            $("#isIncludedInTaxAmountReibursement").dxSelectBox({
                items: ['是', '否'],
                value: '是',
                readOnly: true
            });

            //凭证号输入框
            $("#voucherNoReibursement").dxTextBox({
                value: "",
                placeholder: "请输入凭证号",
            });
        };

        //当前页面的事件
        var thisPageService = {
            //保存NIS数据
            saveNisData: function (modalInstance) {
                SweetAlert.swal('保存Nis数据成功!');
                modalInstance.close($scope.selected.item);
                modalInstance.dismiss('cancel');
            }
        };


        //当前modal的服务
        var thisModalService = {
            createInstance: function () {
                //判断当前需要打开的是哪一个modal
                if ($scope.relevanceType == 1) {
                    //NIS模板
                    templateUrl = 'relevance-nis.html';
                }
                else if ($scope.relevanceType == 2) {
                    //CSSP-四家模板
                    templateUrl = 'relevance-cssp-four.html';
                }
                else if ($scope.relevanceType == 3) {
                    //CSSP
                    templateUrl = 'relevance-cssp.html';
                }
                else if ($scope.relevanceType == 4) {
                    //报销
                    templateUrl = 'relevance-apply-for-reimbursement.html';
                }

                //实例化一个modal并打开
                var modalInstance = $uibModal.open({
                    animation: true,
                    templateUrl: templateUrl,
                    scope: $scope,
                    backdrop: 'static',
                    windowClass: 'invoice-relevance-modal-wrapper',
                    resolve: {
                        items: function () {//items是一个回调函数
                            return $scope.items;//这个值会被模态框的控制器获取到
                        }
                    }
                });

                //modal关闭之后接收返回值的函数
                modalInstance.result.then(function (data) {
                    //设置isShow变量为false
                    $scope.isShow = false;
                    //设置关联的类型为-1
                    $scope.relevanceType = -1;
                    $scope.selected = data;//模态框的返回值

                }, function () {
                    $log.info('Modal dismissed at: ' + new Date());
                });

                //点击modal窗口上的确定事件
                $scope.save = function () {
                    if ($scope.relevanceType == 1) {
                        thisPageService.saveNisData(modalInstance);
                    }
                };


                //点击modal窗口上的取消事件
                $scope.cancel = function () {
                    //设置isShow变量为false
                    $scope.isShow = false;
                    //设置关联的类型为-1
                    $scope.relevanceType = -1;
                    modalInstance.dismiss('cancel');
                };

            }
        };





        (function initialize() {
            $log.debug('invoiceRelevanceModalController.ctor()...');

            $scope.$watch('isShow', function (newValue, oldValue) {
                if (newValue === true) {
                    thisModalService.createInstance();
                    $timeout(function () {
                        if ($scope.relevanceType == 1) {
                            initNISPageControl();
                        }
                        else if ($scope.relevanceType == 2) {
                            initCSSPFourPageControl();
                        }
                        else if ($scope.relevanceType == 3) {
                            initCSSPPageControl();
                        }
                        else if ($scope.relevanceType == 4) {
                            initApplyForReimbursementPageControl();
                        }
                    }, 500);

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