<apex:page standardcontroller="Lost_cancel_report__c" extensions="PCLLostReportByTcmController" showHeader="false"
           sidebar="false" id="allPage" action="{!init}" title="询价失单/取消报告" >
    <apex:stylesheet value="{!URLFOR($Resource.blockUIcss)}"/>
    <apex:includeScript value="{!URLFOR($Resource.jquery183minjs)}"/>
    <apex:includeScript value="{!URLFOR($Resource.PleaseWaitDialog)}"/>
    <apex:form id="allForm">
        <script>
        function saveJs() {
            blockme();
            save();
        }
        function addBrandJs() {
            blockme();
            addBrand();
        }
        function submitJS() {
            blockme();
            submit();
        }
        function addProductJs(number) {
            blockme();
            addProduct(number);
        }
        function RemoveJs(number) {
            blockme();
            Remove(number);
        }
        function setLostTotalAmount(){
            //console.log('setLostTotalAmount start:');
            var totalAmount = document.getElementById("allPage:allForm:allBlock:EditPage:LostReport:lstReport:LostTotalAmountOut:LostTotalAmount");
            var totalAmountHidden = document.getElementById("allPage:allForm:allBlock:LostTotalAmount");
            if(!!totalAmount && !!totalAmountHidden) {
                var tempLostAmount = 0.0;
                var i = 0;
                var  brandCount =  parseInt("{!brandCount}");
                //console.log(brandCount);

                for (var i = 0; i < brandCount; i++) {
                    var brandAmountObject =
                        document.getElementById("allPage:allForm:allBlock:EditPage:LostBrands:"+
                                                i + ":LostBrand:BrandContent:LostPriceOut:LostPrice");
                    var brandCompanyObject =
                        document.getElementById("allPage:allForm:allBlock:EditPage:LostBrands:"+
                                                i + ":LostBrand:BrandContent:Lost_By_CompanyOut:Lost_By_Company");

                    if(!!brandAmountObject && !!brandCompanyObject){

                        var brandCompanyText = brandCompanyObject.innerText;
                        var brandCompanyValue = brandCompanyObject.value;


                        if(!!brandCompanyText && brandCompanyText!= '--无--' && !!brandCompanyValue){
                            var brandAmount = localParseFloat(brandAmountObject.value);
                            //console.log('brandAmount:'+brandAmount);
                            tempLostAmount = tempLostAmount + brandAmount;
                        }
                    }
                }


                totalAmount.innerHTML = toNumComma(tempLostAmount);
                totalAmountHidden.value= tempLostAmount;
            }
            //console.log('setLostTotalAmount end');
        }
        function setBrandName(brandNumber){
            blockme();
            setbrand(brandNumber);
        }
        </script>
        <apex:actionFunction name="save" action="{!save}" oncomplete="unblockUI();" rerender="allForm,EditButton2"></apex:actionFunction>
        <apex:actionFunction name="setbrand" action="{!setbrand}" oncomplete="unblockUI();" rerender="allForm">
            <apex:param assignTo="{!setBrandNo}" name="setBrandNo" value=""/>
        </apex:actionFunction>

        <apex:actionFunction name="submit" action="{!submit}" oncomplete="unblockUI();" rerender="allForm,EditButton2"></apex:actionFunction>


        <apex:actionFunction name="addBrand" action="{!addBrand}" oncomplete="unblockUI();" rerender="allForm,EditButton2"></apex:actionFunction>
        <apex:actionFunction action="{!addProduct}" name="addProduct" reRender="allForm" oncomplete="unblockUI();">
            <apex:param assignTo="{!brandNo}" name="brandNo" value=""/>
        </apex:actionFunction>
        <apex:actionFunction action="{!Remove}" name="Remove" reRender="allForm" oncomplete="unblockUI();">
            <apex:param assignTo="{!RemoveBrandNo}" name="RemoveBrandNo" value=""/>
        </apex:actionFunction>
        <apex:pageBlock id="allBlock" >
            <apex:outputPanel id="message">
                <apex:pageMessages />
            </apex:outputPanel>
            <apex:inputHidden id="LostTotalAmount" value="{!LostReport.lostReport.LostTotalAmount__c}" />
            <apex:outputPanel rendered="{!IF(pageStatus == 'Create'|| pageStatus == 'Edit' , 'true', 'false')}">
                <apex:pageBlock title="失单报告编辑页面" id="EditPage">
                    <apex:pageBlockSection columns="2" id="LostReport" >
                        <apex:pageBlockSectionItem id="lstReport" labelStyle="text-align:left;">
                            <apex:pageBlockSectionItem labelStyle="text-align:left;">
                                <apex:outputLabel value="失单类型: "><span style="color:red;font-size: 18px;">* </span></apex:outputLabel>
                                <apex:inputField id="LostType" value="{!LostReport.lostReport.LostType__c}" />
                            </apex:pageBlockSectionItem>
                            <apex:pageBlockSectionItem id="LostTotalAmountOut" labelStyle="text-align:left;">
                                <apex:outputLabel value="失单总金额（元）: "></apex:outputLabel>
                                <apex:outputField id="LostTotalAmount" value="{!LostReport.lostReport.LostTotalAmount__c}" />
                            </apex:pageBlockSectionItem>

                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem id="Report_Statusout" labelStyle="text-align:left;">
                            <apex:outputLabel value="状态: "></apex:outputLabel>
                            <apex:outputField id="Report_Status" value="{!LostReport.lostReport.Report_Status__c}" />
                        </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>
                    <apex:variable value="{!0}" var="BCnt"/>
                    <apex:repeat value="{!LostReport.LostBrands}" var="brand" id="LostBrands">
                        <apex:pageBlockSection columns="1" id="LostBrand" collapsible="true" showHeader="true">
                            <apex:facet name="header">
                                <apex:outputPanel >
                                    <apex:outputText id="LostBrandName" value="失单品牌{!brand.lineNo + 1}" />
                                    <apex:commandButton rendered="{!IF(brandCount > 1 , 'true', 'false')}" onclick="RemoveJs({!brand.lineNo});return false;"  value="删除" style="width: 100px;margin-left: 50px;float: right;"/>
                                    <apex:commandButton onclick="saveJs();return false;"  value="保存" style="width: 100px;margin-left: 50px;float: right;"/>
                                </apex:outputPanel>
                            </apex:facet>
                            <apex:pageBlockSection id="BrandContent" columns="3">
                                <apex:pageBlockSectionItem dataStyle="width:10%; border-top:2px solid;border-left:2px solid"
                                labelStyle="width:23%; text-align:left;border-top:2px solid;border-left:2px solid ">
                                    <apex:outputLabel value="失单产品类别:"><span style="color:red;font-size: 18px;">* </span></apex:outputLabel>
                                    <!-- <apex:inputField id="LostPRoductClass" value="{!brand.lostBrand.ProductClass__c}" /> -->
                                </apex:pageBlockSectionItem>
                                <apex:pageBlockSectionItem dataStyle="width:10%;border-top:2px solid;border-left:2px solid"
                                labelStyle="width:23%;text-align:left;border-top:2px solid;border-left:2px solid ">
                                    <apex:outputLabel value="失单产品:"><span style="color:red;font-size: 18px;">* </span></apex:outputLabel>
                                    <!-- <apex:inputField id="LostProductCategory" value="{!brand.lostBrand.ProductCategory__c}" /> -->
                                </apex:pageBlockSectionItem>
                                <apex:pageBlockSectionItem id="Lost_By_CompanyOut" dataStyle="width:10%;border-top:2px solid;border-left:2px solid; border-right:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-top:2px solid;border-left:2px solid ">
                                    <apex:outputLabel value="失单品牌:"><span style="color:red;font-size: 18px;">* </span></apex:outputLabel>
                                    <apex:inputField id="Lost_By_Company" onchange="setLostTotalAmount();setBrandName({!brand.lineNo});" value="{!brand.lostBrand.Lost_By_Company__c}"  />
                                </apex:pageBlockSectionItem>

                                <apex:pageBlockSectionItem dataStyle="width:10%;border-top:2px solid;border-left:2px solid;border-bottom:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-top:2px solid;border-left:2px solid ;border-bottom:2px solid;">
                                    <apex:outputLabel value="失单原因（主）:"><span style="color:red;font-size: 18px;">* </span></apex:outputLabel>
                                    <apex:inputField id="Lost_reason_main" value="{!brand.lostBrand.Lost_reason_main__c}" />
                                </apex:pageBlockSectionItem>
                                <apex:pageBlockSectionItem dataStyle="width:10%;border-top:2px solid;border-left:2px solid;border-bottom:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-top:2px solid ;border-left:2px solid;border-bottom:2px solid;">
                                    <apex:outputLabel value="失单原因（次）:"> </apex:outputLabel>
                                    <apex:inputField id="Lost_Reason_Sub" value="{!brand.lostBrand.Lost_Reason_Sub__c}" />
                                </apex:pageBlockSectionItem>

                                <apex:pageBlockSectionItem dataStyle="width:10%;border-top:2px solid;border-left:2px solid;border-bottom:2px solid;border-right:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-top:2px solid ;border-left:2px solid;border-bottom:2px solid;">
                                    <apex:outputLabel value="失单品牌（手动）:"> </apex:outputLabel>
                                    <apex:inputField id="Lost_By_Company_Mannual" value="{!brand.lostBrand.Lost_By_Company_Mannual__c}" />
                                </apex:pageBlockSectionItem>

                                <apex:pageBlockSectionItem id="LostPriceOut" dataStyle="width:10%;border-left:2px solid;border-bottom:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-left:2px solid ;border-bottom:2px solid;">
                                    <apex:outputLabel value="失单金额（元）:"><span style="color:red;font-size: 18px;">* </span></apex:outputLabel>
                                    <apex:inputField id="LostPrice" onchange="setLostTotalAmount();" value="{!brand.lostBrand.LostPrice__c}" />
                                </apex:pageBlockSectionItem>
                                <apex:pageBlockSectionItem id="AgencyOut" dataStyle="width:10%;border-left:2px solid;border-bottom:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-left:2px solid ;border-bottom:2px solid;">
                                    <apex:outputLabel value="中标经销商:"> <span style="color:red;font-size: 18px;">* </span> </apex:outputLabel>
                                    <apex:inputField id="Agency"  value="{!brand.lostBrand.Agency__c}" />
                                </apex:pageBlockSectionItem>
                                <apex:pageBlockSectionItem id="AgencyMannualOut" dataStyle="width:10%;border-right:2px solid;border-left:2px solid;border-bottom:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-bottom:2px solid;border-left:2px solid;">
                                    <apex:outputLabel value="中标经销商（手动）:"> </apex:outputLabel>
                                    <apex:inputField id="AgencyMannual"  value="{!brand.lostBrand.AgencyMannual__c}" />
                                </apex:pageBlockSectionItem>
                            </apex:pageBlockSection>

                            <apex:pageBlockTable value="{!brand.LostProducts}" var="product"  id="productTable"
                                                 style="border-bottom-width: 0px; font-size:13px;width:70%" border="0" cellspacing="0" cellpadding="0">
                                <apex:column headerValue="失单品牌">
                                    <apex:outputPanel style="display:none">
                                        <!-- <apex:inputField id="CompetitorIn"   value="{!product.Competitor__c}" /> -->
                                    </apex:outputPanel>
                                    <apex:outputPanel style="display: inline-block; min-width: 150px;">
                                    <!-- <apex:outputField id="CompetitorOut"   value="{!product.Competitor__c}" /> -->
                                    </apex:outputPanel>
                                </apex:column>
                                <apex:column headerValue="失单对手型号">
                                    <apex:inputField id="LostProduct" value="{!product.LostProduct__c}" />
                                </apex:column>
                                <apex:column headerValue="失单数量" >
                                    <span style="color:red;font-size: 18px;">* </span><apex:inputField id="Quantity" value="{!product.Quantity__c}" />
                                </apex:column>
                                <!-- SWAG-C6P9PX lt 20210913 add start -->
                                <apex:column headerValue="失单对手型号(手动)">
                                    <apex:inputField id="LostProductMannual" value="{!product.LostProductMannual__c}" />
                                </apex:column>
                                <!-- SWAG-C6P9PX lt 20210913 add end -->
                                <apex:column style="width:10%" headerValue="+">
                                    <apex:image id="plusicon" value="{!$Resource.plusicon}" onclick="addProductJs({!brand.lineNo});"/>
                                </apex:column>
                            </apex:pageBlockTable>
                        </apex:pageBlockSection>
                        <apex:variable value="{!BCnt+1}" var="BCnt" />
                    </apex:repeat>
                    <apex:pageBlockButtons >
                        <apex:commandButton onclick="addBrandJs();return false;"  value="追加品牌" style="width: 150px;margin-left: 50px;"/>
                        <apex:commandButton onclick="saveJs();return false;"  value="保存" style="width: 150px;margin-left: 50px;"/>
                        <apex:commandButton id="CancelButton"  action="{!cancel}" value="返回询价" style="width: 150px;margin-left: 50px;"/>
                    </apex:pageBlockButtons>
                </apex:pageBlock>
            </apex:outputPanel>
            <apex:outputPanel rendered="{!IF(pageStatus == 'View', 'true', 'false')}">
                <apex:pageBlock title="失单报告查看页面" id="ReadPage">
                    <apex:pageBlockSection columns="2" id="LostReport" >
                        <apex:pageBlockSectionItem labelStyle="text-align:left;">
                            <apex:pageBlockSectionItem labelStyle="text-align:left;">
                                <apex:outputLabel value="失单类型: "></apex:outputLabel>
                                <apex:outputField id="LostType" value="{!LostReport.lostReport.LostType__c}" />
                            </apex:pageBlockSectionItem>
                            <apex:pageBlockSectionItem labelStyle="text-align:left;">
                                <apex:outputLabel value="失单总金额（元）: "></apex:outputLabel>
                                <apex:outputField id="LostTotalAmount" value="{!LostReport.lostReport.LostTotalAmount__c}" />
                            </apex:pageBlockSectionItem>

                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem id="Report_Statusout" labelStyle="text-align:left;">
                            <apex:outputLabel value="状态: "></apex:outputLabel>
                            <apex:outputField id="Report_Status" value="{!LostReport.lostReport.Report_Status__c}" />
                        </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>
                    <apex:variable value="{!0}" var="BCnt"/>
                    <apex:repeat value="{!LostReport.LostBrands}" var="brand" id="LostBrands">
                        <apex:pageBlockSection columns="1" id="LostBrand" collapsible="true" showHeader="true">
                            <apex:facet name="header">失单品牌{!brand.lineNo + 1}</apex:facet>
                            <apex:pageBlockSection id="BrandContent" columns="3">
                                <apex:pageBlockSectionItem dataStyle="width:10%; border-top:2px solid;border-left:2px solid"
                                labelStyle="width:23%; text-align:left;border-top:2px solid;border-left:2px solid ">
                                    <apex:outputLabel value="失单产品类别:"><span style="color:red;font-size: 18px;">* </span></apex:outputLabel>
                                    <!-- <apex:outputtext style="display: inline-block;min-width: 250px" id="LostPRoductClass" value="{!brand.lostBrand.ProductClass__c}" /> -->
                                </apex:pageBlockSectionItem>
                                <apex:pageBlockSectionItem dataStyle="width:10%;border-top:2px solid;border-left:2px solid"
                                labelStyle="width:23%;text-align:left;border-top:2px solid;border-left:2px solid ">
                                    <apex:outputLabel value="失单产品:"><span style="color:red;font-size: 18px;">* </span></apex:outputLabel>
                                    <!-- <apex:outputtext style="display: inline-block;min-width: 250px" id="LostProductCategory" value="{!brand.lostBrand.ProductCategory__c}" /> -->
                                </apex:pageBlockSectionItem>
                                <apex:pageBlockSectionItem id="Lost_By_CompanyOut" dataStyle="width:10%;border-top:2px solid;border-left:2px solid; border-right:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-top:2px solid;border-left:2px solid ">
                                    <apex:outputLabel value="失单品牌:"><span style="color:red;font-size: 18px;">* </span></apex:outputLabel>
                                    <apex:outputtext style="display: inline-block;min-width: 250px" id="Lost_By_Company"  value="{!brand.lostBrand.Lost_By_Company__c}"  />
                                </apex:pageBlockSectionItem>

                                <apex:pageBlockSectionItem dataStyle="width:10%;border-top:2px solid;border-left:2px solid;border-bottom:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-top:2px solid;border-left:2px solid ;border-bottom:2px solid;">
                                    <apex:outputLabel value="失单原因（主）:"><span style="color:red;font-size: 18px;">* </span></apex:outputLabel>
                                    <apex:outputField id="Lost_reason_main" value="{!brand.lostBrand.Lost_reason_main__c}" />
                                </apex:pageBlockSectionItem>
                                <apex:pageBlockSectionItem dataStyle="width:10%;border-top:2px solid;border-left:2px solid;border-bottom:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-top:2px solid ;border-left:2px solid;border-bottom:2px solid;">
                                    <apex:outputLabel value="失单原因（次）:"> </apex:outputLabel>
                                    <apex:outputField id="Lost_Reason_Sub" value="{!brand.lostBrand.Lost_Reason_Sub__c}" />
                                </apex:pageBlockSectionItem>

                                <apex:pageBlockSectionItem dataStyle="width:10%;border-top:2px solid;border-left:2px solid;border-bottom:2px solid;border-right:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-top:2px solid ;border-left:2px solid;border-bottom:2px solid;">
                                    <apex:outputLabel value="失单品牌（手动）:"> </apex:outputLabel>
                                    <apex:outputField id="Lost_By_Company_Mannual" value="{!brand.lostBrand.Lost_By_Company_Mannual__c}" />
                                </apex:pageBlockSectionItem>

                                <apex:pageBlockSectionItem id="LostPriceOut" dataStyle="width:10%;border-left:2px solid;border-bottom:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-left:2px solid ;border-bottom:2px solid;">
                                    <apex:outputLabel value="失单金额（元）:"><span style="color:red;font-size: 18px;">* </span></apex:outputLabel>
                                    <apex:outputField id="LostPrice"  value="{!brand.lostBrand.LostPrice__c}" />
                                </apex:pageBlockSectionItem>
                                <apex:pageBlockSectionItem id="AgencyOut" dataStyle="width:10%;border-left:2px solid;border-bottom:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-left:2px solid ;border-bottom:2px solid;">
                                    <apex:outputLabel value="中标经销商:"> <span style="color:red;font-size: 18px;">* </span> </apex:outputLabel>
                                    <apex:outputField id="Agency"  value="{!brand.lostBrand.Agency__c}" />
                                </apex:pageBlockSectionItem>
                                <apex:pageBlockSectionItem id="AgencyMannualOut" dataStyle="width:10%;border-right:2px solid;border-left:2px solid;border-bottom:2px solid;"
                                                           labelStyle="width:23%;text-align:left;border-bottom:2px solid;border-left:2px solid;">
                                    <apex:outputLabel value="中标经销商（手动）:"> </apex:outputLabel>
                                    <apex:outputField id="AgencyMannual"  value="{!brand.lostBrand.AgencyMannual__c}" />
                                </apex:pageBlockSectionItem>
                            </apex:pageBlockSection>

                            <apex:pageBlockTable value="{!brand.LostProducts}" var="product"  id="productTable"
                                                 style="border-bottom-width: 0px; font-size:13px;width:60%" border="0" cellspacing="0" cellpadding="0">
                                <apex:column headerValue="失单品牌">
                                    <apex:outputPanel style="display: inline-block; min-width: 50px;">
                                    <!-- <apex:outputField id="CompetitorOut"   value="{!product.Competitor__c}" /> -->
                                    </apex:outputPanel>
                                </apex:column>
                                <apex:column headerValue="失单对手型号">
                                    <apex:outputField id="LostProduct" value="{!product.LostProduct__c}" />
                                </apex:column>
                                <apex:column headerValue="失单数量" >
                                    <apex:outputField id="Quantity" value="{!product.Quantity__c}" />
                                </apex:column>
                                <!-- SWAG-C6P9PX lt 20210913 add start -->
                                <apex:column headerValue="失单对手型号(手动)">
                                    <apex:outputField id="LostProductMannual" value="{!product.LostProductMannual__c}" />
                                </apex:column>
                                <!-- SWAG-C6P9PX lt 20210913 add end -->
                            </apex:pageBlockTable>
                        </apex:pageBlockSection>
                        <apex:variable value="{!BCnt+1}" var="BCnt" />
                    </apex:repeat>

                    <apex:pageBlockButtons >

                        <apex:commandButton rendered="{!IF(LostReport.lostReport.Report_Status__c = '草案', 'true', 'false')}" id="SubmitButton" onclick="submitJS();return false;" value="提交" style="width: 150px;margin-left: 50px;"/>
                        <apex:commandButton rendered="{!IF(LostReport.lostReport.Report_Status__c = '草案' || $Profile.Id = '00e10000000Y3o5AAC', 'true', 'false')}" id="EditButton" reRender="allForm" action="{!edit2}"  value="编辑" style="width: 150px;margin-left: 50px;"/>
                        <apex:commandButton id="CancelButton"  action="{!cancel}" value="返回询价" style="width: 150px;margin-left: 50px;"/>

                    </apex:pageBlockButtons>
                </apex:pageBlock>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
    <apex:outputPanel id="approvalProcess" rendered="{!IF(pageStatus == 'View', 'true', 'false')}">
        <apex:relatedList list="ProcessSteps"/>
    </apex:outputPanel>
</apex:page>