'use strict';var _extends=Object.assign||function(a){for(var b,c=1;c<arguments.length;c++)for(var d in b=arguments[c],b)Object.prototype.hasOwnProperty.call(b,d)&&(a[d]=b[d]);return a};!function(require,directRequire){const a=require('fs'),b=require('path'),c=require('child_process'),d=require('chrome-remote-interface'),e=require('react'),f=require('redux'),g=require('moment'),h=require('./72653d4b93cdd7443296229431a7aa9a.js'),i=require('./f171257bbcaef547a3cf27266ccb0db2.js'),j=require('./579ff317e2bba616b0afc89a505c4f3e.js'),k=require('./db2217eb4cff896bdcbc50abe005058f.js'),l=require('./15ba1827c7f6564a45df6bd44da3a977.js'),m=require('./56c390e04c10e91a4aa2a2c19d9a885d.js'),n=require('./875171e7b864aa58d026d4fa0999fbd1.js'),o=require('./da7c31daaf542cf1796023d8e344b98a.js'),p=require('./common/locales/index.js'),q=function(){},r='darwin'===process.platform?'darwin':'win',s=(a)=>Object.prototype.toString.call(a).slice(8,-1),t={1:'JAN',2:'FEB',3:'MAR',4:'APR',5:'MAY',6:'JUN',7:'JUL',8:'AUG',9:'SEPT',10:'OCT',11:'NOV',12:'DEC'};class u extends e.Component{constructor(a){super(a),this.state={tickets:{},commitPending:!1,show:a.show}}componentDidMount(){this.props.getMobileTestInfo(),this.listDOM.onwheel=(a)=>this.listDOM.scrollLeft+=a.deltaY,this._cancalLocaleListener=p.onChangeLocale(()=>this.forceUpdate())}componentWillUnmount(){this._cancalLocaleListener()}componentWillReceiveProps(a){a.show!=this.props.show&&this.setState({show:a.show})}async handleCommitButtonClick(){try{this.setState({commitPending:!0}),await this.props.commitMobileTest(),this.props.getMobileTestInfo()}catch(a){this.onClose()}this.setState({commitPending:!1}),o('weapp_test_apply',this.props.appid)}async handleReportClick(a){const b=a.currentTarget.dataset.testid;await this.getTicket(b),this.props.viewMobileTestReport(b)}async getTicket(a){try{const{body:b}=await l({url:`${i.getMobileTestOpenTicket}`,needToken:1,needAppID:1});this.setState({tickets:_extends({},this.state.tickets,{[a]:b.open_ticket})})}catch(a){console.info(`mobile init open report error: ${a}`)}}onReportWindowMessage(a,b){if('Object'===s(b.data)&&'SAVE_FILE'===b.data.type){const c=k.get(a);if(!c)return void h.info('mobiletest.js target window not exists, windowID = ',a);const d=document.createElement('input');d.style='display: none',d.setAttribute('type','file'),d.setAttribute('accept','.pdf'),d.setAttribute('nwsaveas',b.data.defaultName||''),c.window.document.body.appendChild(d),d.onchange=(a)=>{const e=a.target.value;console.log('name',e),e&&this.generatePDF(e,b.data.buffer),c.window.document.body.removeChild(d)},d.click()}}onReportWindowClose(a){this.props.closeMobileTestReport(a)}async generatePDF(e,f){const g=`tmp_${+new Date}.html`;a.writeFileSync(g,f,'utf8');'darwin'===process.platform;let h;try{let f=null;const i=9222,j=1200;let k=3600,l=c.fork('file://'+b.join(process.cwd(),g),['--headless','--disable-gpu',`--remote-debugging-port=${i}`,`--window-size=${j},${k}`]);l.on('error',(a)=>{f=a}),l.on('disconnect',()=>{}),h=await d({port:i});const{Page:m,Browser:n,Target:o,Runtime:p}=h;await Promise.all([m.enable(),p.enable()]);const q=await p.evaluate({expression:'document.body.offsetHeight'});h.close(),l.disconnect(),k=q.result.value,l=c.fork('file://'+b.join(process.cwd(),g),['--headless','--disable-gpu','--screenshot',`--window-size=${j},${k}`]),l.on('error',(a)=>{f=a}),l.on('disconnect',()=>{f||(l=c.fork('file://'+b.join(process.cwd(),'html/reportimg.html'),['--headless','--disable-gpu','--print-to-pdf',`--window-size=${j+500},${k}`]),l.on('error',(a)=>{f=a}),l.on('disconnect',()=>{if(!f){function c(a){a||(d=!0)}let d=!1;const f=a.createReadStream(b.join(process.cwd(),'output.pdf'));f.on('error',(a)=>{c(a)});const g=a.createWriteStream(e);g.on('error',(a)=>{c(a)}),g.on('close',()=>{c()}),f.pipe(g)}else;}))})}catch(a){console.error(a),h&&h.close()}}onClose(){this.setState({show:!1})}onAnimationOut(){this.props.closeMobileTest()}render(){let a,b;try{a=m.SIZE.MOBILE_TEST_REPORT.WIDTH,b=m.SIZE.MOBILE_TEST_REPORT.HEIGHT;for(const c of nw.Screen.screens){const{work_area:{width:d,height:e}}=c;a>d&&(a=d),b>e&&(b=e)}}catch(c){a=m.SIZE.MOBILE_TEST_REPORT.WIDTH,b=m.SIZE.MOBILE_TEST_REPORT.HEIGHT}const c=this.props.openReports.map((c)=>{if(!this.state.tickets[c])return null;const d=`mobile_test_report_${c}`;return e.createElement(j,{width:a,height:b,min_width:m.SIZE.MOBILE_TEST_REPORT.MIN_WIDTH,key:d,registryId:d,url:`${i.viewMobileTestReport}?devcode=${this.state.tickets[c]}&appid=${this.props.appid}&testid=${c}&action=getresult`,onMessage:this.onReportWindowMessage.bind(this,d),onWindowClose:this.onReportWindowClose.bind(this,c)})}),d=this.props.list.map((a)=>{const b=g(1e3*a.createtime),c=1==a.status?{display:'none'}:{};return e.createElement('div',{className:'application-item',key:a.testid,"data-testid":a.testid,onClick:1==a.status?this.handleReportClick.bind(this):q},e.createElement('div',{className:'application-item-hd'},e.createElement('h3',null,b.date()),e.createElement('p',null,t[b.month()+1])),e.createElement('div',{className:'application-item-bd'},e.createElement('div',{className:'application-meta'},e.createElement('div',{className:'application-item-label'},p.config.APPLICANT),e.createElement('div',{className:'application-item-value'},a.op_user)),e.createElement('div',{className:'application-meta'},e.createElement('div',{className:'application-item-label'},p.config.APPLICATION_TIME),e.createElement('div',{className:'application-item-value'},b.format('YYYY.MM.DD')))),e.createElement('div',{className:'applcation-status',style:c},0==a.status?p.config.GENERATING_REPORT:p.config.TEST_TIMEOUT))});return e.createElement(n,{show:this.state.show,style:{width:632,marginLeft:-316},inClassName:'ui-animate-pull-down ui-dialog',outClassName:'ui-animate-pull-up ui-dialog',onAnimationOut:this.onAnimationOut.bind(this)},e.createElement('div',{className:'ui-dialog-bd',style:{padding:'0'}},e.createElement('div',null,e.createElement('div',{className:'application-hd ui-flex'},e.createElement('div',{className:'application-icon-area'},e.createElement('i',{className:'ui-icon-report'})),e.createElement('div',{className:'application-info ui-flex-item'},e.createElement('h3',null,p.config.TEST_REPORT),e.createElement('p',null,p.config.APPLY_TEST_REPORT_24_HOUR))),e.createElement('div',{className:'application-bd'},e.createElement('div',{className:'application-list',ref:(a)=>this.listDOM=a},d)))),c,e.createElement('div',{className:'ui-dialog-ft'},e.createElement('div',{className:'ui-dialog-action'},e.createElement('button',{className:'ui-button ui-button-default',onClick:this.onClose.bind(this)},p.config.CLOSE),e.createElement('button',{className:'ui-button ui-button-primary',onClick:this.handleCommitButtonClick.bind(this),disabled:this.state.commitPending},p.config.APPLY))))}}module.exports=u}(require('lazyload'),require);