import { Component } from '@angular/core'; import { NavController, NavParams, ActionSheetController, ToastController } from 'ionic-angular'; import { Shake, BarcodeScanner } from 'ionic-native'; // import { Product } from '../../models/product' import { DataProvider } from '../../providers/data-provider' import { CameraProvider } from '../../providers/camera-provider'; import { PreferencesProvider } from '../../providers/preferences-provider'; @Component({ selector: 'page-product-details', providers: [CameraProvider], templateUrl: 'product-details.html' }) export class ProductDetailsPage { product = { company_id: 0, default_code: null, description: null, ean13: null, image_medium: null, image_small: null, name: null, list_price: 1, purchase_ok: true, qty_available: 0, rental: false, sale_ok: true, standard_price: 0, type: "product", website_published: false, remote_id: 0, doc_state: "created", attribute_line_ids: [] } attributes = [ { id: 1, name: "Talle", }, { id: 2, name: "Color", }, { id: 3, name: "Género", }, { id: 4, name: "Marca", } ]; values = [ { id: 1, name: "36", attr_id: 1 }, { id: 2, name: "40", attr_id: 1 }, { id: 3, name: "Azul Prelavado", attr_id: 2 }, { id: 4, name: "Femenino", attr_id: 3 }, { id: 5, name: "RI19", attr_id: 4 } ]; view: string = "information"; watcher: any; constructor( public navCtrl: NavController, public params: NavParams, public data: DataProvider, public toastCtrl: ToastController, public actionSheetCtrl: ActionSheetController, public cameraProvider: CameraProvider, public preferencesPreferences: PreferencesProvider ) { } /** * */ ionViewDidLoad() { this.initialize(); this.watcher = Shake.startWatch(40).subscribe(() => { BarcodeScanner.scan().then(bar => { this.product.ean13 = bar.cancelled ? null : bar.text; }).catch(e => { this.toastCtrl.create({ message: 'No se ha podido leer el código', duration: 3000 }).present(); }); }); } /** * */ ionViewDidLeave() { if (this.watcher) { this.watcher.unsubscribe(); } } /** * */ private initialize() { if (!(this.params.data instanceof Array)) { this.product = this.params.data; this.product.doc_state = "updated"; } } /** * */ private isValidForm() { if (!this.product.name) { return false; } return true; } /** * */ showPhotoOptions() { this.actionSheetCtrl.create({ title: "Tomar una foto", buttons: [ { text: "Desde la cámara", icon: "camera", handler: () => { this.takePicture("camera"); } }, { text: "Desde la galería", icon: "images", handler: () => { this.takePicture("album"); } }, { text: "Cancelar", role: "cancel" } ] }).present(); } /** * */ takePicture(source: string): void { this.cameraProvider.getPicture(source).then(i => { this.product.image_medium = i; this.product.image_small = i; }).catch(e => { console.log(e); }); } /** * */ addAttribute(): void { if (!this.product.attribute_line_ids) { this.product.attribute_line_ids = []; } this.product.attribute_line_ids.push({ attribute: 1, value: 1 }); } /** * */ save(): void { if (!this.isValidForm()) { return; } this.product.company_id = this.preferencesPreferences.getCompany(); this.data.save('product', this.product).then(result => { if (this.params.data instanceof Array) { this.params.data.push(result.products[0]); } this.navCtrl.pop(this); }).catch(e => { console.log(e); }); } }