<?xml version="1.0" encoding="UTF-8"?>
<a:screen xmlns:a="http://www.leaf-framework.org/application" xmlns:p="uncertain.proc" trace="true">
    <a:init-procedure/>
    <a:view>
        <a:link id="remove_link" url="${/request/@context_path}/task_report/delete"/>
        <a:link id="psr_task_reports_authority_link_id" url="${/request/@context_path}/modules/psr/PSR1030/psr_task_reports_authority.lview"/>
        <script type="text/javascript"><![CDATA[
            function down(value, record, name) {
                var report_id = record.get('report_id');
                var file_path = record.get('file_path');
                var file_name_desc = record.get('file_name');
            
                file_path = file_path.replace(/\\/g, '/');
                var index = file_path.lastIndexOf('/');
                index = index + 1;
                var fileName = file_path.substring(index);
                if (!file_name_desc) {
                    file_name_desc = fileName;
                }
                var download_url = '${/request/@context_path}/task_report/download?report_id=' + report_id + '&fileName=' + fileName + '&fileNameDesc=' + file_name_desc;
                var delete_url = $('remove_link').getUrl() + "?report_id=" + report_id + "&fileName=" + fileName;
                var url = '<a href="javascript:download(\'' + download_url + '\',\'' + delete_url + '\')">' + file_name_desc + '</a>';
                return url;
            }
            
            function download(download_url, delete_url) {
                var ds = $('rpt_task_reports_ds');
                Leaf.post(download_url);
                //deleteRecord(delete_url);
            }
            
            function deleteRecord(delete_url) {
                var ds = $('rpt_task_reports_ds');
                $L.showConfirm(_lang['grid.remove.confirm'], _lang['grid.remove.confirmMsg'], function(win) {
                    Leaf.request({
                        url: delete_url,
                        para: {},
                        success: function(args) {
                            ds.remove();
                        },
                        error: ds.onSubmitError,
                        failure: ds.onAjaxFailed,
                        scope: this
                    });
                });
            }
            
            function remove(value, record, name) {
                var report_id = record.get('report_id');
                var file_path = record.get('file_path');
                file_path = file_path.replace(/\\/g, '/');
                var index = file_path.lastIndexOf('/');
                index = index + 1;
                var fileName = file_path.substring(index);
                var delete_url = $('remove_link').getUrl() + "?report_id=" + report_id + "&fileName=" + fileName;
                var url = '<a href="javascript:deleteRecord(\'' + delete_url + '\')">删除文件</a>';
                return url;
            }
            
            function remove_select() {
                $L.showConfirm(_lang['grid.remove.confirm'], _lang['grid.remove.confirmMsg'], function(win) {
                    var ds = $('rpt_task_reports_ds');
                    var records = ds.getSelected();
                    var record;
                    /* for (var i = 0;i < records.length;i++) {
                     record = records[i];
                     var report_id = record.get('report_id');
                     var file_path = record.get('file_path');
                     var index = 0;
                     index = file_path.lastIndexOf('\\');
                     if (index == -1) {
                     index = file_path.lastIndexOf('/');
                     }
                     index = index + 1;
                     var fileName = file_path.substring(index);
                     var delete_url = $('remove_link').getUrl() + "?report_id=" + report_id + "&fileName=" + fileName;
                     deleteRecord_noConfirm(delete_url, record);
                     } */
                    for (var i = 0;i < records.length;i++) {
                        record = records[i];
                        var report_id = record.get('report_id');
                        var file_path = record.get('file_path');
                        file_path = file_path.replace(/\\/g, '/');
                        var index = file_path.lastIndexOf('/');
                        index = index + 1;
                        var fileName = file_path.substring(index);
                        var delete_url = $('remove_link').getUrl() + "?report_id=" + report_id + "&fileName=" + fileName;
                        deleteRecord_noConfirm(delete_url, record);
                    }
                });
            }
            
            function deleteRecord_noConfirm(delete_url, record) {
                var ds = $('rpt_task_reports_ds');
                Leaf.request({
                    url: delete_url,
                    para: {},
                    success: function(args) {
                        ds.remove(record);
                    },
                    error: ds.onSubmitError,
                    failure: ds.onAjaxFailed,
                    scope: this
                });
            }
            
            function query() {
                $('rpt_task_reports_ds').query();
            }
            
            function reset() {
                $('rpt_task_reports_query_ds').reset();
            }
            
            function config_report_result_authority_renderer(value, record, name) {
                var url = '<a href="javascript:result_authority(\'' + record.id + '\')">报表数据授权</a>';
                return url;
            }
            
            function result_authority(report_id) {
                var authority_url = $('psr_task_reports_authority_link_id').getUrl();
                var record = $('rpt_task_reports_ds').findById(report_id);
                new Leaf.Window({
                    id: 'psr_task_reports_authority_winid',
                    url: authority_url,
                    params: {
                        report_id: record.get('report_id'),
                        file_name: record.get('file_name'),
                        winid: 'psr_task_reports_authority_winid'
                    },
                    fullScreen: true,
                    draggable: true,
                    title: '报表数据授权'
                });
            }
        ]]></script>
        <a:dataSets>
            <a:dataSet id="rpt_task_reports_query_ds"/>
            <a:dataSet id="rpt_task_reports_ds" autoQuery="true" canQuery="true" fetchAll="false" model="psr.PSR1030.psr_task_reports_for_query" pageSize="15" queryDataSet="rpt_task_reports_query_ds" selectable="true"/>
        </a:dataSets>
        <a:form column="2" style="margin-left:10px;margin-top:10px;" title="报表查询" width="600">
            <a:datePicker name="creation_date_from" bindTarget="rpt_task_reports_query_ds" prompt="创建时间从" width="200">
                <a:events>
                    <a:event name="enterdown" handler="query"/>
                </a:events>
            </a:datePicker>
            <a:datePicker name="creation_date_to" bindTarget="rpt_task_reports_query_ds" prompt="创建时间到" width="200">
                <a:events>
                    <a:event name="enterdown" handler="query"/>
                </a:events>
            </a:datePicker>
        </a:form>
        <a:hBox style="margin-left:7px;">
            <a:button click="query" text="HAP_QUERY"/>
            <a:button click="reset" text="HAP_RESET"/>
        </a:hBox>
        <a:grid id="rpt_task_reports_ds_grid" bindTarget="rpt_task_reports_ds" marginHeight="150" marginWidth="30" navBar="true">
            <a:toolBar>
                <a:button click="remove_select" icon="${/request/@context_path}/images/remove.gif" style="height:16px;" text="HAP_DELETE"/>
            </a:toolBar>
            <a:columns>
                <!-- <a:column name="task_description" prompt="任务" width="70"/> -->
                <!-- <a:column name="session_id" prompt="会话" width="70"/> -->
                <a:column name="user_description" align="center" prompt="用户" width="100"/>
                <a:column name="role_name" align="center" prompt="角色" width="70"/>
                <a:column name="company_short_name" align="center" prompt="公司" width="70"/>
                <!-- <a:column name="lang_description" prompt="语言" width="100"/> -->
                <a:column name="creation_date_view" align="center" prompt="创建时间" width="150"/>
                <!-- <a:column name="url" prompt="请求地址" width="200"/> -->
                <a:column name="file_path" align="center" prompt="下载" renderer="down" width="270"/>
                <a:column name="config_report_result_authority" align="center" prompt="报表数据授权" renderer="config_report_result_authority_renderer" width="120"/>
                <!-- <a:column name="download" align="center" prompt="下载" width="270"/>-->
                <!-- <a:column name="delete" align="center" prompt="删除文件" renderer="remove" width="70"/> -->
            </a:columns>
            <a:editors/>
        </a:grid>
    </a:view>
</a:screen>