﻿var SouthOrderForm;
var ContractStore;
var ContractPrice;
var ContractStoreTemp=new Ext.data.Store({
    proxy:new Ext.data.HttpProxy({url:'QueryData/QueryContract.aspx',timeout:300000//五分钟 
    }),
    reader:new Ext.data.ArrayReader({},[
    {name:'value'},
    {name:'text'}])
    ,sortInfo:{field:'value',direction:'asc'}
    });    
    
    
    
     var Text1 = new Ext.form.NumberField({  
             fieldLabel : '可开',  
             name : 'txtAvailable',  
             width : 117,
             //disabled:true,
             id:'txtAvailableId'
              //xtype:"textfield",
//            name:'txtAvailable',
//            id:'txtAvailableId',
//            fieldLabel: '可开',
         }); 
    
    var Text2 = new Ext.form.TextField({  
             fieldLabel : '价格',  
             name : 'txtPrice',  
             width : 117  
//                                                name:'txtPrice',
//                                                fieldLabel: '价格',  
//                                                blankText:'请输入价格',                                              
//                                                width:117
         }); 
         var Text3 = new Ext.form.NumberField({  
             fieldLabel : '数量',  
             name : 'txtCount',  
             width : 117  
 //name:'txtCount',
//                                                blankText:'请输入数量',
//                                                fieldLabel: '数量',
         }); 
    
    
    
    
    
    
