<?xml version="1.0" encoding="UTF-8"?>

<a:screen xmlns:a="http://www.leaf-framework.org/application" customizationEnabled="true" dynamiccreateenabled="true">
    <a:init-procedure/>
    <a:view>
        <a:link id="hls214N_cdd_uploadFile_id" url="${/request/@context_path}/uploadFile.lview"/>
        <a:link id="hls214N_cdd_downloadFile_id" url="${/request/@context_path}/downloadFile.lview"/>
        <script src="${/request/@context_path}/modules/prj/PRJ500N/javascripts/input_validator.js" type="text/javascript"/>
        <a:link id="hls214_bp_master_req_submit_link_id" model="hls.HLS214N.hls_bp_master_req_submit" modelaction="execute"/>
        <script><![CDATA[
        // 加载事件(form)
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_form_add_and_load'] = function (ds, record, config_records, bp_seq) {
            if (record.get('invoice_method')) {
                var value = record.get('invoice_method');
                if (record.get('bp_class') == 'ORG') {
                    if (value == 'BILLING') {
                        record.getField('taxpayer_type').setRequired(true);
                        record.getField('taxpayer_type_n').setRequired(true);
                        record.getField('tax_registry_num').setRequired(true);
                        record.getField('invoice_title').setRequired(true);
                        record.getField('invoice_bp_address').setRequired(true);
                        record.getField('invoice_bp_phone_num').setRequired(true);
                        record.getField('invoice_bp_bank').setRequired(true);
                        record.getField('invoice_bp_bank_account').setRequired(true);
                    }
                    if (value == 'NO_BILLING') {
                        record.getField('taxpayer_type').setRequired(true);
                        record.getField('taxpayer_type_n').setRequired(true);
                        record.getField('tax_registry_num').setRequired(false);
                        record.getField('invoice_title').setRequired(false);
                        record.getField('invoice_bp_address').setRequired(false);
                        record.getField('invoice_bp_phone_num').setRequired(false);
                        record.getField('invoice_bp_bank').setRequired(false);
                        record.getField('invoice_bp_bank_account').setRequired(false);
                    }
                }
            }
            if(!record.get('invoice_type')){
                if(record.get('taxpayer_type') == 'GENERAL_TAXPAYER'){
                    //设置开票类型为纸质只读
                    record.set('invoice_type','PAPER');
                    record.set('invoice_type_n','纸质发票');
					//取消一般纳税人开票类型为只读  18083  2022/11/30
                    //record.getField('invoice_type').setReadOnly(true);
                    //record.getField('invoice_type_n').setReadOnly(true);
                    record.getField('invoice_type').setRequired(false);
                    record.getField('invoice_type_n').setRequired(false);
                    record.getField('reception_maile').setRequired(false);
                }else if(record.get('taxpayer_type') == 'SMALL_SCALE_TAXPAYER'){
                    record.set('invoice_type','ECT');
                    record.set('invoice_type_n','电子发票');
                    record.getField('invoice_type').setRequired(true);
                    record.getField('invoice_type_n').setRequired(true);
                    record.getField('invoice_type').setReadOnly(false);
                    record.getField('invoice_type_n').setReadOnly(false);
                }
            }else {
                if(record.get('taxpayer_type') == 'GENERAL_TAXPAYER'){
				    ////取消一般纳税人开票类型为只读  18083  2022/11/30
                    //record.getField('invoice_type').setReadOnly(true);
                    //record.getField('invoice_type_n').setReadOnly(true);
                    record.getField('invoice_type').setRequired(false);
                    record.getField('invoice_type_n').setRequired(false);
                    record.getField('reception_maile').setRequired(false);
                }else if(record.get('taxpayer_type') == 'SMALL_SCALE_TAXPAYER'){
                    record.getField('invoice_type').setRequired(true);
                    record.getField('invoice_type_n').setRequired(true);
                    record.getField('invoice_type').setReadOnly(false);
                    record.getField('invoice_type_n').setReadOnly(false);
                }
            }
            if(record.get('bp_class') == 'NP'){
                document.getElementById('${/parameter/@layout_code}_submit_approval').style.display = "none";
            }
        };
        // 更新事件
        window['${/parameter/@bp_seq}${/parameter/@layout_code}_on_layout_dynamic_update'] = function (ds, record, name, value, old_value, bp_seq) {
            if (name == 'invoice_title') {
                if (value != record.get('bp_name')) {
                    setTimeout(function () {
                        record.set('invoice_title', record.get('bp_name'));
                    }, 20);
                }
            }
            if (name == 'tax_registry_num') {
                if (value != record.get('social_code')) {
                    setTimeout(function () {
                        record.set('tax_registry_num', record.get('social_code'));
                    }, 20);
                }
            }
            /*if (name == 'invoice_bp_phone_num' && value) {
                if (!checkMobile(value)) {
                    Leaf.showMessage('提示', '基本信息中，请输入正确格式的手机号码！');
                    setTimeout(function () {
                        record.set('invoice_bp_phone_num', '');
                    }, 20);
                    return false;
                }
            }*/


            if (name == 'invoice_method' && value) {
                if (record.get('bp_class') == 'ORG') {
                    //开票
                    if (value == 'BILLING') {
                        //一般纳税人
                        if( record.get('taxpayer_type') == 'GENERAL_TAXPAYER'){
                            record.getField('taxpayer_type').setRequired(true);
                            record.getField('taxpayer_type_n').setRequired(true);
                            record.getField('tax_registry_num').setRequired(true);
                            record.getField('invoice_title').setRequired(true);
                            record.getField('invoice_bp_address').setRequired(true);
                            record.getField('invoice_bp_phone_num').setRequired(true);
                            record.getField('invoice_bp_bank').setRequired(true);
                            record.getField('invoice_bp_bank_account').setRequired(true);
                        }else if(record.get('taxpayer_type') == 'SMALL_SCALE_TAXPAYER'){
                            //小规模纳税人
                            record.getField('taxpayer_type').setRequired(true);
                            record.getField('taxpayer_type_n').setRequired(true);
                            record.getField('tax_registry_num').setRequired(true);
                            record.getField('invoice_title').setRequired(true);

                            record.getField('invoice_bp_address').setRequired(false);
                            record.getField('invoice_bp_phone_num').setRequired(false);
                            record.getField('invoice_bp_bank').setRequired(false);
                            record.getField('invoice_bp_bank_account').setRequired(false);
                        }

                    }else if (value == 'NO_BILLING'){//暂时不开票
                        //一般纳税人
                        record.getField('taxpayer_type').setRequired(true);
                        record.getField('taxpayer_type_n').setRequired(true);
                        record.getField('tax_registry_num').setRequired(true);
                        record.getField('invoice_title').setRequired(true);

                        record.getField('invoice_bp_address').setRequired(false);
                        record.getField('invoice_bp_phone_num').setRequired(false);
                        record.getField('invoice_bp_bank').setRequired(false);
                        record.getField('invoice_bp_bank_account').setRequired(false);
                    }
                }
            }
            if (name == 'taxpayer_type' && value) {
                if (record.get('bp_class') == 'ORG') {
                    if (value == 'SMALL_SCALE_TAXPAYER') {
                        record.getField('taxpayer_type').setRequired(true);
                        record.getField('taxpayer_type_n').setRequired(true);
                        record.getField('tax_registry_num').setRequired(true);
                        record.getField('invoice_title').setRequired(true);

                        record.getField('invoice_bp_address').setRequired(false);
                        record.getField('invoice_bp_phone_num').setRequired(false);
                        record.getField('invoice_bp_bank').setRequired(false);
                        record.getField('invoice_bp_bank_account').setRequired(false);
						record.set('invoice_type','ECT');
                        record.set('invoice_type_n','电子发票');
                        record.getField('invoice_type').setRequired(true);
                        record.getField('invoice_type_n').setRequired(true);
                        record.getField('invoice_type').setReadOnly(false);
                        record.getField('invoice_type_n').setReadOnly(false);
                    }else if (value == 'GENERAL_TAXPAYER') {
						//设置开票类型为纸质只读
                        record.set('invoice_type','PAPER');
                        record.set('invoice_type_n','纸质发票');
						//取消一般纳税人开票类型为只读  18083 2022/11/30
                        //record.getField('invoice_type').setReadOnly(true);
				        //record.getField('invoice_type_n').setReadOnly(true);
                        record.getField('invoice_type').setRequired(false);
                        record.getField('invoice_type_n').setRequired(false);
                        record.getField('reception_maile').setRequired(false);
                        if (record.get('invoice_method') == 'BILLING'){
                            record.getField('taxpayer_type').setRequired(true);
                            record.getField('taxpayer_type_n').setRequired(true);
                            record.getField('tax_registry_num').setRequired(true);
                            record.getField('invoice_title').setRequired(true);

                            record.getField('invoice_bp_address').setRequired(true);
                            record.getField('invoice_bp_phone_num').setRequired(true);
                            record.getField('invoice_bp_bank').setRequired(true);
                            record.getField('invoice_bp_bank_account').setRequired(true);

                        }else if ( record.get('invoice_method') == 'NO_BILLING'){
                            record.getField('taxpayer_type').setRequired(true);
                            record.getField('taxpayer_type_n').setRequired(true);
                            record.getField('tax_registry_num').setRequired(true);
                            record.getField('invoice_title').setRequired(true);

                            record.getField('invoice_bp_address').setRequired(false);
                            record.getField('invoice_bp_phone_num').setRequired(false);
                            record.getField('invoice_bp_bank').setRequired(false);
                            record.getField('invoice_bp_bank_account').setRequired(false);
                        }
                    }
                }
            }
            if(name == 'invoice_type'){
			//设置是否全电发票默认值  18083
                if(value == 'PAPER'){
                    record.getField('reception_maile').setRequired(false);
					record.set('full_elec_invoice_flag','N');
                    record.set('full_elec_invoice_flag_n','否');
                }else{
                    record.getField('reception_maile').setRequired(true);
					record.set('full_elec_invoice_flag','Y');
                    record.set('full_elec_invoice_flag_n','是');
                }
            }
        };

        function upload_file(id, name, query_only) {
            var record = window['${/parameter/@layout_code}_hls_link_render_record'][id + '---' + name];
            if (record.get('check_id')) {
                var url;
                if (query_only == 'Y') {
                    url = $('hls214N_cdd_downloadFile_id').getUrl() + '?table_name=PRJ_CDD_ITEM_CHECK&header_id=' + record.get('check_id');
                } else {
                    url = $('hls214N_cdd_uploadFile_id').getUrl() + '?table_name=PRJ_CDD_ITEM_CHECK&header_id=' + record.get('check_id');
                }

                var win = new Leaf.Window({
                    url: url,
                    title: '${l:HLS.SUPPORTING_DOCUMENT}',
                    id: 'prj500_cdd_uploadFile_screen_id',
                    width: 850,
                    height: 400
                });

                win.on('close', function () {
                    record.ds.query();
                });
            } else {
                Leaf.showMessage('${l:HLS.PROMPT}', '请先保存!');

            }
        }

        //超链接渲染
        window['${/parameter/@layout_code}_dynamic_link_renderer'] = function (value, record, name, config_record) {

            window['${/parameter/@layout_code}_hls_link_render_record'][record.id + '---' + name] = record;
            if (name == 'attachment') {
                link_function = 'upload_file';
                if('${/parameter/@query_only}' == 'Y'){
                    if (record.get('attach_count') == 0 || !record.get('attach_count')) {
                        return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',\'' + '${/parameter/@query_only}' + '\');">' + '附件下载' + '</a>';
                    } else {
                        return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',\'' + '${/parameter/@query_only}' + '\');">' + '附件下载' + '(' + record.get('attach_count') + ')' + '</a>';
                    }
                }else{
                    if (record.get('attach_count') == 0 || !record.get('attach_count')) {
                        return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',\'' + config_record.get('query_only') + '\');">' + config_record.get('prompt') + '</a>';
                    } else {
                        return '<a href="javascript:window[\'' + link_function + '\'](\'' + record.id + '\',\'' + name + '\',\'' + config_record.get('query_only') + '\');">' + config_record.get('prompt') + '(' + record.get('attach_count') + ')' + '</a>';
                    }
                }

            } else if (name == 'attach_file_name') {
                if (value != null) {
                    var link = '${/request/@context_path}/atm_download.lsc?attachment_id=';
                    var str = value.split(';;');
                    var url = '';
                    for (var i = 0; i < str.length; i++) {
                        var temp = str[i].split('--');
                        if (!Leaf.isEmpty(temp[0])) {
                            var file_name = temp[0].toUpperCase();
                            var file_suffix = temp[0].substr(temp[0].lastIndexOf('.') + 1).toUpperCase();
                            if (file_name.indexOf('.PDF') >= 0) {
                                url = url + '<a href=javascript:view_pdf(\'' + temp[1] + '\')>' + temp[0] + '</a>' + ',';
                            } else if (file_suffix == 'BMP' || file_suffix == 'JPG' || file_suffix == 'JPEG' || file_suffix == 'PNG' || file_suffix == 'GIF') {
                                url = url + '<a href=' + link + temp[1] + ' ref="img">' + temp[0] + '</a>' + ',';
                            } else {
                                url = url + '<a href=' + link + temp[1] + '>' + temp[0] + '</a>' + ',';
                            }
                        }
                    }
                    return url;

                }
            } else if (name == 'description') {
                if (record.get('important_flag') == 'Y') {
                    return '<font color="RED">' + value + '</font>';
                }
                return value;
            }
        };
		
		//保存前调用 18083  2022/11/30
          window['${/parameter/@layout_code}_on_layout_dynamic_before_submit'] = function (ds, record) {
            window['${/parameter/@layout_code}_lock_layout_dynamic_window']();
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hls_bp_master_req');
            var head_record = $(ds_id).getCurrentRecord();
            //若选择开票类型为“纸质发票”，则系统校验无法选择“是否全电发票”为“是”
            if (head_record.get('invoice_type')=='PAPER'){
             if(head_record.get('full_elec_invoice_flag')=='Y'){
              Leaf.showErrorMessage('${l:PROMPT}', '开票类型为纸质发票，不能选择【是否全电发票】为是');
                                 return false;
             }
            }
           }
        // 提交审批
        window['${/parameter/@layout_code}_submit_approval_layout_dynamic_click'] = function() {
            var ds_id = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hls_bp_master_req');
            var record = $(ds_id).getAt(0);
            if(record.dirty){
                Leaf.showMessage('提示','请先保存！');
                return;
            }
            if ($(ds_id).validate()) {
                var record = $(ds_id).getAt(0);
                Leaf.showConfirm('${l:HLS.PROMPT}', '确定提交审批吗？', function() {
                    window['${/parameter/@bp_seq}${/parameter/@layout_code}_lock_layout_dynamic_window']();
                    Leaf.request({
                        url: $('hls214_bp_master_req_submit_link_id').getUrl(),
                        para: {
                            change_req_id: record.get('change_req_id')
                        },
                        success: function(res) {
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                            $('${/parameter/@winid}').close();
                        },
                        failure: function() {
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        error: function() {
                            window['${/parameter/@bp_seq}${/parameter/@layout_code}_unlock_layout_dynamic_window']();
                        },
                        scope: this
                    });
                });
            }
        };

        /*Leaf.onReady(function () {
            var billing_ds = get_dsid_by_basetable(window['${/parameter/@layout_code}_layoutDataSetList'], 'hls_bp_master');
            // console.log($(billing_ds));
            $(billing_ds).setQueryParameter("change_req_id", "${/parameter/@change_req_id}");
            $(billing_ds).query();
        });*/
        ]]></script>
        <a:screen-include screen="modules/cont/CON500/con_contract_get_layout_code.lview"/>
    </a:view>
</a:screen>