<apex:page showHeader="true" sidebar="true" id="allPage" lightningStylesheets="true" title="web to app">
<apex:stylesheet value="{!URLFOR($Resource.blockUIcss)}"/>
<apex:includeScript value="{!URLFOR($Resource.jquery183minjs)}"/>
<apex:includeScript value="{!URLFOR($Resource.PleaseWaitDialog)}"/>
<apex:includeScript value="{!URLFOR($Resource.instascan)}"/>
<apex:includeScript value="{!URLFOR($Resource.connection20)}"/>
<apex:includeScript value="{!URLFOR($Resource.apex20)}"/>
<!-- 20230914 ljh add -->
<style type="text/css">
    table.mytable{
        margin-left: 15px;
    }
    
    table.mytable tr{
        height:40px;
    }
</style>
<script>
sforce.connection.sessionId = "{!$Api.Session_ID}";
function go() {
    var id = j$(escapeVfId('id')).value();
    if (id == '') {
        alert('请输入申请单编码／主体条码');
    } else {
        var type = j$("input[name='scanType']:checked").val();
        if (type == 'type1') {
            var raList = sforce.connection.query("select Id, OwnerId, ToAgency__c, Rental_Apply_Equipment_Set_Cnt__c, RecordType.DeveloperName, Shippment_ng_num__c, Wei_Return__c from Rental_Apply__c where Name = '"+id+"'");
            raList = raList.getArray("records");
            if(raList.length == 0) {
                location.href ="/apex/EquipmentSetSRList2?id=" + id;
            }
            else if(raList[0].RecordType.DeveloperName == 'AgencyRequest') {
                // 借出申请里的分配办事处实际存的是管理省
                var workProvince = getUserProvince(raList[0].OwnerId);
                if(workProvince == '' || workProvince == undefined) {
                    alert('找不到用户的OCSM管理省!');
                    return;
                }
                var isSameCity = isMatch(workProvince, raList[0].ToAgency__c);
                // 未发货
                if(raList[0].Shippment_ng_num__c > 0) {
                    location.href ="/apex/RentalFixtureSetAssignAgency?pt_recid=" + raList[0].Id;
                }
                // 已发货同城
                else if(isSameCity) {
                    location.href ="/apex/RentalAgencyReceived?id=" + raList[0].Id;
                }
                // 已发货非同城
                else {
                    // 非同城全未回寄
                    if(raList[0].Rental_Apply_Equipment_Set_Cnt__c == raList[0].Wei_Return__c) {
                        location.href ="/apex/EquipmentSetSRList2?id=" + id;
                    }
                    // 非同城有已回寄
                    else {
                        location.href ="/apex/RentalAgencyReceived?id=" + raList[0].Id;
                    }
                }
            }
            else {
                location.href ="/apex/EquipmentSetSRList2?id=" + id;
            }
        }
        else if(type == 'type2') {
            location.href ="/apex/ConsumApplyEquipmentSetSRList?id=" + id;
        }
        else if(type == 'type3') {
            var taList = sforce.connection.query("select Id, OwnerId, From_Location__c, Destination_location__c, RecordType.DeveloperName from TransferApply__c where Name ='"+id+"'");
            taList = taList.getArray("records");
            var profileId = "{!$User.ProfileId}";
            var isCenterStaff = "{!$Label.ProfileId_EquCenAdmin}".match(profileId)
                    || "{!$Label.ProfileId_EquCenCheckAndDepot}".match(profileId)
                    || "{!$Label.ProfileId_EquipmentCenter}".match(profileId);

            if(taList.length == 0 || isCenterStaff) {
                location.href ="/apex/TransferEquipmentSetSRList?id=" + id;
            }
            else {
                // 备品中心到办事处，用户所在管理省=调拨至地区，则打开办事处上架画面
                if(taList[0].RecordType.DeveloperName == 'CenterToAgency'){
                    location.href ="/apex/TransferAgencyReceived?id=" + taList[0].Id;
                }
                // 办事处到备品中心，用户所在管理省=调拨前地区，则打开办事处下架画面
                else if(taList[0].RecordType.DeveloperName == 'AgencyToCenter'){
                    location.href ="/apex/TransferAgencyShipment?id=" + taList[0].Id;
                }
                // 其它则打开备品中心画面
                else {
                    location.href ="/apex/TransferEquipmentSetSRList?id=" + id;
                }
            }
        }
    }
}
// 获取用户的OCSM管理省
function getUserProvince(userId) {
    var userList = sforce.connection.query("select OCM_man_province_Rental__c from User where Id='" + userId + "'");
    userList = userList.getArray("records");
    if(userList.length == 0) {
        return '';
    }
    else {
        return userList[0].OCM_man_province_Rental__c;
    }
}
var provinceMap;
// 省转办事处
function getAgency(workProvince) {
    if(provinceMap == undefined) {
        provinceMap = new Map();
        var strList = "{!($Label.OCM_Management_Province_Mapping)}".split(',');
        for(var i = 0 ; i < strList.length ; i++) {
            //省->办事处
            provinceMap.set(strList[i].split(':')[0].trim(), strList[i].split(':')[1].trim());
        }
    }
    if(provinceMap.has(workProvince)) {
        return provinceMap.get(workProvince);
    }
    return workProvince;
}
function isMatch(workProvince, destination) {
    if(workProvince == null || destination == null) {
        return false;
    }
    var loc = destination.split(' ')[0];
    return workProvince.startsWith(loc) || getAgency(workProvince).startsWith(loc);
}
</script>
<!-- 20230914 ljh  -->
<!-- <table> -->
<table class="mytable">
    <!-- <tr>
        <td>出库/入库 ： </td>
        <td>
            <select id="type" name="chooseType" size="1">
                <option value="-" selected="selected">无</option>
                <option value="Received" >下架</option>
                <option value="Shippment">出库检测</option>
                <option value="Received" >发货</option>
                <option value="Received" >回寄</option>
                <option value="Received" >回收检测</option>
                <option value="Received" >上架</option>
            </select>
        </td>
    </tr> -->
    <tr>
        <td colspan="2">
            <video playsinline="true" id="preview" style="width: 300px;z-index: 20;transform: scaleX(-1);margin-top: 20px;"></video>
        </td>
    </tr>
    <tr>
        <td>备品借出/耗材借出/调拨： </td>
        <td>
            <input type="radio" id="contactChoice1" name="scanType" value="type1" checked="checked" /><label for="contactChoice1">备品借出</label>
            <input type="radio" id="contactChoice2" name="scanType" value="type2"/><label for="contactChoice2">耗材借出</label>
            <input type="radio" id="contactChoice3" name="scanType" value="type3"/><label for="contactChoice3">调拨</label>
        </td>
    </tr>
    <tr>
        <td>申请单编码／主体条码 ： </td>
        <td><input id="id" type="text" name="id" autofocus="autofocus" /></td>
    </tr>
    <tr>
        <td><input type="button" class="btn" value="确定" onclick="go();" /></td>
    </tr>
</table>
<!-- <div>
    <div>

        <div>
            code:<input type="text" id="code"/>
            <br/>
            <button>确定</button>
        </div>

    </div>
</div> -->
<script type="text/javascript">
    j$(document).ready(function(){
        document.get
        let scanner = new Instascan.Scanner({ video: document.getElementById('preview'), scanPeriod: 5 ,mirror: false});
        scanner.addListener('scan', function (content) {
          j$("#id").val(content);
          go();
        });
        Instascan.Camera.getCameras().then(function (cameras) {
          if (cameras.length > 0) {
            let ver = '';

            try {
                ver = (navigator.userAgent).match(/OS (\d)?\d_\d(_\d)?/i)[0].split('_')[0].replace("OS ","");
            }
            catch{}
            if (ver >= 13) {
                scanner.start(cameras[1]);
            } else {
                // scanner.start(cameras[0]);
                // 20231105 zq 设置摄像头 start
                if(cameras.length > 1){
                    scanner.start(cameras[1]);
                }else{
                    scanner.start(cameras[0]);
                }
                // 20231105 zq 设置摄像头 end
            }
          } else {
            console.error('No cameras found.');
          }
        }).catch(function (e) {
          console.error(e);
        });
    });
</script>
</apex:page>