/**
 * 加载门店页面函数
 */
const path=require('path');
const log = require(".."+path.sep+"setting"+path.sep+"set_log").clog;
const electron = require('electron');
const screen = electron.screen;
const app = electron.app;
const ipcMain=electron.ipcMain;
const BrowserWindow = electron.BrowserWindow;
const process = require('child_process');
const print_monitor=require(".."+path.sep+"setting"+path.sep+"print_monitor");
const finger_monitor=require(".."+path.sep+"setting"+path.sep+"finger_monitor");

let pharmacy={
  createPhamWindow:function (apppath,respath,main,url,updateHandle){
    //阻止应用多开
    const gotTheLock = app.requestSingleInstanceLock();
    if (!gotTheLock) {
      app.quit();
    }
    log.info("加载门店客户端窗口...");
    let win = new BrowserWindow({
      //直接设置最大宽高
      width: (screen.getPrimaryDisplay().workAreaSize.width/4)*3,
      height: (screen.getPrimaryDisplay().workAreaSize.height/4)*3,
      title: "钜星科技便民问诊系统 版本 2.0",
      frame: false,
      show: false,
      icon: respath +path.sep+"static"+path.sep+"images"+path.sep+"justar.ico",
      webPreferences: {
        nodeIntegration: true,
        //允许跨域请求
        webSecurity: false,
        //允许远程调用
        enableRemoteModule: true,
        //防止require引入失败
        contextIsolation:false,
        //禁止远程页面调用electron框架的window.open
        nativeWindowOpen: true
      }
    });
    //窗口最大化
    win.maximize();
    //使窗口获得焦点
    win.show()
    main.registerDevTool(win);
    win.loadURL(url);
    win.setMenu(null);
    main.taskbarStar(win);
    main.winTop(win);

    //开启指纹登录监听程序
    finger_monitor.runFingerMonitor(respath);

    //开启打印列表页面监听
    print_monitor.openPrinterMonitor(win,apppath,respath,main);
    //
    ipcMain.on("getSerialNumber",function (event, args){
      process.exec('wmic bios get serialnumber',function (error, stdout, stderr) {
        if (error !== null) {
          log.error("获取本机SN码失败,异常如下："+error);
        }else{
          let serial=stdout.replaceAll("SerialNumber","");
          serial=serial.replace(/[\r\n]/g,"");
          serial=serial.replace(/\ +/g,"");
          event.returnValue=serial;
          /*win.webContents.executeJavaScript("let pharmacy_sn='"+serial+"';");*/
        }
      });
    });

    //监听最后一个窗口被关闭时,退出程序
    win.on("close", (e) => {
      e.preventDefault();
      main.exitSoft(respath);
    });
    updateHandle(win);
  }
}

module.exports=pharmacy;