# app_script_history

id: 60ac6cb75db2f5d22859c49d
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:28:49.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            putImageBackToPool ( data.id);
            return {success:true};
        }
    }
    return  {success:true};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id ,verified)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action=  ( verified ==1 )?"edit":"view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action=action; //控制是否显示删除和新增按钮
    FJ.buildGridly( getPZFJ(id));
}


id: 60ac6cb75db2f5d22859c4a3
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:29:47.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            putImageBackToPool ( data.id);
            return {success:true};
        }
    }
    return  {success:true};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id ,verified)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action=  ( verified ==0 )?"edit":"view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action=action; //控制是否显示删除和新增按钮
    FJ.buildGridly( getPZFJ(id));
}


id: 60ac6cb75db2f5d22859c4a9
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:53:04.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            return  putImageBackToPool ( data.id);
            
        }
    }
    return  {success:true};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id ,verified)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action=  ( verified ==0 )?"edit":"view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action=action; //控制是否显示删除和新增按钮
    FJ.buildGridly( getPZFJ(id));
}


id: 60ac6cb75db2f5d22859c4b1
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:53:26.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



function putImageBackToPool( id )
{

}


id: 60ac750d5db2f5d22859c4b9
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 11:54:54.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


id: 60ac750d5db2f5d22859c518
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:04:12.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var id= p.id;  // dzpz_pzfj.id 
    
    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid= JSON.parse(t).gguid;


    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //待归档的图片池
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");

        
    var row= dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow( dzpz_pzfj_ds , dzpz_imgpool_fileupload_ds , row);

        //数据调整

  
        dzpz_imgpool_fileupload_ds.setValue(row,"groupname", '附件');  //把类型改成 凭证
        
        dzpz_imgpool_fileupload_ds.setValue(row,"gguid", gguid);
        dzpz_imgpool_fileupload_ds.setValue(row,"tablename", "dzpz_imgpool");
        dzpz_imgpool_fileupload_ds.setValue(row,"corporationid", null);

        //删除源
        //当 
        dzpz_pzfj_ds.deleteRow(0);
 

    var err =  ''+updateAll(  dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow(  dzpz_pzfj_ds , dzpz_imgpool_fileupload_ds , row)
{
    for( var i=0;i<dzpz_pzfj_ds.getColumnCount();i++)
    {
        var col= dzpz_pzfj_ds.getColumnName(i);
        var value= dzpz_pzfj_ds.getValue(0 , i);
        dzpz_imgpool_fileupload_ds.setValue(row, col , value);  
    }
}


id: 60ac77825db2f5d22859c520
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:05:23.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    var p = JSON.parse(arg);
    println(p.rawdata);
    var id = p.id;  // dzpz_pzfj.id 

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac77825db2f5d22859c533
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:11:15.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


id: 60ac77825db2f5d22859c549
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:15:00.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

println( arg); 
    var p = JSON.parse(arg);
    println(p.rawdata);
    var id = p.id;  // dzpz_pzfj.id 

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac77825db2f5d22859c551
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:16:11.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pzfj.id 
    println( "id="+ id);

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" id='"+ id+"' ");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac77825db2f5d22859c557
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 12:37:44.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            var ret= putImageBackToPool ( data.id);
            if( ret.success) cb_refresh_onClick();
            
        }
    }
    return  {success:true};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id ,verified)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action=  ( verified ==0 )?"edit":"view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action=action; //控制是否显示删除和新增按钮
    FJ.buildGridly( getPZFJ(id));
}


id: 60ac77825db2f5d22859c55d
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:25:54.000
script:
var pathToMonit = "";//当前监控的目录
var electron=null; 


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期",{corporationid:corporationid} ,function(ym)
    {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete="确信要将图片从当前凭证中移除，并放回到图片池中吗?";
 
    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage" , function(msg){
        msg= JSON.parse(msg);
        if( msg.cmd=="newFileUploaded")   newImgCommingTipVisible(true); //通知有新文件上传了
        if( msg.cmd=="notifyNeedUploadFileCount")  notifyNeedUploadFileCount( msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron= getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);

 
    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout( function(){
    if (electron != null) {
        var hostName = electron.getHostName();
        pathToMonit = getMonitePath(hostName);
        
        //开启监控
        if( pathToMonit!='') moniteStart(pathToMonit);
    }
    },500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function  saveSelectionAndPZInfo( )
{
     
    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection=selection;
    g_pzMap=pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function  restoreSelectionAndPZInfo()
{
     var selection= g_selection;
     var pzhMap= g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

     //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();
   
    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet , name ,data ) {

    if( name=='FJ')
    {
        if( data.groupname=='凭证')
        {
            return { success:false, message:"这是凭证，不允许这样移除，请点击“删除凭证”"};
        }else
        {
            var ret= putImageBackToPool ( data.id);
            if( ret.success) cb_refresh_onClick();
            
        }
    }
    return  {success:true};
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet , name ) {

   

    if( name=='FJ')
    {
          var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

   
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh!='')
        {
            toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
            return;
        }
    }
 


    }

    
    return { success:false, message:"禁止上传"};
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH( id)
{
    let obj= find_pzh( id , false);
    if( obj==null) return; 
    let s= obj.text();
    s=s.replace(/\s/g ,'');
    return s; 
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);
 
    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path)
{
    pathToMonit=path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

     if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit+ " 监控中...");
    electron.ipcSendMessage( {cmd:"moniteStart" , path:path});
}

