<template>
    <div class="card" style="margin-bottom: 20px">
        <van-row style="border-bottom: solid 1px #e4e4e4;">
            <van-col span="14">
                <h3><strong>校卡审核</strong></h3>
            </van-col>
            <van-col span="6">
               <p :style="{ color: statusColor }">{{statusString}}</p>
            </van-col>
        </van-row>
        <div v-if="status===30 || status === 500">
            <add-photo @onRead="onRead"/>
            <p>{{note}}</p>
            <van-button
                    type="primary"
                    @click="schoolPresent"
                    style="width: 100%"
                    :loading="loading"
            >保存
            </van-button>
        </div>
        <div v-else>
            <img :src="schoolpic" style="max-width: 100%"/>
        </div>


    </div>
</template>

<script>
    import {Row, Col, Toast, Button} from 'vant';
    import AddPhoto from "../components/addPhoto";
    import {BasicFunction} from "../connector/basic-service";
    export default {
        name: "schoolCard",
        components: {
            AddPhoto,
            [Row.name]: Row, [Col.name]: Col,
            [Button.name]: Button
        },
        data() {
            return {
                statusColor:'',
                statusString:'-----',
                photo: null,
                loading:false,
                status:0,
                imgage: null,
                imgDo:{},
                schoolpic:"",
                IMGPREFIX: "http://gift.fogice.com",
                note: ""
            }
        },
        methods: {
            schoolPresent() {
                // 检验输入
                this.loading=true;
                if (this.photo === null) {
                    Toast.fail('请上传礼物照片');
                    this.loading=false;
                    return;
                }
                let that=this;
                console.log('------- photo -------');
                console.log(this.photo);
                let formData = new FormData();
                formData.append('file', this.photo);
                BasicFunction.get_data("http://gift.fogice.com/server/fileserver/upload?sign=abcdefg", function (res) {
                    if (res.model === undefined || res.model.length === 0 || res === undefined ) {
                        Toast.fail('上传失败，请重试');
                        return;
                    }
                    if (res.ret === '10000') {
                        let photoURL = res.model;
                        let userid = localStorage.getItem('frontend-userid');
                        BasicFunction.get_data('smartIdentity/list?userId=' + userid, function (res) {
                            // console.log('----- User Identity -----');
                            // console.log(res);
                            if (res.ret === '10000') {
                                let t = res.model.list[0];
                                t.userSchoolCardPic = photoURL;
                                t.userReviewStatus = 100;
                                t.reviewerNote = "未审核";
                                t.reviewerNoteToUser = "未审核";
                                BasicFunction.get_data('smartIdentity/updateBatch', function (res) {
                                    if (res.ret === '10000') {
                                        Toast.success('上传成功');
                                        location.reload();
                                    }
                                    else Toast.fail('上传失败，请重试');
                                }, [t]);
                            } else Toast.fail('访问失败，请重试');
                        }, {});
                    } else {
                        Toast.fail('上传失败');
                    }
                }, formData);
            },
            onRead: function (e) {
                this.photo = e;
            }
        },
        created: function () {
            if (!localStorage.getItem('frontend-userid')) {
                this.$router.push('login');
                return;
            }
            let userid = localStorage.getItem('frontend-userid');
            let that = this;
            BasicFunction.get_data('smartIdentity/list?userId=' + userid, function (res) {
                // console.log('----- User Identity -----');
                // console.log(res);
                if(res === undefined || res.model === undefined )
                {
                    Toast('读取失败，请检查网络');
                    that.$router.push('verify');
                }
                if (res.ret === '10000') {
                    var t=res.model.list;
                    that.status=t[0].userReviewStatus;
                    that.imgDo=t[0];
                    that.note = t[0].reviewerNoteToUser;
                    // console.log(that);
                    if (res.model.list.length === 0 || that.status === 30) {
                        that.statusColor='#c5c5c5';
                        that.statusString='未上传校卡';
                        that.$router.push('verify');
                        return;
                    } else if (that.status === 100 || that.status === 1000) {
                        that.schoolpic=that.IMGPREFIX+t[0].userSchoolCardPic;
                        if (that.status === 100)
                        {
                            that.statusColor='#c5c530';
                            that.statusString='审核中'
                        }

                        if(that.status === 1000) {
                            that.statusColor = '#00ff00';
                            that.statusString='审核通过'
                        }
                    } else {
                        that.statusColor='#ff0000';
                        that.statusString='审核未通过'
                    }
                }
            }, {});
        }
    }
</script>

<style scoped>
    p, h3 {
        color: #fd6740;
    }

    p, h3 {
        padding: 5px;
    }
    p {
        padding-top:  3px ;
    }
    .card {
        background-color: white;
        margin: 20px 20px;
        border: solid 1px #e4e4e4;
        padding: 15px;
        text-align: left;
    }
    .van-button {
         background-color: #fd6740;
         border-color: #fd6740;
         margin: 10px 0 10px;
         width: 55%;
     }
</style>