<apex:page controller="ConsumableSampleApplyController" sidebar="false" action="{!init}" id="Page">
    <apex:stylesheet value="{!URLFOR($Resource.jquerysuggestcss)}"/>
    <apex:stylesheet value="{!URLFOR($Resource.blockUIcss)}"/>
    <apex:includeScript value="{!URLFOR($Resource.jquery183minjs)}"/>
    <apex:includeScript value="{!URLFOR($Resource.PleaseWaitDialog)}"/>
    <apex:includeScript value="{!URLFOR($Resource.jquerysuggestjs)}"/>
    <apex:includeScript value="{!URLFOR($Resource.ConsumableSampleApplyJS)}"/>
    <apex:includeScript value="{!URLFOR($Resource.CommonUtilJs)}"/>
    <apex:includeScript value="{!URLFOR($Resource.connection20)}"/>
    <apex:includeScript value="{!URLFOR($Resource.apex20)}"/>
    <apex:includeScript value="{!URLFOR($Resource.jquerydoubletapjs)}"/>
    <script type="text/javascript">
        var displayCost = '{!displayCost}';
        var quoid = '{!quoid}';
        var Session_ID = '{!$Api.Session_ID}';
        var Price_Valid_Period = '{!$Label.Price_Valid_Period}';
        var Message_001 = '{!JSENCODE($Label.Message_001)}';
        var Message_Please_Save_Quote = '{!JSENCODE($Label.Please_Save_Quote)}';
        var Message_Check_Your_Clipboard = '{!JSENCODE($Label.Check_Your_Clipboard)}';
        var Error_Message3 = '{!JSENCODE($Label.Error_Message3)}';
        var Error_Message11 = '{!JSENCODE($Label.Error_Message11)}';
        var Error_Message29 = '{!JSENCODE($Label.Error_Message29)}';
        var Error_Message33 = '{!JSENCODE($Label.Error_Message33)}';
        var Error_Message34 = '{!JSENCODE($Label.Error_Message34)}';
        var Error_Message35 = '{!JSENCODE($Label.Error_Message35)}';
        var Error_Message36 = '{!JSENCODE($Label.Error_Message36)}';
        var Error_Message40 = '{!JSENCODE($Label.Error_Message40)}';
        var Confirm_ChangedAfterPrint = '打印后行信息有变化，是否继续操作（报价编码会变新）？';
        var Confirm_PriceRefresh = '报价作成后{!$Label.Price_Valid_Period}天,还没有更新过价格，需要执行{!$Label.Status_Update}？';
        var Confirm_saveBtn = '您选择了委托人员，确定只是保存吗（不进行委托）？';
        window.sfdcPage.appendToOnloadQueue(function() { calonLoad() });
        var openQuoteExcelImportWindow = null;
        
        function selectAll() {
            var checklist = j$("input[name='checklist']");
            var all = j$(escapeVfId("checkAll"));
            for(var i = 0; i < checklist.length; i++){
                if (all[0].checked == true) {
                    checklist[i].checked = true;
                } else {
                    checklist[i].checked = false;
                }
            }
        }
        
        function openpdf() {
            var qid = j$(escapeVfId("Page:mainForm:hiddenQuoid")).value();
            if (qid == '') {
                alert('请先做成消耗品样品。');
                return false;
            }
            if (window.confirm('是否新建SIS报告书？')) {
                try {
                    sforce.connection.sessionId = "{!GETSESSIONID()}";
                    var accid = j$(escapeVfId("Page:mainForm:hiddenHpid")).value();
                    var recordType = "SIS";
                    var repOwnerId = '{!$User.Id}';
                    
                    // 明細の同梱数を取得
                    var cntArray = new Array();
                    var result = sforce.connection.query("select Id, Product2__c, Product2__r.Packing_list_manual__c from ConsumableSampleLineItem__c where ConsumableSample__c =\'" + qid + "\'");
                    var records = result.getArray("records");
                    for (var i = 0; i < records.length; i++) {
                        var manualCnt = records[i].Product2__r.Packing_list_manual__c;
                        if (manualCnt == null || manualCnt <= 0) {
                            manualCnt = 1;
                        }
                        //if (records[i].Product2__r.Packing_list_manual__c != "") {
                        for (var j = 1; j <= manualCnt; j++) {
                            cntArray.push(records[i].Product2__c);
                        }
                        //}
                    }
                    
                    var pdfno = sforce.apex.execute("ControllerUtil", "selectCommonSequence", {valueField: 'EvaluationPDF_NextValue__c', formatField: 'EvaluationPDF_Format__c'});
                    // 報告書を複数作成
                    for (var i = 0; i < cntArray.length; i++) {
                        //新建OPD报告书
                        var rtn = sforce.apex.execute("Add_Report", "addReportOPWithEvaluationPDF", 
                            {repOwnerId: repOwnerId,
                             reportId: null,
                             dailyReportId: null,
                             eventId: null,
                             recordType: recordType,
                             aId: accid,
                             visitor1: null,
                             visitor2: null,
                             visitor3: null,
                             visitor4: null,
                             visitor5: null,
                             opp1: null,
                             opp2: null,
                             opp3: null,
                             opp4: null,
                             opp5: null,
                             reportDate: null,
                             evaluationPDFNumber: pdfno,
                             pro1: cntArray[i]
                            }
                        );
                    }
                    window.open('/apex/BeforeOPDPDF?csid=' + encodeURI(qid) + '&pdfNo=' + encodeURI(pdfno), 'BeforeOPDPDF');
                } catch(e) {
                    alert(e); 
                }
            } else {
                window.open('/apex/BeforeOPDPDF?csid=' + encodeURI(qid), 'BeforeOPDPDF');
            }
        }
    </script>
    <style type="text/css">
        td .dateFormat  {
            display: none;
        }
        
        div#iframelike {
            color: #fff;
            height: 300px;
            overflow: auto;
        }
        div#iframelikeheader {
            color: #fff;
            height: 23px;
            overflow: auto;
        }
        input {
            font-size: 10.5px;
        }
        body {
            font-size: 10.5px;
        }
        
        .visitorplace_results {
            border: 1px solid gray;
            background-color: white;
            padding: 0;
            margin: 0;
            list-style: none;
            position: absolute;
            z-index: 10000;
            display: none;
            overflow:auto;
            white-space:nowrap;
            width:400px;
            height:250px;
        }
        .visitorplace_results li {
            padding: 2px 5px 2px 0px;
            margin-left : 2px;
            color: #101010;
            text-align: left;
        }
    </style>

    <apex:form id="mainForm">
        <apex:outputText id="hiddenQuoid" value="{!quoid}" style="display:none;"/>
        <apex:outputText id="hiddenHpid" value="{!quo.Contact__r.AccountId}" style="display:none;"/>
        
        <apex:inputHidden id="changedAfterPrint" value="{!changedAfterPrint}"/>
        <apex:inputHidden id="productStatusUpdated" value="{!productStatusUpdated}"/>
        <apex:actionFunction action="{!excelImport}" name="excelImport" reRender="mainForm" oncomplete="unblockUI();calPriceAll();">
            <apex:param assignTo="{!excel_text}" name="select_index" value=""/>
        </apex:actionFunction>
        <apex:actionFunction action="{!Save}" name="Save" reRender="mainForm,message1" oncomplete="unblockUI();"/>
        <apex:actionFunction action="{!OppReflection}" name="OppReflection" reRender="mainForm,message1" oncomplete="unblockUI();"/>
        <apex:actionFunction action="{!ApprovalBtn}" name="ApprovalBtn" reRender="mainForm,message1" oncomplete="unblockUI();"/>
        <apex:pageBlock id="block">
            <apex:inputHidden value="{!quo.CurrencyIsoCode}" id="CurrencyIsoCode"/>
            <apex:inputHidden value="{!baseUrl}" id="baseUrl"/>
            <apex:outputPanel id="message1">
                <apex:messages styleClass="editListError"/>
            </apex:outputPanel>
            <apex:outputPanel rendered="{!errorflg}" >
                <table width="100%">
                    <tr>
                        <td align="left"><div class="errorMsg">{!errorMessage}</div></td>
                    </tr>
                </table>
            </apex:outputPanel>
            <apex:outputPanel rendered="{!Messageflg}" >
                <table width="100%">
                    <tr>
                        <td align="left">{!Message}</td>
                    </tr>
                </table>
            </apex:outputPanel>
            <div>
                <table border="0">
                    <tr>
                        <th style="text-align:right;width:100px;">{!$ObjectType.ConsumableSample__c.fields.Name.Label}</th>
                        <td style="text-align:left;width:110px;"><apex:outputField value="{!quo.Name}" style="width:100px;"/></td>
                        <th style="text-align:right;width:100px;">{!$ObjectType.Contact.fields.Hospital_name__c.Label}</th>
                        <td style="text-align:left;width:100px;"><apex:outputField value="{!quo.Contact__r.Hospital_name__c}" style="width:90px;"/></td>
                        <th style="text-align:right;width:100px;">{!$ObjectType.ConsumableSample__c.fields.Contact__c.Label}</th>
                        <td style="text-align:left;width:100px;">
                            <div class="requiredInput"><div class="requiredBlock"></div><apex:inputField value="{!quo.Contact__c}" style="width:75px;"/></div>
                        </td>
                        <th style="text-align:right;width:100px;">{!$ObjectType.ConsumableSample__c.fields.Expect_Arrive_Date__c.Label}</th>
                        <td style="text-align:left;width:100px;"><apex:inputField value="{!quo.Expect_Arrive_Date__c}" style="width:75px;"/></td>
                        <th style="text-align:right;width:100px;">{!$ObjectType.ConsumableSample__c.fields.Expect_Use_Date__c.Label}</th>
                        <td style="text-align:left;width:100px;"><apex:inputField value="{!quo.Expect_Use_Date__c}" style="width:75px;"/></td>
                    </tr>
                    <tr>
                        <th style="text-align:right;">{!$ObjectType.ConsumableSample__c.fields.Purpose__c.Label}</th>
                        <td style="text-align:left;" colspan="5"><apex:inputField value="{!quo.Purpose__c}" style="width:500px;"/></td>
                        <th style="text-align:right;">{!$ObjectType.ConsumableSample__c.fields.PIC__c.Label}</th>
                        <td style="text-align:left;"><apex:inputField value="{!quo.PIC__c}" style="width:75px;"/></td>
                    </tr>
                </table>
                <table border="0">
                    <tr>
                        <th style="width:40px;">&nbsp;</th>
                        <td style="width:150px;"></td>
                        <td style="width:150px;"><apex:commandButton onclick="openQuoteExcelImport(event);return false;" value="{!$Label.Excel_Import}" rerender="dummy"/></td>
                        <td style="text-align:right;width:80px;"><apex:commandButton rerender="dummy" id="Btn_RowDelete" onclick="radioChecker2('del');return false;" value="{!$Label.deleteLabel}" style="width:60px;"/></td>
                        <td style="width:150px;">
                            <apex:commandButton rerender="dummy" id="Btn_RowUp" onclick="radioChecker2('up');return false;" value="{!$Label.Row_Up}" style="width:60px;"/>
                            <apex:commandButton rerender="dummy" id="Btn_RowDown" onclick="radioChecker2('down');return false;" value="{!$Label.Row_Down}" style="width:60px;"/>
                        </td>
                        <th style="text-align:right;width:80px;">&nbsp;</th>
                        <th style="text-align:right;width:70px;">资产数量</th>
                        <td style="text-align:right;width:80px;"><apex:outputtext id="Asset_cnt" value="0" /></td>
                        <th style="text-align:right;width:70px;">耗材数量</th>
                        <td style="text-align:right;width:80px;"><apex:outputtext id="Consumable_cnt" value="0" /></td>
                        <th style="text-align:right;width:70px;">总计</th>
                        <td style="text-align:right;width:80px;">
                            <apex:outputtext id="Estimation_List_Price" value="{0, number, ###,##0.00}"><apex:param value="{!total_ListPrice}"/></apex:outputtext>
                            <apex:inputHidden id="hidden_Estimation_List" value="{!total_ListPrice}"/>
                        </td>
                    </tr>
                </table>
            </div>
            <table>
                <tr>
                    <td>&nbsp;</td>
                </tr>
            </table>
            <table style="width:1125px;" border="0">
                <tr>
                    <th style="text-align:center;width:20px;">&nbsp;<input type="checkbox" id="checkAll" onclick="selectAll()" style="width:15px;"/></th>
                    <th style="text-align:center;width:20px;">No</th>
                    <th style="text-align:center;width:150px;">{!$Label.Asset_No}</th>
                    <th style="text-align:center;width:100px;">{!$Label.SFDA_Status}</th>
                    <th style="text-align:center;width:100px;">Serial/Lot区分</th>
                    <th style="text-align:center;width:100px;">资产/耗材</th>
                    <th style="text-align:center;width:320px;">{!$Label.Product_Name}</th>
                    <th style="text-align:center;width:50px;">同捆数</th>
                    <th style="text-align:center;width:80px;">{!$Label.Quantity}</th>
                    <th style="text-align:center;width:85px;">ListPrice</th>
                    <th style="text-align:center;width:100px;">小计</th>
                </tr>
            </table>
            <div id="iframelike" style="width:1143px;">
                <input type="hidden" id="ListPriceTotal" value="0" />
                <input type="hidden" id="UnitPriceTotal" value="0" />
                <apex:pageblocktable value="{!activities}" var="s" id="lists" style="width:1125px;">
                    <apex:column style="width:20px;" >
                        <input type="checkbox" name="checklist" value="{!s.lineNo}" style="width:15px;"/>
                    </apex:column>
                    <apex:column style="width:20px;text-align:right;" >
                        <apex:outputLabel id="indexNo" value="{!IF(s.PageObject.Product2__c==null,null,s.lineNo + 1)}" style="width:15px;"/>
                    </apex:column>
                    <apex:column style="text-align:center;width:150px;">
                        <apex:inputText id="Assert" style="width:120px;" value="{!s.Asset_Model}" onclick="searchProduct('{!s.lineNo}',this.value)" />
                    </apex:column>
                    <apex:column style="width:100px;">
                        <apex:outputField style="width:90px;" id="Status__c" value="{!s.PageObject.SFDA_Status__c}"/>
                        <apex:inputHidden id="SFDA" value="{!s.PageObject.SFDA_Status__c}"/>
                    </apex:column>
                    <apex:column style="width:100px;">
                        <apex:outputText style="width:90px;" id="Serial_Lot" value="{!s.Serial_Lot}"/>
                    </apex:column>
                    <apex:column style="width:100px;">
                        <apex:outputText style="width:90px;" id="Asset_loaner_category" value="{!s.Asset_loaner_category}"/>
                    </apex:column>
                    <apex:column style="width:320px;">
                        <div id="Page:mainForm:block:lists:{!s.lineNo}:NameLink"><apex:outputLink style="width:300px;" value="{!baseUrl}/{!s.PageObject.Product2__c}" id="Nametext1" target="_blank">{!s.PageObject.Name__c}</apex:outputLink></div>
                        <apex:inputHidden id="Name__c" value="{!s.PageObject.Name__c}"/>
                    </apex:column>
                    <apex:column style="width:50px;text-align:right;">
                        <apex:outputText style="width:45px;" id="Packing_list_manual" value="{!s.Packing_list_manual}"/>
                    </apex:column>
                    <apex:column style="text-align:center;width:80px;">
                        <apex:inputField id="Quantity" style="width:50px;text-align:right;" value="{!s.PageObject.Quantity__c}" onChange="calPrice('{!s.lineNo}')" />
                        <script type="text/javascript">
                            j$(escapeVfId('Page:mainForm:block:lists:'+ {!s.lineNo} + ':Quantity')).attr('readonly',true);
                        </script>
                    </apex:column>
                    <apex:column style="width:85px;text-align:right;">
                        <apex:outputPanel layout="none" rendered="{!$ObjectType.ConsumableSampleLineItem__c.fields.ListPrice__c.accessible}" >
                            <apex:outputText style="width:85px;" id="ListPricetext" value="{0, number, ###,##0.00}">
                                <apex:param value="{!s.ListPrice_Page}" />
                            </apex:outputText>
                        </apex:outputPanel>
                        <apex:inputHidden id="ListPrice" value="{!s.ListPrice_Page}"/>
                        <apex:outputPanel layout="none" rendered="{!!$ObjectType.ConsumableSampleLineItem__c.fields.ListPrice__c.accessible}" >
                            <span style="width:85px;text-align:right;" id="Page:mainForm:block:lists:{!s.lineNo}:ListPricetext">{!IF(s.PageObject.Product2__c == null, ' ', 0.00)}</span>
                            <script type="text/javascript">
                                j$(escapeVfId('Page:mainForm:block:lists:'+ {!s.lineNo} + ':ListPrice')).val(toNum(0));
                            </script>
                        </apex:outputPanel>
                    </apex:column>
                    <apex:column style="width:100px;text-align:right;">
                        <apex:outputPanel layout="none" rendered="{!$ObjectType.ConsumableSampleLineItem__c.fields.ListPrice__c.accessible}" >
                            <apex:outputText style="width:80px;" id="ListPriceTotalText" value="{0, number, ###,##0.00}">
                                <apex:param value="{!s.ListPriceTotal_Page}" />
                            </apex:outputText>
                        </apex:outputPanel>
                        <apex:inputHidden id="ListPriceTotal" value="{!s.ListPriceTotal_Page}"/>
                        <apex:outputPanel layout="none" rendered="{!!$ObjectType.ConsumableSampleLineItem__c.fields.ListPrice__c.accessible}" >
                            <span style="width:80px;text-align:right;" id="Page:mainForm:block:lists:{!s.lineNo}:ListPriceTotalText">{!IF(s.PageObject.Product2__c == null, ' ', 0.00)}</span>
                            <script type="text/javascript">
                                j$(escapeVfId('Page:mainForm:block:lists:'+ {!s.lineNo} + ':ListPriceTotal')).val(toNum(0));
                            </script>
                        </apex:outputPanel>
                        <apex:inputHidden id="Product_Id" value="{!s.PageObject.Product2__c}"/>
                        <apex:inputHidden id="lineNo" value="{!s.lineNo}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </div>

            <BR></BR>
            <table border="0">
                <tr>
                    <th style="width:650px;" align="left">
                        {!$ObjectType.ConsumableSample__c.fields.Reason__c.Label}<br/>
                        <apex:inputField value="{!quo.Reason__c}" style="width:650px;height:55px;"/>
                    </th>
                    <td>
                        <table border="0">
                            <tr>
                                <th style="width:15px">&nbsp;</th>
                                <td style="width:100px;" align="right"><apex:commandButton onclick="save2btn();return false;" reRender="IraiUserId,hiddenQuoid,message1" value="{!$Label.Save_Button}" style="width:90px;" disabled="{!Save_button}"/></td>
                                <td style="width:100px;" align="right"><apex:commandButton onclick="oppReflection2btn();return false;" rerender="IraiUserId,hiddenQuoid,message1" value="{!$Label.Opp_Button}" style="width:90px;" disabled="{!Save_button}"/></td>
                                <td style="width:100px;" align="right"><apex:commandButton onclick="approval2btn();return false;" reRender="IraiUserId,hiddenQuoid,message1" value="提交申请" style="width:90px;" disabled="{!Save_button}"/></td>
<!--                                 <td style="width:100px;" align="right"><apex:commandButton onclick="openpdf();return false;" value="PDF" style="width:90px;" disabled="{!pdf_button}"/></td> -->
                                <td style="width:100px;" align="right"><apex:commandButton onclick="openpdf();return false;" value="PDF" style="width:90px;" /></td>
                                <td style="width:100px;" align="right"><apex:commandButton action="{!Back}" rerender="IraiUserId,hiddenQuoid,message1" value="不保存（返回）" style="width:90px;"/></td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </apex:pageBlock>
    </apex:form>
</apex:page>