<?xml version="1.0" encoding="UTF-8"?>
<!--
    $author: zhuxianfei
    $date: 2018年6月26日 下午4:33:30
    $revision: 1.0
    $purpose:
-->
<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" dynamiccreateenabled="true">
    <a:init-procedure/>
    <a:view>
        <a:link id="lend_doc_get_layout_code_link_id" model="cont.CON500.con_contract_get_layout_code"
                modelaction="update"/>
        <a:link id="lend_document_modify_link"
                url="${/request/@context_path}/modules/cont/CON556/document_lend_record_add.lview"/>
        <a:link id="lend_document_return_modify_link"
                url="${/request/@context_path}/modules/cont/CON556/document_lend_record_return.lview"/>
        <a:link id="lend_submit_link_id" model="cont.CON556.lend_record_number" modelaction="update"/>

        <script type="text/javascript"><![CDATA[
        //归还维护
        window['${/parameter/@layout_code}_user_button1_layout_dynamic_click'] = function () {
            //
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_doc_lend_record_hd');
            var record = $(ds_id).getSelected();
            if (record.length != 1) {
                Leaf.showMessage('${HLS.PROMPT}', '请选择一条单据！');
                return;
            } else if (record[0].get('status') == 'APPROVING') {
                Leaf.showMessage('${HLS.PROMPT}', '该单据正在审批中，不可操作！');
                return;
            } else if (record[0].get('status') == 'NEW' || record[0].get('status') == 'REJECT') {
                Leaf.showMessage('${HLS.PROMPT}', '新建和拒绝的单据不可维护归还信息，不可操作！');
                return;
            }
            var param = record[0].data;
            param['function_code'] = 'CON556R';
            param['function_usage'] = 'MODIFY';
            param['maintain_type'] = 'UPDATE';
            param['lend_header_id'] = record[0].get('lend_header_id');
            param['winId'] = 'document_lend_return_id';
            param['ds_id'] = ds_id;
            param['url_title'] = '归还维护';
            hls_doc_get_layout_code('lend_doc_get_layout_code_link_id', param, 'lend_document_return_modify_link', ds_id);
        }

        //新增
        window['${/parameter/@layout_code}_user_button2_layout_dynamic_click'] = function () {
            //
            var ds_id = 'LEND_DOCUMENT_ENTRANCE_G_GRID_con_doc_lend_record_hd_ds';
            var param = {};
            param['function_code'] = 'CON556E';
            param['function_usage'] = 'MODIFY';
            param['maintain_type'] = 'UPDATE';
            param['winid'] = 'document_lend_record_win_id';
            param['ds_id'] = ds_id;
            param['url_title'] = '借阅申请';
            hls_doc_get_layout_code('lend_doc_get_layout_code_link_id', param, 'lend_document_modify_link', ds_id);
        }
        //提交审批
        window['${/parameter/@layout_code}_submit_approval_layout_dynamic_click'] = function () {
            var lend_hd_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_doc_lend_record_hd');
            var records = $(lend_hd_ds_id).getSelected();
            if (records.length != 1) {
                Leaf.showMessage('${HLS.PROMPT}', '请选择一条单据！');
                return;
            } else if (records[0].get('status') != 'NEW') {
                Leaf.showMessage('${HLS.PROMPT}', '只可以提交新建状态的单据！');
                return;
            }
            window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
            Leaf.request({
                url: $('lend_submit_link_id').getUrl(),
                para: {
                    lend_header_id: records[0].get('lend_header_id')
                },
                success: function (res) {
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                    Leaf.SideBar.show({
                        msg: '操作成功',
                        duration: 2000
                    });
                    $(lend_hd_ds_id).query();
                }
                ,
                error: function () {
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                }
                ,
                failure: function () {
                    window['${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                }
                ,
                sync: true,
                scope: this
            });
        };
        //渲染
        window['${/parameter/@layout_code}_dynamic_link_renderer'] = function (value, record, name, config_record) {
            //
            if (name == 'lend_number' && value) {
                return '<a href="javascript:open_lend_modify_win(\'' + record.id + '\',\'' + record.ds.id + '\')">' + value + '</a>';
            }else if(name == 'status' && value){
                var real_date = record.get('real_date');
                if(!isEmpty(real_date)){
                    return '已归还';
                }
            }
            return value;
        };


        function open_lend_modify_win(record_id, ds_id) {
            //
            var record = $(ds_id).findById(record_id);
            var status = record.get('status');
            var param = record.data;

            if (status == 'NEW') {
                param['function_code'] = 'CON556E';
                param['function_usage'] = 'MODIFY';
                param['maintain_type'] = 'UPDATE';
            } else {
                param['function_code'] = 'CON556N';
                param['function_usage'] = 'QUERY';
                param['maintain_type'] = 'QUERY';
            }
            param['lend_header_id'] = record.get('lend_header_id');
            param['url_title'] = '借阅信息';
            param['layout_debugger_flag'] = 'N';
            hls_doc_get_layout_code('lend_doc_get_layout_code_link_id', param, 'lend_document_modify_link', ds_id);
        }


        //导出
        window['${/parameter/@layout_code}_user_button4_layout_dynamic_click'] = function () {
            //
            var lend_hd_ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'con_doc_lend_record_hd');
            var grid_id = lend_hd_ds_id.replace('ds', 'layout_grid_id');
            $(grid_id)._export(null,'档案借阅管理');


        }

        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>