function SouthOrderAreaInit()
{
    //期货合约列表ds
    ContractStore = new Ext.data.Store({
    proxy:new Ext.data.HttpProxy({url:'QueryData/QueryContract.aspx'}),
    reader:new Ext.data.ArrayReader({},[
    {name:'value'},
    {name:'text'}])
    ,sortInfo:{field:'value',direction:'asc'}
    });    
// form start------------------------------------------------------------------------------------------------------------
     SouthOrderForm = new Ext.form.FormPanel({
            layout:'table',
            title: 'form',
            split: true,//添加分割线是否可以改变该panel的大小
		    minSize: 40,//设置拖动的最小拖动值
    	    maxSize: 800,//设置拖动的最大拖动值
		    collapsible:true,//是否让panel能自动缩放
		    collapseMode:'mini',//在分割线处出现按钮
            labelWidth: 50,
            frame:true,
            hideCollapseTool:false,
            width: Ext.lib.Dom.getViewportWidth()*2/5,
            //autoWidth:true,
            monitorResize:true,
            title: '委托下单',
            region: 'west',
            defaults: {
                //frame:true
            },
            layoutConfig:{
                columns:2
                //columnWidth :400
              },
            //url:'QueryData/OrderForm.aspx',
            //reader:formReader,
            items: [
                         
                           //item1
                         {
                            width:Ext.lib.Dom.getViewportWidth()*6/30,
                            height:Ext.lib.Dom.getViewportHeight()*30/72,
                            rowspan:1,
                            colspan:1,
                            layout:'form',
                            items:[
                                    {   xtype:"combo",
                                        triggerAction: 'all',
                                        fieldLabel: '合约',
                                        name:'comboContract',
                                        id:'comboContract',
                                        store: ContractStore,
                                        width:100,
                                        valueField: 'value',
                                        displayField: 'value',
                                        mode: 'local',
                                        emptyText:'',
                                        value:''
                                        //,msgTarget:'请选择合约'
                                        //,readOnly:true
                                        //,allowBlank:false
                                        ,local:'remote'
                                        ,minListWidth:150
                                        ,listeners:{   "select":function(combox){
                                                          var contractValue=combox.getValue();
                                                          var comboOrderType= SouthOrderForm.getForm().findField('comboOrderType');
                                                           comboOrderType.setValue("0");
                                                          var txtCount= SouthOrderForm.getForm().findField('txtAvailableId');
                                                           txtCount.setValue("");
                                                          var txtCount= SouthOrderForm.getForm().findField('txtCount');
                                                           txtCount.setValue("1");
                                                           //setPriceFromContract(contractValue); 
                                                          var txtPrice= SouthOrderForm.getForm().findField('txtPrice'); 
                                                          //报价方式  市价   
                                                            var comboPriceType=SouthOrderForm.getForm().findField('comboPriceTypeId');  
                                                            if(comboPriceType.getValue()=="2")
                                                            {                                                                    
                                                                txtPrice.setValue(""); 
                                                            } 
                                                           firstSetPrice=true;
//                                                           FlashIframe.setFlashContract(contractValue);                             
                                                        },
                                                        "blur":function(combox){
                                                            if(ContractStoreTemp.find('value',combox.getRawValue(),0,false,false)==-1)
                                                            {
                                                                Ext.MessageBox.alert("提示","指定的合约不存在或不可交易！",function(){
                                                                    //combox.focus(true,true);
                                                                    });
                                                                return;
                                                            }else
                                                            {
//                                                                if(combox.getRawValue()!=combox.getValue())
//                                                                {
//                                                                    Ext.MessageBox.alert("提示","指定的合约不存在或不可交易！",function(){
//                                                                    combox.focus(true,true);});
//                                                                    return;
//                                                                }
                                                               //combox.selectByValue(combox.getRawValue(),true);
                                                               //combox.setValue(combox.getRawValue());
                                                               var contractValue=combox.getRawValue();
                                                               var comboOrderType= SouthOrderForm.getForm().findField('comboOrderType');
                                                                comboOrderType.setValue("0");
                                                               var txtAvailable= SouthOrderForm.getForm().findField('txtAvailableId');
                                                               txtAvailable.setValue("");                                                               
                                                               var txtCount= SouthOrderForm.getForm().findField('txtCount');
                                                                txtCount.setValue("1");
                                                                //setPriceFromContract(contractValue); 
                                                               var txtPrice= SouthOrderForm.getForm().findField('txtPrice'); 
                                                               //报价方式  市价   
                                                                var comboPriceType=SouthOrderForm.getForm().findField('comboPriceTypeId');  
                                                                if(comboPriceType.getValue()=="2")
                                                                {                                                                    
                                                                    txtPrice.setValue(""); 
                                                                }
                                                               firstSetPrice=true;
                                                               setFlashContractFromTop(contractValue);  
                                                            }
                                                        }
                                                   }
                                      }, 
                                       {       
                                        xtype:"combo",
                                        readOnly:true,
                                        fieldLabel: '报价',
                                        name:'comboPriceType',
                                        id:'comboPriceTypeId',
                                        triggerAction: 'all',
                                        store: new Ext.data.SimpleStore({
                                                            fields: ['value', 'text'],
                                                            data: [
                                                                //['1', '任意价'],
                                                                ['2', '限价'],
                                                                ['1', '市价']
                                                            ]
                                                        }),
                                        editable:false,
                                        width:100,
                                        displayField: 'text',
                                        valueField: 'value',
                                        mode: 'local',
                                        emptyText:'出价类型不能为空',
                                        value:2
                                        //,msgTarget:'请选择出价类型'
                                        //,allowBlank:false
                                        ,listeners:{   "select":function(combox){
                                                        var combPriTypeValue=combox.getValue();
                                                        var contractValue=Ext.get('comboContract').getValue();
                                                        var txtPrice= SouthOrderForm.getForm().findField('txtPrice');                                                          
                                                           if(combPriTypeValue=="1")//市价
                                                           {    
                                                                txtPrice.setValue("市价"); 
                                                                txtPrice.disable();
                                                           } else
                                                           {
                                                                //setPriceFromContract(contractValue); 
                                                                txtPrice.setValue(ContractPrice);
                                                                txtPrice.enable();
                                                           }    
                                                        }
                                                   }
                                     },                        
                                     {  
                                      layout : 'form',  
                                        items : [Text1]  
//                                        xtype:"textfield",
//                                        name:'txtAvailable',
//                                        id:'txtAvailableId',
//                                        fieldLabel: '可开',
//                                        //hidden:true,
//                                        //hideLabel:true,
//                                        width:117,
//                                        //allowBlank:true,
//                                        disabled:true
                                                                     
                                      }
//                                      ,{       
//                                            xtype:"numberfield",
//                                            name:'txtBuyPrice',
//                                            id:'txtBuyPriceId',
//                                            fieldLabel: '买价',
//                                            width:117,
//                                            allowBlank:true,
//                                            disabled:true
//                                     }
                                     ,{
                                        xtype:"button",
                                        minWidth:90,    
                                        text: '查可开数量',
                                        itemCls:'btnBuy',
                                        handler:function(){      
                                            queryBuyAbleCount();                                             
                                        }  
                                       },
                                       {
                                       height:10 
                                       },
                                       {
                                            width:Ext.lib.Dom.getViewportWidth()*2/5,
                                            height:Ext.lib.Dom.getViewportHeight()*1/9,
                                            rowspan:1,
                                            columns:1,
                                            layout:'table',
                                            layoutConfig:{
                                            columns:3
                                            },
                                            defaults: {
                                                //frame:true
                                                    },
                                                    items:[{
                                                    xtype:"button",
                                                    text: '复 位',
                                                    minWidth:90, 
                                                    handler:function(){
                                                   reSetOrderForm();  
                                                   tempContractPrice="";
                                                   ContractPrice="";
                                                    }
                                              }
                                              ,{
                                                width:20
                                              }
                                            ,{       
                                                xtype:"checkbox",
                                                name:'checkboxName',
                                                boxLabel: '一键下单',  
                                                itemCls:'required',                                             
                                                width:100
                                               ,hideLabel:true
                                               ,listeners:{"check":function(combox,checked){
                                                        if(successLogin==false) return;
                                                        if(checked) 
                                                        {
                                                          Ext.MessageBox.confirm('警告','如果启用一键下单，则按下“买入”按钮或“卖出”按钮时，委托将直接发出，不再需要确认。<br/>警告：如果发生误操作，所有责任由您本人承担。是否启用一键下单？',function(btn)
                                                            {
                                                                if(btn=='yes')
                                                                { 
                                                                  setUserCookie('OrderNoAlram','true');
                                                                }else
                                                                {
                                                                    combox.setValue(false);
                                                                }		            
                                                            });
                                                        }
                                                        //else setUserCookie('OrderNoAlram','false');
                                                        }
                                                   }
                                        } 
                                      ]
                                       }
                                    ]                
                             },
                              //item2
                                {   
                                    width:Ext.lib.Dom.getViewportWidth()*7/30,
                                    height:Ext.lib.Dom.getViewportHeight()*30/72,
                                    rowspan:1,
                                    colspan:1,
                                    layout:'form',
                                    items:[
                                            {
                                                xtype:"combo"
                                                ,readOnly:true,
                                                triggerAction: 'all',
                                                fieldLabel: '开平',
                                                name: 'comboOrderType',
                                                id:'comboOrderTypeid',
                                                store: new Ext.data.SimpleStore({
                                                            fields: ['value', 'text'],
                                                            data: [
                                                                ['0', '开仓'],
                                                                ['3', '平今'],
                                                                ['1', '平仓']
                                                                //['2', '强平'],                                                                
                                                                //['4', '平昨']
                                                            ]
                                                        }),
                                                editable:false,
                                                width:100,
                                                displayField: 'text',
                                                valueField: 'value',
                                                mode: 'local',
                                                emptyText:'开平不能为空',
                                                value:0
                                                //,allowBlank:false
                                            },
                                            {       
                                            layout : 'form',  
                                        items : [Text2]  
//                                                xtype:"textfield",
//                                                name:'txtPrice',
//                                                fieldLabel: '价格',  
//                                                blankText:'请输入价格',                                              
//                                                width:117
                                                //,allowBlank:false
                                                //,decimalPrecision:0
                                                //,emptyText:"当前价"
                                            },
                                            {   
                                            layout : 'form',  
                                        items : [Text3]  
//                                                xtype:"numberfield",
//                                                name:'txtCount',
//                                                blankText:'请输入数量',
//                                                fieldLabel: '数量',
//                                                width:117
//                                                ,value:1
                                                //,allowBlank:false
                                                    
                                               
                                            }
//                                            ,{       
//                                            xtype:"numberfield",
//                                            name:'txtSalePrice',
//                                            id:'txtSalePriceId',
//                                            fieldLabel: '卖价',
//                                            width:100,
//                                            allowBlank:true,
//                                            disabled:true
//                                            }    
                                            ,{  xtype:"textfield",
                                                hideLabel:true,
                                                hidden:true
                                             },
                                             {
                                             height:10
                                             },
                                             {
                                                    width:Ext.lib.Dom.getViewportWidth()*3/12,
                                                    height:Ext.lib.Dom.getViewportHeight()*1/9,
                                                    //rowspan:2,
                                                    layout:'table',
                                                    layoutConfig:{
                                                    columns:3
                                                    },
                                                    defaults: {
                                                        //frame:true
                                                    },
                                                    items:[
                                                           {    
                                                            rowspan:1,
                                                            colspan:1,                                   
                                                            xtype:"button",
                                                            text: '买入',
                                                            minWidth:90,    
                                                            cls:'btnBuytxt',
                                                            style  :'btnTable',
                                                            //height:50,
                                                            handler:function(){orderTrasaction();}
                                                           },
                                                           
                                                            {
                                                            width:5
                                                            },
                                                            { 
                                                            xtype:"button",
                                                            name:'salebtn',
                                                            text: '卖出',
                                                            cls:'btnSaletxt',
                                                            style:'btnTable',
                                                            minWidth:90,  
                                                             handler:function(){ SaleTrasaction();}
                                                            }
                                                          ]
                                             }                     
                                    ]
                                }
                                //item3
//                                 {
//                                    layout:'form',
//                                    width:1
//                         } ,
//                                //item4                               
//                                ,{
//                                    
//                                }
                               
                    ]
 });
// form end  
}

