import {Component} from '@angular/core';
import {
    ActionSheetController,
    LoadingController,
    NavController,
    ToastController
} from 'ionic-angular';
import {FileTransfer, FileTransferObject, FileUploadOptions} from "@ionic-native/file-transfer";
import {CommonService} from "../../../core/common.service";
import {Camera, CameraOptions} from "@ionic-native/camera";
import {Storage} from "@ionic/storage";
import {ChangePasswordPage} from "../change-password/change-password";
import {GlobalData} from "../../../core/GlobleData";

@Component({
    selector: 'page-person',
    templateUrl: 'person.html',
})
export class PersonPage {

    user: any;
    gender: object;

    mineInfo;

    constructor(public navCtrl: NavController, public camera: Camera, public commonSer: CommonService,
                public storage: Storage, public actionSheetCtrl: ActionSheetController,
                public loadingCtrl: LoadingController,private globle:GlobalData,
                public toast: ToastController, public transfer: FileTransfer,) {
        //获取个人信息
        this.mineInfo = this.globle.userObj;
    }

    ionViewWillEnter(): void {
    }

    modifyPwd() {
        this.navCtrl.push(ChangePasswordPage)
    }

    //不同的code代表修改不同的数据，1房间，2座机，3邮箱
    edit() {
        this.navCtrl.push("EditPersonInfoPage");
    }

    //选择头像
    tackePic() {
        const actionSheet = this.actionSheetCtrl.create({
            cssClass: 'cameraAction',
            buttons: [
                {
                    text: '拍照',
                    role: 'fromCamera',
                    handler: () => {
                        this.selectPicture(1);
                    }
                }, {
                    text: '从相册中选',
                    role: 'fromPhoto',
                    handler: () => {
                        console.log('fromPhoto');
                        this.selectPicture(0);
                    }
                }, {
                    text: '取消',
                    role: 'cancel',
                    handler: () => {
                        console.log('Cancel clicked');
                    }
                }
            ]
        });
        actionSheet.present();
    }

    //选择图片
    selectPicture(srcType) {
        const options: CameraOptions = {
            quality: 50,
            destinationType: this.camera.DestinationType.FILE_URI,
            encodingType: this.camera.EncodingType.JPEG,
            mediaType: this.camera.MediaType.PICTURE,
            sourceType: srcType,
            saveToPhotoAlbum: false
        };
        const option: FileUploadOptions = {
            httpMethod: 'post',
            headers: {
                'Accept': 'application/json',
            },
            fileName: 'image.png'
        };
        this.camera.getPicture(options).then((imagedata) => {
            let filePath = imagedata;
            if (filePath.indexOf('?') !== -1) {     //获取文件名
                filePath = filePath.split('?')[0];
            }
            let arr = filePath.split('/');
            console.log(imagedata);
            option.fileName = arr[arr.length - 1];
            this.upload(imagedata, option);
        })
    }

    upload(file, options) {
        const uploadLoading = this.loadingCtrl.create({
            content: '上传中...',
            dismissOnPageChange: true,
            enableBackdropDismiss: true,
        });
        uploadLoading.present();
        const fileTransfer: FileTransferObject = this.transfer.create();
        fileTransfer.upload(file, "", options).then(
            (res) => {
                console.log(res);
                uploadLoading.dismiss();
                this.commonSer.toast('上传成功');
            }, err => {
                uploadLoading.dismiss();
                this.commonSer.toast('上传错误');
            });
        fileTransfer.onProgress((listener) => {
            let per = <any>(listener.loaded / listener.total) * 100;
            per = Math.round(per * Math.pow(10, 2)) / Math.pow(10, 2)
            uploadLoading.setContent('上传中...' + per + '%');
        })
    }

    goBack() {
        this.navCtrl.popToRoot();
    }
}