function cb_refresh_onClick()
{
    newImgCommingTipVisible(false);

     // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit( restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");
   
     
    
}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v)
{
    book.sheet0.getBrick('shineText').visible=v;
 
}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount( fc)
{
    var info=  fc==0?"": "【"+fc+"】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick()
{

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if( selection.length==0)
    {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup=[];
    var lastPZ=null; 
    for( let i=0;i< selection.length;i++)
    {
        var one= selection[i];
        var pzh = getPZH( one.id);
        if( pzh=='')
        {
            if( lastPZ !=null)
            {
                lastPZ.fj.push( one.id);
            }
        }else{
            lastPZ={};
            lastPZ.pzh= pzh; 
            lastPZ.imgID= one.id; 
            lastPZ.fj=[];
            lastPZ.fj.push( one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push( lastPZ); 
        }
    }

    if( pzGroup.length==0)
    {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir( pzGroup);

    $confirm("提示","确信要归档"+pzGroup.length+"张凭证吗?", function()
    {
        do_pzgd( pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function  do_pzgd( pzGroup)
{
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档", 
    {
        corporationid: book.sheet2.cells("corporationid").getValueString(),
        ym: book.sheet2.cells("ym").getValueString(),
        pzGroup:pzGroup 
     } , function()
    {

        hideInfoPane();
        setButtonsStatus(true); //启用一些按钮点击
        smartRetrieve( book.sheet2 , "dzpz_pz"); //刷新凭证列表
        //重新检索图片区
        cb_refresh_onClick();

    });
}

function setButtonsStatus(b)
{
    book.sheet0.getBrick('cb_fj').enabled=b;
    book.sheet0.getBrick('cb_auto').enabled=b;       
}

function loadPZ( id ,verified)
{
    book.sheet3.cells("id").setValue(id);
    smartRetrieve( book.sheet3,"one"); 
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action=  ( verified ==0 )?"edit":"view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action=action; //控制是否显示删除和新增按钮
    FJ.buildGridly( getPZFJ(id));
}


id: 60ac77825db2f5d22859c563
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:30:04.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) cb_refresh_onClick();

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false}; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false}; // 此返回，阻止了默认的上传操作
            }
        }

        var ret= addImageToPZ( $one_ds.getString(0,"id") , selection);
        if( ret.success)
        {
            //刷新图片区
            cb_refresh_onClick(); 
            //刷新凭证
            loadPZ( $one_ds.getString(0,"id") , $one_ds.getString(0,"verifiername")!=""?1:0 );
        }

    }


    return { success: false}; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


id: 60ac77825db2f5d22859c56b
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:31:50.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


function addImageToPZ( pzid , images)
{
    return  JSON.parse( runTask("",{pzid:pzid, images:images}));
}


id: 60ac77825db2f5d22859c573
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:32:07.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


//@comment (group=,caption=追加图片到凭证中,needRightControl=false)
function addImageToPZ( pzid , images)
{
    return  JSON.parse( runTask("",{pzid:pzid, images:images}));
}


id: 60ac77825db2f5d22859c57b
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:32:18.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


//@comment (group=,caption=追加图片到凭证中,needRightControl=false)
function addImageToPZ( pzid , images)
{
    return  JSON.parse( runTask("追加图片到凭证中",{pzid:pzid, images:images}));
}


id: 60ac8bf55db2f5d22859c633
scriptid: 60ac8be75db2f5d22859c588
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:40:10.000
script:
/*

   凭证主记录已存在 ， 向凭证中添加更多的附件
   

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var id= p.pzid;
    var images= p.images;


    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.retrieve(" id='"+  id+"' ");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //用来检索数据
    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    //用来更新数据
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");
    
 
    var corporationid= dzpz_pz_ds.getValue(0,"corporationid");
     
    // 附件清单
    for( var i=0;i< images.length;i++)
    {
        var fromID = images[i].id;
        
        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整
  
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        //当 
        dzpz_imgpool_fileupload_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupload where id='"+fromID+"' ");

        
    }

    //更新凭证附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ pzid+"' ) where id='"+ pzid+"' ");

    var err =  ''+updateAll( dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ac8f625db2f5d22859c649
scriptid: 60ac8be75db2f5d22859c588
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:47:15.000
script:
/*

   凭证主记录已存在 ， 向凭证中添加更多的附件
   

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var id= p.pzid;
    var images= p.images;


    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.retrieve(" id='"+  id+"' ");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //用来检索数据
    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    //用来更新数据
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");
    
 
    var corporationid= dzpz_pz_ds.getValue(0,"corporationid");
     
    // 附件清单
    for( var i=0;i< images.length;i++)
    {
        var fromID = images[i].id;
        
        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整
  
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        //当 
        dzpz_imgpool_fileupload_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupload where id='"+fromID+"' ");

        
    }

    //更新凭证附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ pzid+"' ) where id='"+ id+"' ");

    var err =  ''+updateAll( dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ac8f625db2f5d22859c651
scriptid: 60ac8be75db2f5d22859c588
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:48:05.000
script:
/*

   凭证主记录已存在 ， 向凭证中添加更多的附件
   

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var id= p.pzid;
    var images= p.images;


    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.retrieve(" id='"+  id+"' ");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //用来检索数据
    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    //用来更新数据
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");
    
 
    var corporationid= dzpz_pz_ds.getValue(0,"corporationid");
     
    // 附件清单
    for( var i=0;i< images.length;i++)
    {
        var fromID = images[i].id;
        
        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整
  
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        //当 
        dzpz_imgpool_fileupload_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupload where id='"+fromID+"' ");

        
    }

    //更新凭证附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ id+"' ) where id='"+ id+"' ");

    var err =  ''+updateAll( dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ac8f625db2f5d22859c65d
scriptid: 60ac8be75db2f5d22859c588
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:54:50.000
script:
/*

   凭证主记录已存在 ， 向凭证中添加更多的附件
   

*/

function main(arg)
{
   
    var p= JSON.parse(arg);
    println( p.rawdata);
    var id= p.pzid;
    var images= p.images;


    var  dzpz_pz_ds = newDataStore("pz", "select * from dzpz_pz ");  //凭证主记录
    dzpz_pz_ds.retrieve(" id='"+  id+"' ");

    var  dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id","*" ,"id");

    //用来检索数据
    var  dzpz_imgpool_ds= newDataStore("","select * from dzpz_imgpool_fileupload");

    //用来更新数据
    var  dzpz_imgpool_fileupload_ds= newDataStore("","select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id","*" ,"id");
    
 
    var corporationid= dzpz_pz_ds.getValue(0,"corporationid");
     
    // 附件清单
    for( var i=0;i< images.length;i++)
    {
        var fromID = images[i].id;
        
        dzpz_imgpool_ds.retrieve(" id='"+fromID+"'");
        if( dzpz_imgpool_ds.getRowCount()!=1)
        {
            println( config.fj[i]+" 对应的上传记录不存在")
            continue; 
        }

        var row= dzpz_pzfj_ds.insertRow(9999);

        cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row);

        //数据调整
  
        dzpz_pzfj_ds.setValue(row,"gguid", id);
        dzpz_pzfj_ds.setValue(row,"tablename", "dzpz_pz");
        dzpz_pzfj_ds.setValue(row,"corporationid", corporationid);

        //删除源
        //当 
        dzpz_imgpool_fileupload_ds.executeAfterUpdate( " delete from dzpz_imgpool_fileupload where id='"+fromID+"' ");

        
    }

    //更新凭证附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ id+"' ) -1  where id='"+ id+"' ");

    var err =  ''+updateAll( dzpz_pzfj_ds ,dzpz_imgpool_fileupload_ds);

    if( err=='') return  {success:true};
    println( err);
    return {success:false, message:err}; 

}

function cloneRow( dzpz_imgpool_ds , dzpz_pzfj_ds , row)
{
    for( var i=0;i<dzpz_imgpool_ds.getColumnCount();i++)
    {
        var col= dzpz_imgpool_ds.getColumnName(i);
        var value= dzpz_imgpool_ds.getValue(0 , i);
        dzpz_pzfj_ds.setValue(row, col , value);  
    }
}


id: 60ac8f625db2f5d22859c673
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:55:52.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pzfj.id 
    println( "id="+ id);

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" id='"+ id+"' ");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);

    //更新附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ id+"' ) -1  where id='"+ id+"' ");


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac8f625db2f5d22859c679
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:57:44.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success)
            {
                 smartRetrieve(book.sheet3, "one");   
                cb_refresh_onClick();
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false}; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false}; // 此返回，阻止了默认的上传操作
            }
        }

        var ret= addImageToPZ( $one_ds.getString(0,"id") , selection);
        if( ret.success)
        {
            //刷新图片区
            cb_refresh_onClick(); 
            //刷新凭证
            loadPZ( $one_ds.getString(0,"id") , $one_ds.getString(0,"verifiername")!=""?1:0 );
        }

    }


    return { success: false}; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


