var app = new Vue({
    el: '#app',
    data: {
        bro_year: '',
        bro_month: '',
        bro_day: '',
        broth_info: '请选择日期',
        default_bro: '日期',
        date_type: '',
        type_name: '',
        type_home: '',
        type_id: '',
        choose_type_name: '名称',
        choose_homes: '机构名称',
        popupVisible: false,
        img_report: '',
        img_sao_yi_sao: '',
        Illness_list: [],
        org_list: [],
        content: '',
        rid: '',
        code: ''
    },
    created: function () {
        // apiready = function () {
            // app.broth_info=$("#date2").val();
            console.log(this.broth_info)
           this.broth_info=localStorage.getItem("birth")
           //  this.rid = api.pageParam.rid;
           //  this.date_type = api.pageParam.date_type;
           //  this.type_name = api.pageParam.type_name;
           //  this.type_home = api.pageParam.type_home;
           // this.type_id = api.pageParam.type_id;
            this.rid=localStorage.getItem("rid");
            this.date_type=localStorage.getItem("date_type");
            this.type_name=localStorage.getItem("type_name");
            this.type_home=localStorage.getItem("type_home");
            this.type_id=localStorage.getItem("type_id");
           this.getOrg();
           this.getIllness();
            switch (this.type_id) {
                case 1:
                    app.getIllness();
                    break;
                case 2:
                    app.getPExam();
                    break;
                case 3:
                    app.getCheckReport();
                    break;
                default:
                    return false;
            }

        // }
    },
    methods: {
        // 选择日期
        // broSelect: function () {
        //         //     api.openPicker({
        //         //         type: 'date',
        //         //         title: '选择日期'
        //         //     }, function (ret, err) {
        //         //         if (ret) {
        //         //             app.bro_year = ret.year;
        //         //             app.bro_month = ret.month;
        //         //             app.bro_day = ret.day;
        //         //             app.broth_info = app.bro_year + '-' + add0(app.bro_month) + '-' + add0(app.bro_day);
        //         //             if (checkDate(app.broth_info, 2)) {
        //         //                 return app.broth_info
        //         //             } else {
        //         //                 toastMsg('日期不能比今天晚');
        //         //                 return app.broth_info = app.default_bro
        //         //             }
        //         //         } else {
        //         //             console.log(JSON.stringify(err));
        //         //         }
        //         //     });
        //         // },
        // 上传照片
        choose_pic: function (type) {
            var app=this;
            app.popupVisible = false;
            api.getPicture({
                sourceType: type,
                encodingType: 'jpg',
                mediaValue: 'pic',
                destinationType: 'url',
                allowEdit: true,
                quality: 50,
                targetWidth: 300,
                targetHeight: 400,
                saveToPhotoAlbum: true
            }, function (ret, err) {
                // alert(JSON.stringify(ret));
                if (ret && ret.data && ret.data !== "") {
                    api.ajax({
                        url: baseurl + 'user/index/addReportPic',
                        method: 'post',
                        data: {
                            values: {
                                r_type: app.type_id
                            },
                            files: {
                                file: ret.data
                            }
                        },
                        headers: {
                            "XX-Device-Type": getDevice(),
                            'XX-Token': getToken()
                        }
                    }, function (res, erro) {
                        if (res.code == 1) {
                            app.img_report = res.data.url;
                            // alert(JSON.stringify(app.img_report));
                        } else {
                            toastMsg(res.msg);
                        }
                    });
                }
            });
        },
        // 获取疾病类型
        getIllness: function () {
           
            var app=this;
            var heder = {
                "XX-Device-Type": getDevice(),
                'XX-Token': getToken()
            };
            getRequest('post', 'user/index/getIllness', null, heder).then(function (res) {
               console.log(res)
                if (res.data.code == 1) {
                    for (var i in res.data.data) {
                        app.Illness_list.push({
                            value: res.data.data[i].id,
                            label: res.data.data[i].name
                        })
                    }
                    loadEnd()
                }
            })
        },
        // 获取体检名称
        getPExam: function () {
            var app=this;
            var heder = {
                "XX-Device-Type": getDevice(),
                'XX-Token': getToken()
            };
            getRequest('post', 'user/index/getPExam', null, heder).then(function (res) {
                if (res.data.code == 1) {
                    for (var i in res.data.data) {
                        app.Illness_list.push({
                            value: res.data.data[i].id,
                            label: res.data.data[i].name
                        })
                    }
                    loadEnd()
                }
            })
        },
        // 获取
        getCheckReport: function () {
            var app=this;
            var header = {
                "XX-Device-Type": getDevice(),
                'XX-Token': getToken()
            };
            getRequest('post', 'user/index/getCheckReport', null, header).then(function (res) {
                if (res.data.code == 1) {
                    for (var i in res.data.data) {
                        app.Illness_list.push({
                            value: res.data.data[i].id,
                            label: res.data.data[i].name
                        })
                    }
                    loadEnd()
                }
            })
        },
        // 获取机构名称
        getOrg: function () {

            var app=this;
            var heder = {
                "XX-Device-Type": getDevice(),
                'XX-Token': getToken()
            };
            getRequest('post', 'user/index/getOrg', null, heder).then(function (res) {
                 // console.log(res);
                if (res.data.code == 1) {
                    for (var i in res.data.data) {
                        app. org_list.push({
                            value: res.data.data[i].id,
                            label: res.data.data[i].name
                        })
                    }
                    loadEnd()
                }
            })
        },
        // 选择类别

        choose_item: function () {
            var app=this;
            weui.picker(app.Illness_list, {
                onChange: function (result) {
                },
                onConfirm: function (result) {

                    app.choose_type_name = result[0].label
                }
            })
        },
        // 选择机构名称
        choose_home: function () {
            var app=this;
            weui.picker(app.org_list, {
                onChange: function (result) {
                },
                onConfirm: function (result) {
                    app.choose_homes = result[0].label
                }
            })
        },
        // 打开二维码
        openQrc: function () {
            var app=this;
            var FNScanner = api.require('FNScanner');
            FNScanner.open({
                autorotation: true
            }, function (ret, err) {
                if (ret) {
                    FNScanner.openScanner({
                        autorotation: true
                    }, function (ret, err) {
                        if (ret) {
                            // alert(JSON.stringify(ret))
                            app.code = ret.content;
                            // alert(JSON.stringify(app.img_report));
                            // toastMsg('扫码成功')
                        } else {
                            // alert(JSON.stringify(ret))
                        }
                    });
                } else {
                }
            });
        },
        // 保存报告
        save_info: function () {
            var app=this;
            app.broth_info=$("#date2").val();
            localStorage.setItem("birth",app.broth_info);
            console.log(localStorage)
            console.log( app.broth_info)
            var header = {
                "XX-Device-Type": getDevice(),
                'XX-Token': getToken()
            };
            var post = {
                rid: app.rid,
                checktime: Date.parse(app.broth_info),
                name: app.choose_type_name,
                org: app.choose_homes,
                descript: app.content,
                pic: app.img_report,
                r_type: app.type_id,
                code: app.code
            };
            if (app.broth_info == '') {
                toastMsg('请选择日期')
            } else if (app.choose_type_name == '名称') {
                toastMsg('请选择类别')
            } else if (app.choose_homes == '机构名称') {
                toastMsg('请选择机构名称')
            } else if (app.content == '') {
                toastMsg('请输入描述')
            } else if (app.img_report == '' && app.code == '') {
                toastMsg('请上传报告照片')
            }
            else {
                getRequest('post', 'user/index/addReport', post, header).then(function (res) {
                    // alert(JSON.stringify(res))
                    if (res.data.code == 1) {
                        toastMsg('添加成功');
                        api.sendEvent({
                            name:'add_success'
                        });
                        setTimeout(function() {
                            api.closeWin()
                        }, 1000)
                    } else {
                        toastMsg(res.msg)
                    }
                })
            }
        }
    }
})