function setPriceFromContract(contract)
{
     if(contract!="")
        {            
            Ext.Ajax.request(
                        {
                            url: 'QueryData/QueryContractPricey.aspx',    
                            success:function(response)
                            {
                                var txtPrice= SouthOrderForm.getForm().findField('txtPrice'); 
                                var comboPriceType=SouthOrderForm.getForm().findField('comboPriceTypeId');
                                var contractValue=comboPriceType.getValue();     
                                if(contractValue=="1")//市价
                                   {
                                   } else
                                   {
                                        txtPrice.setValue(""); 
                                   }                          
                                
                                var result=Ext.decode(response.responseText);
                                
                                if(result.success==true)
                                { 
                                    //var txtCount= SouthOrderForm.getForm().findField('txtCount');                                  
                                    //txtCount.setValue("1");                                    
//                                    var comboPriceType=SouthOrderForm.getForm().findField('comboPriceTypeId');
//                                    var contractValue=comboPriceType.getValue();                                                        
                                       if(contractValue=="1")//市价
                                       {
                                            txtPrice.setValue("市价"); 
                                            txtPrice.disable();
                                       } else
                                       {
                                            txtPrice.setValue(""); 
                                            txtPrice.setValue(FormatNumber(result.SettlementPrice,0));  
                                            txtPrice.enable();
                                       } 
                                       window.status="正常通讯中......";
                                  }else
                                  {
                                      //runner.stopAll();
                                      stopReload();
                                      Ext.MessageBox.confirm('确认（已断开）',result.msg,function(btn)
                                            {
                                                if(btn=='yes')
                                                {    
                                                   successLogin=false;                            
                                                   callTrade();
                                                }  			            
                                            });
                                  }
                            },
                            failure:function()
                            {
                                window.status="通讯已断开,请重新登陆......";
                                var txtPrice= SouthOrderForm.getForm().findField('txtPrice'); 
                                txtPrice.setValue(""); 
                                //Ext.MessageBox.alert('错误','获取合约价格失败');
                            },
                            params:{"InstrumentID":contract}
                        });
        }                           
}