id: 60ac8f625db2f5d22859c67f
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 13:57:47.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


id: 60ac8f625db2f5d22859c687
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:00:40.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pzfj.id 
    println( "id="+ id);

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" id='"+ id+"' ");
    var pzid = dzpz_pzfj_ds.getStrig(0,"gguid");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);

    //更新附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ pzid+"' ) -1  where id='"+ pzid+"' ");


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac8f625db2f5d22859c69d
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:02:13.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pzfj.id 
    println( "id="+ id);

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" id='"+ id+"' ");
    var pzid = dzpz_pzfj_ds.getString(0,"gguid");

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);

    //更新附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ pzid+"' ) -1  where id='"+ pzid+"' ");


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac8f625db2f5d22859c6a3
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:05:53.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').enabled= verified==0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


id: 60ac8f625db2f5d22859c6a9
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:07:10.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').enable= verified==0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


id: 60ac944d5db2f5d22859c6af
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:08:14.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible=false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible= verified==0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


id: 60ac944d5db2f5d22859c6b5
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:42:39.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible=false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible= verified==0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


function cb_delete_pz_onClick()
{
    var id=$one_ds.getString(0,"id");
    var ret= deletePZAndPutBackToPool( id);

}


id: 60ac944d5db2f5d22859c6bb
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:47:29.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible=false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible= verified==0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick()
{
    book.sheet3.getBrick('cb_delete_pz').visible=false;
    var id=$one_ds.getString(0,"id");
    var ret= deletePZAndPutBackToPool( id);
    if( ret.success)
    {
           $one_ds.reset();
            var FJ = book.sheet3.getBrick('FJ');
            FJ.config.action = 'view'; 
            FJ.buildGridly([]);
           cb_refresh_onClick();

    }else{
        toastr.error( ret.message);
    }

}


id: 60ac944d5db2f5d22859c6c3
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:49:36.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


//@comment (group=,caption=追加图片到凭证中,needRightControl=false)
function addImageToPZ( pzid , images)
{
    return  JSON.parse( runTask("追加图片到凭证中",{pzid:pzid, images:images}));
}

function deletePZAndPutBackToPool(id)
{
     return  JSON.parse( runTask("删除凭证所有图片回归池中", {id:id} ));
}


id: 60ac944d5db2f5d22859c6c9
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:50:18.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();

    } else {
        toastr.error(ret.message);
    }

}


id: 60ac944d5db2f5d22859c6cf
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:50:35.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }

}


id: 60ac9eb65db2f5d22859c7a6
scriptid: 60ac944d5db2f5d22859c6de
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:57:49.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pz.id 
    println( "id="+ id);

    if( getIntFromSQL2("pz" ,"select  verifierid  from dzpz_pz  where id='"+id+"' ")>0) return { success:false, message:'凭证已审核，禁止删除'};
    
    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" gguid='"+ id+"' "); //检索出所有图片
    

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");

    var RC= dzpz_pzfj_ds.getRowCount();
    for ( var i= RC -1; i>=0;i--)
    {
    
    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);
    cloneRow(dzpz_pzfj_ds, i ,  dzpz_imgpool_fileupload_ds, row);

    //数据调整

    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证
    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);
    //删除源
    dzpz_pzfj_ds.deleteRow(i);
    }

    //删除凭证
    dzpz_pzfj_ds.executeAfterUpdate(" delete from  dzpz_pz  where id='"+ id+"' ");

    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac9eb65db2f5d22859c7ae
scriptid: 60ac944d5db2f5d22859c6de
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 14:59:30.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pz.id 
    println( "id="+ id);

    if( getIntFromSQL2("pz" ,"select  verifierid  from dzpz_pz  where id='"+id+"' ")>0) return { success:false, message:'凭证已审核，禁止删除'};

    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" gguid='"+ id+"' "); //检索出所有图片
    

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");

    var RC= dzpz_pzfj_ds.getRowCount();
    for ( var i= RC -1; i>=0;i--)
    {
    
    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);
    cloneRow(dzpz_pzfj_ds, i ,  dzpz_imgpool_fileupload_ds, row);

    //数据调整

    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证
    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);
    //删除源
    dzpz_pzfj_ds.deleteRow(i);
    }

    //删除凭证
    dzpz_pzfj_ds.executeAfterUpdate(" delete from  dzpz_pz  where id='"+ id+"' ");

    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, sr , dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(sr , i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60ac9eb65db2f5d22859c7c4
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 15:00:22.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pzfj.id 
    println( "id="+ id);

   
    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" id='"+ id+"' ");
    var pzid = dzpz_pzfj_ds.getString(0,"gguid");

    if( getIntFromSQL2("pz" ,"select  verifierid  from dzpz_pz  where id='"+pzid+"' ")>0) return { success:false, message:'凭证已审核，禁止删除'};
    

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);

    //更新附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ pzid+"' ) -1  where id='"+ pzid+"' ");


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60aca1225db2f5d22859c7da
scriptid: 60ac750d5db2f5d22859c4c0
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 15:02:58.000
script:
/*
凭证中的某个附件移除，并还原到待归档图片池中
  1 记录移除
  2 移到 待归档图片池中
  3  几个标记数据还愿

*/

function main(arg) {

    println( arg); 
    var p = JSON.parse(arg);
    var id = p.id;  // dzpz_pzfj.id 
    println( "id="+ id);

   
    //得到当前操作员待归档图片对应的GGUID
    var t = runTask("get_dzpz_imgpool_id_of_current_user", {});
    var gguid = JSON.parse(t).gguid;


    var dzpz_pzfj_ds = newDataStore("pz", "select * from dzpz_pzfj "); //凭证附件
    dzpz_pzfj_ds.setUpdateProperty("dzpz_pzfj", "id", "*", "id");
    dzpz_pzfj_ds.retrieve(" id='"+ id+"' ");
    var pzid = dzpz_pzfj_ds.getString(0,"gguid");
    println( "select  verifierid  from dzpz_pz  where id='"+pzid+"' ");
    if( getIntFromSQL2("pz" ,"select  verifierid  from dzpz_pz  where id='"+pzid+"' ")>0)
    {
        println("已审核，禁止删除附件");
     return { success:false, message:'凭证已审核，禁止删除'};
    }
    

    //待归档的图片池
    var dzpz_imgpool_fileupload_ds = newDataStore("", "select * from dzpz_imgpool_fileupload");
    dzpz_imgpool_fileupload_ds.setUpdateProperty("dzpz_imgpool_fileupload", "id", "*", "id");


    var row = dzpz_imgpool_fileupload_ds.insertRow(9999);

    cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row);

    //数据调整


    dzpz_imgpool_fileupload_ds.setValue(row, "groupname", '附件');  //把类型改成 凭证

    dzpz_imgpool_fileupload_ds.setValue(row, "gguid", gguid);
    dzpz_imgpool_fileupload_ds.setValue(row, "tablename", "dzpz_imgpool");
    dzpz_imgpool_fileupload_ds.setValue(row, "corporationid", null);

    //删除源
    //当 
    dzpz_pzfj_ds.deleteRow(0);

    //更新附件数量
    dzpz_pzfj_ds.executeAfterUpdate(" update dzpz_pz  set attachmentcount= (select count(*) from dzpz_pzfj where gguid='"+ pzid+"' ) -1  where id='"+ pzid+"' ");


    var err = '' + updateAll(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds);

    if (err == '') return { success: true };
    println(err);
    return { success: false, message: err };

}

