<template>
    <div class="container">
        <div class="top" v-if="orderInfo.productInfo">
            <span>照片预览</span>
            <span style="margin-left:10px;">订单号:{{orderInfo.orderCode}}</span>
            <span style="margin-left:20px;">{{orderInfo.productInfo.title}}</span>
        </div>

        <div class="content">

            <div class="imgList">
                <div class="title">原<br/>片</div>
                <img-item v-for="(item,index) in orderImage" @changeRemark="getImage" :label='"originalImage"' :index='index' :img='item' :imgStyle='"preView"' @delImg='getImage' style="display:inline-block;margin:0 30px 0 0;vertical-align: middle;"></img-item>
            </div>

            <div class="imgList" style="margin-top:50px;" v-if="orderInfo.statusFlag>2">
                <div class="title" style="color:#FB6E08">成<br/>片</div>
                <img-item v-for="(item,index) in orderImage" @changeRemark="getImage" :label='"finishedImage"' :index='index' :img='item' :imgStyle='"preView"' @delImg='getImage' style="display:inline-block;margin:0 30px 0 0;vertical-align: middle;"></img-item>
            </div>

        </div>
        <div v-if='totalNumber>7'>
            <el-pagination background @current-change="currentPageChange" :current-page="currentPage" :page-size="7" layout="total, prev, pager, next" :total="totalNumber">
            </el-pagination>
        </div>

        <div class="bottom">
            <span v-if="canUploadNum>0">当前已上传{{orderImage.length}}张图片，还需上传{{canUploadNum}}张</span>
            <span v-else>全部{{orderImage.length}}张图片</span>
            <!-- <el-button style="float:right;margin:10px;" v-if="isSuccess" type="primary" class="btn" @click="submit">审核通过</el-button> -->
            <!-- <el-button style="float:right;margin:10px;" v-if="isFail" type="warning" class="btn" @click="submit">确认提交第{{orderInfo.orderRepairList.length+1}}次返修</el-button> -->
        </div>

    </div>
</template>
<script>
import { mapState } from 'vuex'
import imgItem from '../components/ImgItem'
import formValidator from '../formValidator'
const baseUrl = process.env.NODE_ENV === 'production' ? '../' : 'http://localhost:8080';
export default {
    created() {
        this.$http.get({
            url: '/userOrder/getOne',
            data: {
                id: this.$route.query.orderId
            }
        }).then(res => {
            if (res.success) {
                this.orderInfo = res.data

            }
        })

        this.getImage()

    },
    computed: {
        ...mapState(['userInfo']),
        url() {
            return baseUrl + '/assets/uploadFile'
        },
        allImage() {
            var list = []
            this.addImage.forEach(item => {
                list.push(item.originalImage)
            })
            return list
        },
        canUploadNum() {
            var num = 0
            if (this.orderInfo.quantity) {
                num = this.orderInfo.quantity - this.orderImage.length
            }
            return num
        }
    },
    data() {
        return {
            orderInfo: {},
            dialogVisible: false,
            addImage: [],
            orderImage: [],
            currentPage: 1,
            totalNumber: 0,
            isSuccess: false,
            isFail: false
        }
    },
    methods: {
        currentPageChange(page) {
            this.currentPage = page;
            this.getImage();
        },
        getImage() {
            this.orderImage = []
            this.$http.get({
                url: '/orderImage/page',
                data: {
                    orderId: this.$route.query.orderId,
                    currentPage: this.currentPage,
                    pageNumber: 7,
                }
            }).then(res => {
                if (res.success) {
                    this.orderImage = res.data.pp
                    this.totalNumber = res.data.page.totalNumber;
                }
            })
            this.isSuccess = false
            this.isFail = false
            this.$http.get({
                url: '/orderImage/all',
                data: {
                    orderId: this.$route.query.orderId,
                    statusFlag: 0
                }
            }).then(res => {
                if (res.success) {
                    if (res.data.length == 0) {
                        this.$http.get({
                            url: '/orderImage/all',
                            data: {
                                orderId: this.$route.query.orderId,
                                statusFlag: 2
                            }
                        }).then(res => {
                            if (res.data.length == 0) {
                                this.isSuccess = true
                            }
                            else {
                                this.isFail = true
                            }
                        })
                    }
                }
            })
        },
        chooseImg(index) {
            console.log(index)
        },
        changeRemark(json) {
            // if(this.changeRemark.j)
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
            var list = []
            fileList.forEach(item => {
                item.response.data.forEach(todo => {
                    var jsop = {
                        originalImage: todo,
                        remark: ''
                    }
                    list.push(jsop)
                })
            })
            this.addImage = list
        },
        handleSuccess(res) {
            res.data.forEach(item => {
                if (this.allImage.indexOf(item) == -1 && this.addImage.length <= this.canUploadNum) {
                    var jsop = {
                        originalImage: item,
                        remark: ""
                    }
                    this.addImage.push(jsop)
                }
                else if (this.addImage.length > this.canUploadNum) {
                    this.$message({
                        message: '已经上传' + this.addImage.length + '张图片了，请预览后确认图片',
                        type: 'warning'
                    });
                }
            })
        },
        delImg(index) {
            this.addImage.splice(index, 1)
        },
        upload() {
            this.saveImage(0)
        },
        saveImage(i) {
            if (i == this.allImage.length) {
                this.$message.success('成功');
                this.dialogVisible = false
                this.getImage()
            }
            else {
                var data = {
                    originalImage: this.allImage[i],
                    orderId: this.orderInfo.id,
                    userId: this.userInfo.id,
                    storeId: this.orderInfo.storeId

                }
                this.$http.post({
                    url: '/orderImage/save',
                    data: data
                }).then(res => {
                    if (res.success) {
                        this.saveImage(i + 1)
                    } else {
                        this.$message.warning('失败')
                    }
                });
            }
        },
        submit() {
            this.$http.post({
                url: '/userOrder/update',
                data: {
                    id: this.orderInfo.id,
                    statusFlag: 5
                }
            }).then(res => {
                if (res.success) {
                    this.$message.success('提交成功');
                    history.back()

                }
            })
        }
    },
    components: {
        imgItem
    }
}
</script>
<style lang="less" scoped>
.container {
    background: rgba(255, 255, 255, 1);
    border-bottom: 1px solid rgba(234, 234, 234, 1);
    .top {
        height: 78px;
        background: rgba(255, 255, 255, 1);
        border: 1px solid rgba(234, 234, 234, 1);
        padding: 0 30px;
        span {
            font-size: 16px;
            font-family: MicrosoftYaHei;
            color: rgba(51, 51, 51, 1);
            line-height: 78px;
        }
        .btn {
            float: right;
            margin-top: 20px;
        }
    }

    .content {
        padding: 0 30px;
        min-height: 450px;
        .title {
            font-size: 14px;
            font-family: MicrosoftYaHei-Bold;
            font-weight: bold;
            color: rgba(11, 158, 192, 1);
            line-height: 19px;
            margin-bottom: 20px;
            display: inline-block;
            vertical-align: middle;
            margin-right: 20px;
        }

        .imgList {
            margin-top: 20px;
        }
    }

    .bottom {
        height: 58px;
        border-top: 1px solid rgba(234, 234, 234, 1);
        line-height: 58px;
        padding: 0 30px;
        margin-top: 20px;
        span {
            font-size: 14px;
            color: #999;
        }
    }
}
</style>
