<file_sep>import Ember from 'ember';

export default Ember.Component.extend({
	init() {
		this._super(...arguments)
		this.set('hasChecked', true)
		this.set('isCheckAll', false)
	},
	click(e) {
		// click checkbox
		let _this = this;
		if(Ember.$('input.checkbox').index(Ember.$(e.target)) > -1){
			Ember.$('input.checkbox').each( (index,item) => {
				if(Ember.$(item).prop('checked')){
					_this.set('hasChecked', false)
					return false;
				}
				Ember.$('input.checkbox').length === index + 1 && _this.set('hasChecked', true)
			})
		}

		// if not button
		if(Ember.$('.M-box a').index(Ember.$(e.target)) < 0){
			return
		}			

		//page button
		if(Ember.$('.M-box').find('.jump-btn').index(Ember.$(e.target)) > -1){
			if(Ember.$('.M-box').find('.jump-ipt').val() !== ''){
				this.set('current', parseInt(Ember.$('.M-box').find('.jump-ipt').val()))
			}else{
				return;
			}
		}

		// prev/next button
		let current = this.get('current');
		if(Ember.$(e.target).hasClass('active')){
			this.set('current', Ember.$(e.target).text())
		}
		Ember.$(e.target).data('page') && this.set('current', Ember.$(e.target).data('page'))
		if(Ember.$(e.target).hasClass('next')){
			this.set('current', ++current)
		}		
		if(Ember.$(e.target).hasClass('prev')){
			this.set('current', --current)
		}
		current = this.get('current')
		let formData = Ember.$('form.form-inline').serialize()
		let url = this.get('url') + '?' + formData + '&page=' + current;
		Ember.emberAjax(url, groups => {
			this.get('advertisers.advertisers').clear().addObjects(groups)
			Ember.paging(this.advertisers, e.target);
		})
		return false;
	},
	didInsertElement() {
		//upgrade button init
		let _this = this;
		Ember.$('input.checkbox').each( (index,item) => {
			if(Ember.$(item).prop('checked')){
				_this.set('hasChecked', false)
				return false;
			}
		})

		//deal with affiliate which needs to checked
		this.get('advertisers.advertisers').forEach( (advertiser) => {
			if(advertiser.affiliate){
				let arr = advertiser.affiliate.split(',');
				advertiser.optionIndex = [];
				Ember.$('.selectpicker option').each( (index, item) => {
					if(arr.indexOf(item.innerHTML) > -1){
						advertiser.optionIndex.push(index)
					}
				})
			}
		})
		//---------------------------------------------------------------------------
		this.set('url', Ember.devUrl + Ember.$('form.form-inline').data('url'))
		Ember.$('.jplist-preloader').addClass('jplist-hide-preloader')
		//init page plugin
		Ember.paginationInit(this.advertisers.page, this.advertisers.total, this.advertisers.limit, true);
		this.set('current', this.advertisers.page)
		this.set('isCreate', true);
	},
	actions: {
		update() {
			let formData = Ember.$('form.form-inline').serialize()
			let url = this.get('url') + '?' + formData
			Ember.emberAjax(url, (groups,total) => {
				this.set('advertisers.total', total)
				this.get('advertisers.advertisers').clear().addObjects(groups);
				this.set('current', this.advertisers.page || 0)
			})
		},		
		upgrade(e) {
			if(confirm('It will take a little time. Are you sure to do this?')){
				Ember.$(e.target).button('loading');
				let idArr = [];
				Ember.$('input.checkbox').each( (index,item) => {
					Ember.$(item).prop('checked') && idArr.push(Ember.$(item).data('id'))
				})
				let data = {
					all: 1,
					advertiser: idArr
				}
				Ember.$.ajax({
					url: Ember.devUrl + '/api/v1/update/info',
					data: data,
					type: 'POST'
				}).then( data => {
					if(data.total >= 0){
						alert('Upgrade success!')
						Ember.$(e.target).button('reset');
					}else{
						alert('Upgrade failed.')
					}
				}, error => {
					alert(`error: ${error}`)
				})
			}
		},
		reset() {
			Ember.$('form.form-inline')[0].reset();
			Ember.$('.jplist-preloader').removeClass('jplist-hide-preloader')
			Ember.emberAjax(this.get('url'), groups => {
				this.get('advertisers.advertisers').clear().addObjects(groups)
				this.set('current', this.advertisers.page)
			})
		},
		create() {
			Ember.$('.bs-deselect-all').trigger('click');
			Ember.$('#advertiser_form')[0].reset();
			Ember.$('#advertiser_form .active').removeClass('active')
			Ember.$('#payout_percent').val(95).next().addClass('active')
			this.set('title', 'Create Advertiser');
			this.set('query', 'new');
			this.set('isCreate', true);
		},
		edit(event) {
			Ember.$('.bs-deselect-all').trigger('click');
			Ember.$('#advertiser_form')[0].reset();
			let id = Ember.$(event.target).data('id');
			let url = this.get('url') + '/' + id;
			this.set('query', id + '/update')
			this.set('title', 'Edit Advertiser');
			this.set('isCreate', false);
			this.advertisers.advertisers.forEach( (advertiser) => {
				if(advertiser.id === id){
					if(advertiser.optionIndex){
						advertiser.optionIndex.forEach( (option) => {
							Ember.$('.dropdown-menu.inner li a').eq(option).trigger('click')
						})
					}else if(advertiser.affiliate){
						let arr = advertiser.affiliate.split(',');
						Ember.$('.selectpicker option').each( (index, item) => {
							if(arr.indexOf(item.innerHTML) > -1){
								Ember.$('.dropdown-menu.inner li a').eq(index).trigger('click')
							}
						})
					}
				}
			})
			Ember.$.ajax({
				url: url,
				type: 'GET',
				dataType: 'json'
			}).then( data => {
				let item = data.data;
				Ember.$('#name').val(item.name).next().addClass('active')
				Ember.$('#fuseclick_id').val(item.fuseclick_id).next().addClass('active')
				Ember.$('#payout_percent').val(item.payout_percent).next().addClass('active')
				if(item.request_url){
					Ember.$('#request_url').val(item.request_url).next().addClass('active')
				}else{
					Ember.$('#request_url').val('').next().removeClass('active')
				}
				Ember.$('#status').val(item.status)
				Ember.$('#style').val(item.style.toLowerCase())
			})
		},		
		checkAll() {
			this.toggleProperty('isCheckAll')
			this.set('hasChecked', !this.get('isCheckAll'))
			Ember.$('td input[type=checkbox]').prop('checked', this.get('isCheckAll'))
		},
		singleCheck() {
			const _this = this;
			let sum = 0;
			Ember.$('td input[type=checkbox]').each(function(index, item) {
				if(!Ember.$(item).prop('checked')){
					_this.set('isCheckAll', false)
					return
				} else {
					sum++;
				}
			})
			if(sum === Ember.$('td input[type=checkbox]').length){
				this.set('isCheckAll', true)
			}
		},	
		isCheckAllChanged: Ember.observer('isCheckAll', function() {
			if(this.get('isCheckAll')){
				Ember.$('td input[type=checkbox]').prop('checked', true)
			}
		})
	}
});
<file_sep>import Ember from 'ember';

export default Ember.Component.extend({
  didInsertElement() {
    let date = [],
    revenue = [],
    profit = []

    this.model.forEach( item => {
        date.unshift(item.day);
        revenue.unshift(item.revenue);
        profit.unshift(item.profit);
    })
    var myChart = echarts.init(Ember.$('#main')[0]);

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: 'last 7 days line chart'
            },
            grid: {
                show: true
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data:['Revenue', 'Profit']
            },
            xAxis: {
                data: date
            },
            yAxis: {
            },
            series: [{
                name: 'Revenue',
                type: 'line',
                data: revenue
            },{
                name: 'Profit',
                type: 'line',
                data: profit
            }]
        };

        // // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    }
});