function cloneRow(dzpz_pzfj_ds, dzpz_imgpool_fileupload_ds, row) {
    for (var i = 0; i < dzpz_pzfj_ds.getColumnCount(); i++) {
        var col = dzpz_pzfj_ds.getColumnName(i);
        var value = dzpz_pzfj_ds.getValue(0, i);
        dzpz_imgpool_fileupload_ds.setValue(row, col, value);
    }
}


id: 60aca1225db2f5d22859c7e0
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 15:04:23.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }else{
                return ret;
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
    book.sheet0.getBrick('cb_fj').enabled = b;
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }

}


id: 60aca1225db2f5d22859c7e6
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 15:07:50.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }else{
                return ret;
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
   
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }

}


id: 60aca1225db2f5d22859c7ec
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 15:09:24.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }else{
                return ret;
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
   
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    $confirm("提示","确信删除本凭证，并将图片全部回归到待归档状态吗?", function(){
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }
    });

}


id: 60aca1225db2f5d22859c7f2
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 15:51:53.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }else{
                return ret;
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
   
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    $confirm("提示","确信删除本凭证，并将图片全部回归到待归档状态吗?", function(){
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }
    });

}

/**
 * 单元格的值发生了改变
 * @param sheet  所在的工作表
 * @param cell  单元格对象
 * @param oldValue  原先的值
 * @param newValue 变化后的值
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChanged(sheet, cell, oldValue, newValue, innerRow) {

    //如果是绑定了数据库的，强烈不建议使用本事件，换用结果集变化事件 itemChanged

    //通过别名来判断
    if (cell.alias == 'pzh' ||  cell.alias == "rq" ) {
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    }

    


}


id: 60aca1225db2f5d22859c7f8
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 15:54:10.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }else{
                return ret;
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
   
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
    book.sheet3.View.yOffset=0; 
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    $confirm("提示","确信删除本凭证，并将图片全部回归到待归档状态吗?", function(){
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }
    });

}

/**
 * 单元格的值发生了改变
 * @param sheet  所在的工作表
 * @param cell  单元格对象
 * @param oldValue  原先的值
 * @param newValue 变化后的值
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChanged(sheet, cell, oldValue, newValue, innerRow) {

    //如果是绑定了数据库的，强烈不建议使用本事件，换用结果集变化事件 itemChanged

    //通过别名来判断
    if (cell.alias == 'pzh' ||  cell.alias == "rq" ) {
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    }



}


id: 60aca1225db2f5d22859c7fe
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 15:54:24.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }else{
                return ret;
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
   
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
    book.sheet3.View.yOffset=0; //卷滚条归位
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    $confirm("提示","确信删除本凭证，并将图片全部回归到待归档状态吗?", function(){
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }
    });

}

/**
 * 单元格的值发生了改变
 * @param sheet  所在的工作表
 * @param cell  单元格对象
 * @param oldValue  原先的值
 * @param newValue 变化后的值
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChanged(sheet, cell, oldValue, newValue, innerRow) {

    //如果是绑定了数据库的，强烈不建议使用本事件，换用结果集变化事件 itemChanged

    //通过别名来判断
    if (cell.alias == 'pzh' ||  cell.alias == "rq" ) {
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    }



}


id: 60aca1225db2f5d22859c804
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 15:54:39.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }else{
                return ret;
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
   
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
    book.sheet3.View.yOffset=0; //垂直卷滚条归位
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    $confirm("提示","确信删除本凭证，并将图片全部回归到待归档状态吗?", function(){
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }
    });

}

/**
 * 单元格的值发生了改变
 * @param sheet  所在的工作表
 * @param cell  单元格对象
 * @param oldValue  原先的值
 * @param newValue 变化后的值
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChanged(sheet, cell, oldValue, newValue, innerRow) {

    //如果是绑定了数据库的，强烈不建议使用本事件，换用结果集变化事件 itemChanged

    //通过别名来判断
    if (cell.alias == 'pzh' ||  cell.alias == "rq" ) {
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    }



}


id: 60aca1225db2f5d22859c80a
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 15:56:21.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }else{
                return ret;
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

    book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
   
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
    book.sheet3.yOffset=0; //垂直卷滚条归位
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    $confirm("提示","确信删除本凭证，并将图片全部回归到待归档状态吗?", function(){
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }
    });

}

/**
 * 单元格的值发生了改变
 * @param sheet  所在的工作表
 * @param cell  单元格对象
 * @param oldValue  原先的值
 * @param newValue 变化后的值
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChanged(sheet, cell, oldValue, newValue, innerRow) {

    //如果是绑定了数据库的，强烈不建议使用本事件，换用结果集变化事件 itemChanged

    //通过别名来判断
    if (cell.alias == 'pzh' ||  cell.alias == "rq" ) {
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    }



}


id: 60aca1225db2f5d22859c814
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.25 23:29:24.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }else{
                return ret;
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

   // book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() {
    var imgPool = book.sheet1.getBrick('imgPool');
    if (imgPool.getSelectedImageList().length == 0) {
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
   
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
    book.sheet3.yOffset=0; //垂直卷滚条归位
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    $confirm("提示","确信删除本凭证，并将图片全部回归到待归档状态吗?", function(){
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }
    });

}

/**
 * 单元格的值发生了改变
 * @param sheet  所在的工作表
 * @param cell  单元格对象
 * @param oldValue  原先的值
 * @param newValue 变化后的值
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChanged(sheet, cell, oldValue, newValue, innerRow) {

    //如果是绑定了数据库的，强烈不建议使用本事件，换用结果集变化事件 itemChanged

    //通过别名来判断
    if (cell.alias == 'pzh' ||  cell.alias == "rq" ) {
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    }



}


id: 60ad7d905db2f5d22859c8cc
scriptid: 609d4b105db2b0a331d6bf04
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.26 06:44:31.000
script:
//为每个操作员创建一个待归档图片单据 ， 所有单据都先上传到这个单据中做附件
function main(arg) {
    var gguid = '';
    var ds = newDataStore("", "select * from dzpz_imgpool ");
    ds.setUpdateProperty("dzpz_imgpool", "id", "*", "id");
    ds.retrieve(" userid='" + currentUserId() + "'");
    println( ds.getSelectLastRetrieved());
    if (ds.getRowCount() == 1) {
        gguid = ds.getString(0, "gguid");
    } else {

        ds.insertRow(0);
        gguid = newGUID();
        ds.setValue(0, "id", gguid);
        ds.setValue(0, "gguid", gguid);
        ds.setValue(0, "billno", gguid);
        ds.setValue(0, "billdate", newDate());
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "userid", currentUserId());
        ds.setValue(0, "username", currentUserShowName());
        ds.setValue(0, "deleteflag", 0);
        ds.setValue(0, "blocksize", 128);
        

        if (ds.update(true)) {
            error = ds.getError().getMessage();
            println(error);
            return { success: false, message: error };
        }
    }

    var blockSize=  ds.getInt(0,"blocksize");
    if( blockSize==0) blockSize=128; 

    //提供图片上传的必要的参数
    var config = { "serverIndex": "2", 
                    "dbpool": "default", 
                    "logTable": "dzpz_imgpool_fileupload", 
                    "blockSize": blockSize, 
                    "imgMaxSize": 1920, 
                    "groupName": "附件",
                     "tableName": "dzpz_imgpool", 
                     "gguid": gguid, 
                     "path": "dzpz_imgpool", 
                     "maxThumbnailSize": 256 ,
                     "responseType":"JSON"  //表示上传成功捂返回 JSON格式的结果
             }

    println(JSON.stringify(config));
    return config; 


}


id: 60adfe725db26f352029d515
scriptid: 609d4b105db2b0a331d6bf04
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.26 21:23:32.000
script:
//为每个操作员创建一个待归档图片单据 ， 所有单据都先上传到这个单据中做附件
function main(arg) {
    var gguid = '';
    var ds = newDataStore("", "select * from dzpz_imgpool ");
    ds.setUpdateProperty("dzpz_imgpool", "id", "*", "id");
    ds.retrieve(" userid='" + currentUserId() + "'");
    println( ds.getSelectLastRetrieved());
    if (ds.getRowCount() == 1) {
        gguid = ds.getString(0, "gguid");
    } else {

        ds.insertRow(0);
        gguid = newGUID();
        ds.setValue(0, "id", gguid);
        ds.setValue(0, "gguid", gguid);
        ds.setValue(0, "billno", gguid);
        ds.setValue(0, "billdate", newDate());
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "userid", currentUserId());
        ds.setValue(0, "username", currentUserShowName());
        ds.setValue(0, "deleteflag", 0);
        ds.setValue(0, "blocksize", 128);
        

        if (ds.update(true)) {
            error = ds.getError().getMessage();
            println(error);
            return { success: false, message: error };
        }
    }

    var blockSize=  ds.getInt(0,"blocksize");
    if( blockSize==0) blockSize=128; 

    //提供图片上传的必要的参数
    var config = { "serverIndex": "2", 
                    "dbpool": "pz", 
                    "logTable": "dzpz_imgpool_fileupload", 
                    "blockSize": blockSize, 
                    "imgMaxSize": 1920, 
                    "groupName": "附件",
                     "tableName": "dzpz_imgpool", 
                     "gguid": gguid, 
                     "path": "dzpz_imgpool", 
                     "maxThumbnailSize": 256 ,
                     "responseType":"JSON"  //表示上传成功捂返回 JSON格式的结果
             }

    println(JSON.stringify(config));
    return config; 


}


id: 60aed3065db2e8b7a7736f44
scriptid: 609d4b105db2b0a331d6bf04
tablename: app_script
clientip: 192.168.3.2
username: sa
createdate: 2021.05.27 07:10:20.000
script:
//为每个操作员创建一个待归档图片单据 ， 所有单据都先上传到这个单据中做附件
function main(arg) {
    var gguid = '';
    var ds = newDataStore("", "select * from dzpz_imgpool ");
    ds.setUpdateProperty("dzpz_imgpool", "id", "*", "id");
    ds.retrieve(" userid='" + currentUserId() + "'");
    println( ds.getSelectLastRetrieved());
    if (ds.getRowCount() == 1) {
        gguid = ds.getString(0, "gguid");
    } else {

        ds.insertRow(0);
        gguid = newGUID();
        ds.setValue(0, "id", gguid);
        ds.setValue(0, "gguid", gguid);
        ds.setValue(0, "billno", gguid);
        ds.setValue(0, "billdate", newDate());
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "userid", currentUserId());
        ds.setValue(0, "username", currentUserShowName());
        ds.setValue(0, "deleteflag", 0);
        ds.setValue(0, "blocksize", 128);
        

        if (ds.update(true)) {
            error = ds.getError().getMessage();
            println(error);
            return { success: false, message: error };
        }
    }

    var blockSize=  ds.getInt(0,"blocksize");
    if( blockSize==0) blockSize=128; 

    //提供图片上传的必要的参数
    var config = { "serverIndex": "2", 
                    "dbpool": "pz", 
                    "logTable": "dzpz_imgpool_fileupload", 
                    "blockSize": blockSize, 
                    "imgMaxSize": 1920, 
                    "groupName": "附件",
                     "tableName": "dzpz_imgpool", 
                     "gguid": gguid, 
                     "path": "dzpz_imgpool/"+ date2yyyymmdd(newDate(), "/"), 
                     "maxThumbnailSize": 256 ,
                     "responseType":"JSON"  //表示上传成功捂返回 JSON格式的结果
             }

    println(JSON.stringify(config));
    return config; 


}


id: 60b195825db29bb3f5baa63a
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 192.168.3.2
username: sa
createdate: 2021.05.29 09:19:29.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }else{
                return ret;
            }

        }
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

   // book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() { doSelect(true);}
function cb_selectnull_onClick() { doSelect(false);}

function doSelect(b)
{
    var imgPool = book.sheet1.getBrick('imgPool');
    if (b){
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
   
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
    book.sheet3.yOffset=0; //垂直卷滚条归位
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    $confirm("提示","确信删除本凭证，并将图片全部回归到待归档状态吗?", function(){
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }
    });

}

/**
 * 单元格的值发生了改变
 * @param sheet  所在的工作表
 * @param cell  单元格对象
 * @param oldValue  原先的值
 * @param newValue 变化后的值
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChanged(sheet, cell, oldValue, newValue, innerRow) {

    //如果是绑定了数据库的，强烈不建议使用本事件，换用结果集变化事件 itemChanged

    //通过别名来判断
    if (cell.alias == 'pzh' ||  cell.alias == "rq" ) {
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    }



}


id: 60bf56af993270982e9c15f8
scriptid: 609d4b105db2b0a331d6bf04
tablename: app_script
clientip: 10.255.0.2
username: sa
createdate: 2021.06.08 20:00:42.000
script:
//为每个操作员创建一个待归档图片单据 ， 所有单据都先上传到这个单据中做附件
function main(arg) {
    var gguid = '';
    var ds = newDataStore("", "select * from dzpz_imgpool ");
    ds.setUpdateProperty("dzpz_imgpool", "id", "*", "id");
    println ( "currentUserId="+ currentUserId());
    ds.retrieve(" userid='" + currentUserId() + "'");
    println( ds.getSelectLastRetrieved());
    if (ds.getRowCount() == 1) {
        gguid = ds.getString(0, "gguid");
    } else {

        println( "new record");
        ds.insertRow(0);
        gguid = newGUID();
        ds.setValue(0, "id", gguid);
        ds.setValue(0, "gguid", gguid);
        ds.setValue(0, "billno", gguid);
        ds.setValue(0, "billdate", newDate());
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "userid", currentUserId());
        ds.setValue(0, "username", currentUserShowName());
        ds.setValue(0, "deleteflag", 0);
        ds.setValue(0, "blocksize", 128);
        

        if (ds.update(true)) {
            error = ds.getError().getMessage();
            println(error);
            return { success: false, message: error };
        }
    }

    var blockSize=  ds.getInt(0,"blocksize");
    if( blockSize==0) blockSize=128; 

    //提供图片上传的必要的参数
    var config = { "serverIndex": "2", 
                    "dbpool": "pz", 
                    "logTable": "dzpz_imgpool_fileupload", 
                    "blockSize": blockSize, 
                    "imgMaxSize": 1920, 
                    "groupName": "附件",
                     "tableName": "dzpz_imgpool", 
                     "gguid": gguid, 
                     "path": "dzpz_imgpool/"+ date2yyyymmdd(newDate(), "/"), 
                     "maxThumbnailSize": 256 ,
                     "responseType":"JSON"  //表示上传成功捂返回 JSON格式的结果
             }

    println(JSON.stringify(config));
    return config; 


}


id: 60c058be993270982e9c1646
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 10.255.0.2
username: sa
createdate: 2021.06.09 14:08:59.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  and ifnull(info2,'')='' ) order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


//@comment (group=,caption=追加图片到凭证中,needRightControl=false)
function addImageToPZ( pzid , images)
{
    return  JSON.parse( runTask("追加图片到凭证中",{pzid:pzid, images:images}));
}

function deletePZAndPutBackToPool(id)
{
     return  JSON.parse( runTask("删除凭证所有图片回归池中", {id:id} ));
}

function  moveImageToRecycle(id)
{
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));
    runSQL("pz" , " update "+config.logTable+ " set info2='回收站' where id='"+id+"'  ");

}


id: 60c058be993270982e9c164e
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 10.255.0.2
username: sa
createdate: 2021.06.09 14:10:04.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  and ifnull(info2,'')='' ) order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


//@comment (group=,caption=追加图片到凭证中,needRightControl=false)
function addImageToPZ( pzid , images)
{
    return  JSON.parse( runTask("追加图片到凭证中",{pzid:pzid, images:images}));
}

function deletePZAndPutBackToPool(id)
{
     return  JSON.parse( runTask("删除凭证所有图片回归池中", {id:id} ));
}

function  moveImageToRecycle(id)
{
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));
    println(  " update "+config.logTable+ " set info2='回收站' where id='"+id+"'  ");

}


id: 60c058be993270982e9c1664
scriptid: 60a321f05db27838e0c014bf
tablename: app_script
clientip: 10.255.0.2
username: sa
createdate: 2021.06.09 14:11:07.000
script:
//@comment (group=,caption=得到当前操作员待归档图片对应的GGUID,needRightControl=false)
function getImagePoolGGUID() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).gguid;

}

function getImagePoolBlockSize() {
    var ret = runTask("get_dzpz_imgpool_id_of_current_user", {});
    return JSON.parse(ret).blockSize;

}


//@comment (group=,caption=保存当前的缩略图大小,needRightControl=false)
function setImagePoolBlockSize(id, blockSize) {
    runSQL("", " update  dzpz_imgpool set blocksize=" + blockSize + " where gguid='" + id + "'  ");
}


//@comment (group=,caption=得到待归档的图片清单,needRightControl=true)
function loadImagePoolData() {
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));

    var ds = newDataStore(config.dbpool, "select * from " + config.logTable + "  where  gguid='" + config.gguid + "'  and ifnull(info2,'')='' ) order by info1 "); // info1 是客户端文件创建的时间，以它排序
    var n = ds.retrieve();
    var ret = [];
    for (var i = 0; i < n; i++) {
        ret.push(JSON.parse(ds.getJSONRow(i, true)));
    }

    return ret;

}


//@comment (group=,caption=保存监控目录设置,needRightControl=false)
function setMonitePath(hostName, path) {
    var ds = newDataStore("", "select * from dzpz_client");
    ds.setUpdateProperty("dzpz_client","id","*","id");
    ds.retrieve(" hostname='" + hostName + "'");
    if (ds.getRowCount() == 0) {
        var newid = newGUID();
        ds.insertRow(0);
        ds.setValue(0, "id", newid);
        ds.setValue(0, "gguid", newid);
        ds.setValue(0, "billno", newid);
        ds.setValue(0, "userid", 1);
        ds.setValue(0, "username", '管理员');
        ds.setValue(0, "billtype", '');
        ds.setValue(0, "hostname", hostName);
    }
    ds.setValue(0, "path", path);
    ds.update(true);
    println( ds.getError().getMessage());

}


//@comment (group=,caption=获取指定机器的监控目录,needRightControl=false)
function  getMonitePath(hostName)
{
    return   getStringFromSQL("select path from dzpz_client where  hostname='" + hostName + "'");
}


//@comment (group=,caption=构建凭证附件清单,needRightControl=false)
function getPZFJ( id)
{
    var ds= newDataStore("pz", "select id , url_img , url_thumbnail ,groupname , info1 from  dzpz_pzfj where  gguid='"+id+"'  order by groupname , info1 asc ");
    var n=ds.retrieve();
    var ret=[];
    for( var i=0;i<n;i++)
    {
        ret.push(  JSON.parse(  ds.getJSONRow(i, true)));
    }
    return ret; 

}



//@comment (group=,caption=把凭证中的附件图片回归到待归档池中,needRightControl=false)
function putImageBackToPool( id )
{
    println( id);
    return  JSON.parse( runTask("凭证附件移除", {id:id} ));
}


//@comment (group=,caption=追加图片到凭证中,needRightControl=false)
function addImageToPZ( pzid , images)
{
    return  JSON.parse( runTask("追加图片到凭证中",{pzid:pzid, images:images}));
}

function deletePZAndPutBackToPool(id)
{
     return  JSON.parse( runTask("删除凭证所有图片回归池中", {id:id} ));
}

function  moveImageToRecycle(id)
{
    var config = JSON.parse(runTask("get_dzpz_imgpool_id_of_current_user", {}));
    println(  " update "+config.logTable+ " set info2='回收站' where id='"+id+"'  ");


}


id: 60c058be993270982e9c166b
scriptid: 60a321f05db27838e0c014c3
tablename: app_template
clientip: 10.255.0.2
username: sa
createdate: 2021.06.09 14:12:19.000
script:
var pathToMonit = "";//当前监控的目录
var electron = null;


function formOpen() {
    book.showTabOnWhere = "none";
    newImgCommingTipVisible(false);

    book.sheet3.getBrick('cb_delete_pz').visible = false;

    //加载帐套信息及年月
    var corporationid = book.sheet2.cells("corporationid").getValueString();
    runTask("当前记帐期", { corporationid: corporationid }, function (ym) {
        book.sheet2.cells("ym").setValue(ym);

        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    });

    book.sheet3.getBrick('FJ').config.promptInfoOnDelete = "确信要将图片从当前凭证中移除，并放回到图片池中吗?";

    //收到来自服务器推送的消息
    onPushMessage("electronPushMessage", function (msg) {
        msg = JSON.parse(msg);
        if (msg.cmd == "newFileUploaded") newImgCommingTipVisible(true); //通知有新文件上传了
        if (msg.cmd == "notifyNeedUploadFileCount") notifyNeedUploadFileCount(msg.fileCount); //通知还有多少个文件等待上传
    });

    //获取当前的electron接口对象
    electron = getElectron(); //getElectron是系统全局功能函数

    book.sheet1.getBrick('imgPool').selectionMode = 'multi'; //图片设置成多选模式

    // 追加一些CSS类 ，用于凭证号标记
    $(document.body).append(`<style>
                        .pzh {position:absolute;left:0;bottom:0px; 
                                height:24px; line-height:24px;width:100%; 
                                background-color:#40af40a0; 
                                color:white;font-weight:bold;font-size:16px;text-align:center;}
                        .delete_pzh { position:absolute; right:0; width:40px; height:14px; top:0; text-align:right; 
                                    vertical-align:top; font-size:12px; color:gray;padding-right:5px;}      
                        .delete_pzh:hover { color:white;}      
                          
                        </style>
                    `);


    //注册调整缩略图大小的事件
    $('#blockSize').on('input', resetImgPoolBlockSize);

    setTimeout(function () {
        //调取缩略图大小
        var imgPool = book.sheet1.getBrick('imgPool');

        let blockSize = getImagePoolBlockSize();
        if (blockSize != imgPool.config.blockSize) {
            $('#blockSize').val(blockSize);
            resetImgPoolBlockSize(false);
        }
    }, 1000);

    //调取图片目录设置，注意适当延时
    setTimeout(function () {
        if (electron != null) {
            var hostName = electron.getHostName();
            pathToMonit = getMonitePath(hostName);

            //开启监控
            if (pathToMonit != '') moniteStart(pathToMonit);
        }
    }, 500);
}

// 先保存当前选中状态及设置的凭证号信息

var g_selection;
var g_pzMap;
//@comment (group=图片清单刷新,caption=保存选中状态及凭证标记信息,needRightControl=false)
function saveSelectionAndPZInfo() {

    var imgPool = book.sheet1.getBrick('imgPool');

    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    // 保存设置的凭证号信息
    var pzhMap = {};
    var pzhs = imgPool.dom.find('.pzh');
    for (let i = 0; i < pzhs.length; i++) {
        let id = $(pzhs[i]).parent().attr('id');
        let pzh = $(pzhs[i]).find('.no').text();
        pzhMap[id] = pzh;

    }
    g_selection = selection;
    g_pzMap = pzhMap;
}




//@comment (group=图片控件,caption=还原选中及凭证标记,needRightControl=false)
function restoreSelectionAndPZInfo() {
    var selection = g_selection;
    var pzhMap = g_pzMap;
    var imgPool = book.sheet1.getBrick('imgPool');

    //恢复选中状态
    for (let i = 0; i < selection.length; i++) {
        imgPool.setImageSelected(selection[i].id, true);
    }
    //恢复凭证号
    for (let id in pzhMap) {
        var pzh = pzhMap[id];
        find_pzh(id, true).find('.no').text(pzh);
    }

}

//@comment (group=图片控件,caption=重设小图大小,needRightControl=false)
function resetImgPoolBlockSize(needSaveSetting) {
    if (needSaveSetting == null) needSaveSetting = true;
    var imgPool = book.sheet1.getBrick('imgPool');

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    let v = $('#blockSize').val();

    imgPool.config.blockSize = parseInt(v);
    imgPool.rebuildGridly();

    //恢复选中状态
    restoreSelectionAndPZInfo();

    if (needSaveSetting) {
        setTimeout(function () {
            setImagePoolBlockSize(imgPool.getConfig("gguid"), v);
        }, 1000);
    }



}


/**
 * 图片控件中图片上的删除按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 * @param data  图片的信息
 *
 * @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行删除操作，否则表示终止删除操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上的删除按钮被点击)
function imageOnDelete(sheet, name, data) {

    if (name == 'FJ') {
        if (data.groupname == '凭证') {
            return { success: false, message: "这是凭证，不允许这样移除，请点击“删除凭证”" };
        } else {
            var ret = putImageBackToPool(data.id);
            if (ret.success) {
                smartRetrieve(book.sheet3, "one");
                cb_refresh_onClick();
            }else{
                return ret;
            }

        }
    }

    if( name=='imgPool')
    {
        moveImageToRecycle( data.id);
    }
    return { success: true };
}
/**
 * 图片控件中上传按钮被点击
 * @param sheet   所在Sheet
 * @param name  图片控件的名称
 *
 @returns  {object}    返回一个对象 ， 其中  success= boolean 表示继续执行上传操作，否则表示终止上传操作
 *    如果需要显示一个提示信息，则在 属性 message 中指定
 */
//@comment (group=图片控件,caption=图片上传按钮被点击)
function imageOnNewUpload(sheet, name) {



    if (name == 'FJ') {
        var imgPool = book.sheet1.getBrick('imgPool');
        // 保存当前选中状态及设置的凭证号信息
        var selection = imgPool.getSelectedImageList();
        if (selection.length == 0) {
            toastr.error("至少选中一张图片");
            return { success: false }; // 此返回，阻止了默认的上传操作
        }


        var lastPZ = null;
        for (let i = 0; i < selection.length; i++) {
            var one = selection[i];
            var pzh = getPZH(one.id);
            if (pzh != '') {
                toastr.error("选中的图片中有标记为凭证的，禁止将凭证图片添加到当前凭证中");
                return { success: false }; // 此返回，阻止了默认的上传操作
            }
        }

        var ret = addImageToPZ($one_ds.getString(0, "id"), selection);
        if (ret.success) {
            //刷新图片区
            cb_refresh_onClick();
            //刷新凭证
            loadPZ($one_ds.getString(0, "id"), $one_ds.getString(0, "verifiername") != "" ? 1 : 0);
        }

    }


    return { success: false }; // 此返回，阻止了默认的上传操作
}


/**
 * 图片控件中鼠标进入某个图片区域
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=鼠标进入图片区域)
function imageOnMouseEnter(sheet, name, data) {
    //console.dir(data);
    var url = data.url_img;
    book.sheet0.cells("imgPreview").setValue(url);
}
//@comment (group=图片控件,caption=鼠标从图片移开,needRightControl=false)
function imageOnMouseOut(sheet, name, data) {

   // book.sheet0.cells("imgPreview").setValue("");
}


/**
 * 图片控件中某个图片被双击了
 * @param sheet   所在Sheet
 * @param config  图片控件的名称
 *@param data  该图片的详细信息
 *
 */
//@comment (group=图片控件,caption=图片被双击)
function imageOnDBLClick(sheet, name, data) {

    //因为 mouseOut 会把大图清除掉，所以这里重新再设置上
    setTimeout(function () {
        var url = data.url_img;
        book.sheet0.cells("imgPreview").setValue(url);

    }, 200);

    var pzh = '';
    try {

        pzh = find_pzh(data.id, false).find('.no').text();
    } catch (err) {

    }

    if (pzh == undefined) pzh = '';

    inputBox("标记为凭证", "", pzh, "输入凭证号码", function (val) {
        if (val == '') {
            var p = find_pzh(data.id, false);
            if (p) p.remove();
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, false);
        } else {
            find_pzh(data.id, true).find('.no').text(val);
            book.sheet1.getBrick("imgPool").setImageSelected(data.id, true);
        }

    }, true /*允许为空*/);
}


//@comment (group=,caption=得到图片的凭证号如果有的话,needRightControl=false)
function getPZH(id) {
    let obj = find_pzh(id, false);
    if (obj == null) return;
    let s = obj.text();
    s = s.replace(/\s/g, '');
    return s;
}


//@comment (group=图片清单刷新,caption=查找或创建凭证号标记对象,needRightControl=false)
function find_pzh(id, create_if_not_exists) {
    var d = $('#' + id);
    if (!d) {
        $.alert(id + "不存在");
        return null;
    }

    var ret = d.find('.pzh');
    if (ret.length == 0) {
        if (create_if_not_exists) //如果需要创建，则创建它
        {
            d.append(`<div class="pzh" >
                        <div class="no"></div>
                        <div class="delete_pzh"><i class="fa fa-close" onclick="removePZH('${id}');  event.preventDefault(); event.stopPropagation();"> </i></div>
                      </div>`);
        }
        ret = d.find('.pzh');
    }
    return ret;

}

//@comment (group=图片控件,caption=移除图片看的凭证号标记,needRightControl=false)
function removePZH(id) {
    var p = find_pzh(id, false);
    if (p) p.remove();
}

function cb_selectall_onClick() { doSelect(true);}
function cb_selectnull_onClick() { doSelect(false);}

function doSelect(b)
{
    var imgPool = book.sheet1.getBrick('imgPool');
    if (b){
        imgPool.selectAll();
    } else {
        imgPool.unSelectAll();
    }
}


function cb_path_onClick() {
    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    var path = electron.showOpenDialogSync('');
    if (path == '') return;
    var hostName = electron.getHostName();
    setMonitePath(hostName, path);

    moniteStart(path);


}

//@comment (group=上传相关,caption=开始文件变化监控,needRightControl=false)
function moniteStart(path) {
    pathToMonit = path;
    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit);

    if (electron == null) {
        toastr.error("此功能仅能在客户端APP中执行");
        return;
    }

    book.sheet0.cells('stateBar').setValue("图片目录 " + pathToMonit + " 监控中...");
    electron.ipcSendMessage({ cmd: "moniteStart", path: path });
}

function cb_refresh_onClick() {
    newImgCommingTipVisible(false);

    // 先保存当前选中状态及设置的凭证号信息
    saveSelectionAndPZInfo();

    var imgPool = book.sheet1.getBrick('imgPool');
    imgPool.reInit(restoreSelectionAndPZInfo); //重新初始化 ,并在初始化完成后， 恢复选中状态

    toastr.info("重新加载完成");



}

//@comment (group=上传相关,caption=上传好一个文件后，通知有新文件,needRightControl=false)
//@comment (group=上传相关,caption=上传好一个文件后通知有新文件,needRightControl=false)
function newImgCommingTipVisible(v) {
    book.sheet0.getBrick('shineText').visible = v;

}

//@comment (group=上传相关,caption=刷新当前还有多少文件要上传的提示,needRightControl=false)
function notifyNeedUploadFileCount(fc) {
    var info = fc == 0 ? "" : "【" + fc + "】个文件正准备上传";
    book.sheet0.cells("stateInfo2").setValue(info);
}


//@comment (group=归档,caption=凭证归档,needRightControl=false)
function cb_auto_onClick() {

    var imgPool = book.sheet1.getBrick('imgPool');
    // 保存当前选中状态及设置的凭证号信息
    var selection = imgPool.getSelectedImageList();
    if (selection.length == 0) {
        toastr.error("至少选中一张图片");
        return;
    }

    var pzGroup = [];
    var lastPZ = null;
    for (let i = 0; i < selection.length; i++) {
        var one = selection[i];
        var pzh = getPZH(one.id);
        if (pzh == '') {
            if (lastPZ != null) {
                lastPZ.fj.push(one.id);
            }
        } else {
            lastPZ = {};
            lastPZ.pzh = pzh;
            lastPZ.imgID = one.id;
            lastPZ.fj = [];
            lastPZ.fj.push(one.id); //凭证图片，也加到附件列表中，便于撤销归档
            pzGroup.push(lastPZ);
        }
    }

    if (pzGroup.length == 0) {
        toastr.error("凭证归档操作，至少需要标记一张凭证，并选中它");
        return;
    }

    console.dir(pzGroup);

    $confirm("提示", "确信要归档" + pzGroup.length + "张凭证吗?", function () {
        do_pzgd(pzGroup);
    });


}

//@comment (group=归档,caption=归档,needRightControl=false)
function do_pzgd(pzGroup) {
    showInfoPane('*正在归档...');
    setButtonsStatus(false); //禁用一些按钮点击
    runTask("归档",
        {
            corporationid: book.sheet2.cells("corporationid").getValueString(),
            ym: book.sheet2.cells("ym").getValueString(),
            pzGroup: pzGroup
        }, function () {

            hideInfoPane();
            setButtonsStatus(true); //启用一些按钮点击
            smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
            //重新检索图片区
            cb_refresh_onClick();

        });
}

function setButtonsStatus(b) {
   
    book.sheet0.getBrick('cb_auto').enabled = b;
}

function loadPZ(id, verified) {
    book.sheet3.cells("id").setValue(id);
    smartRetrieve(book.sheet3, "one");
    //附件图片清单
    //不能从 $one_ds中取数，因为上面的smartRetrieve 是异步的
    let action = (verified == 0) ? "edit" : "view";
    book.sheet3.getBrick('cb_delete_pz').visible = verified == 0;
    var FJ = book.sheet3.getBrick('FJ');
    FJ.config.action = action; //控制是否显示删除和新增按钮
    FJ.buildGridly(getPZFJ(id));
    book.sheet3.yOffset=0; //垂直卷滚条归位
}


//@comment (group=,caption=删除凭证，并把图片回归到池中,needRightControl=false)
function cb_delete_pz_onClick() {
    $confirm("提示","确信删除本凭证，并将图片全部回归到待归档状态吗?", function(){
    book.sheet3.getBrick('cb_delete_pz').visible = false;
    var id = $one_ds.getString(0, "id");
    var ret = deletePZAndPutBackToPool(id);
    if (ret.success) {
        $one_ds.reset();
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
        var FJ = book.sheet3.getBrick('FJ');
        FJ.config.action = 'view';
        FJ.buildGridly([]);
        cb_refresh_onClick();//刷新池中图片

    } else {
        toastr.error(ret.message);
    }
    });

}

/**
 * 单元格的值发生了改变
 * @param sheet  所在的工作表
 * @param cell  单元格对象
 * @param oldValue  原先的值
 * @param newValue 变化后的值
 * @param innerRow   当单元格没有绑定数据库或绑定到单行数据库时，本参数无意义。如果绑定一个
 * 多行数据库，则表示该单元所绑定的字段的第 innerRow行发生变化
 */
function cellValueChanged(sheet, cell, oldValue, newValue, innerRow) {

    //如果是绑定了数据库的，强烈不建议使用本事件，换用结果集变化事件 itemChanged

    //通过别名来判断
    if (cell.alias == 'pzh' ||  cell.alias == "rq" ) {
        smartRetrieve(book.sheet2, "dzpz_pz"); //刷新凭证列表
    }



}


