<template>
    <view>
        <my-form ref="form" :form="form" @isUncomplete="isUncomplete" @onUploading="onUploading" />
        <view class="footer">
            <view class="but" :class="(uncomplete || onUpload) ? 'forbidden' : ''" hover-class="navigator-hover"
                @click="(uncomplete || onUpload) ? '' : submit()">
                立即提交
            </view>
        </view>
        <view style="height: 75px;" />
    </view>
</template>

<script>
import myForm from "../../components/my_form.vue"
export default {
    components: {
        myForm
    },
    data() {
        return {
            form: [],
            uncomplete: true,
            pageType: 4,
            onUpload: false,
            isSubmit: false,
        }
    },
    onUnload() {
        if(!this.isSubmit) this.$refs.form.deleteFiles()
    },
    onLoad(options) {
        this.pageType = options.pageType == '设计' ? 4 : 5
        uni.setNavigationBarTitle({
            title: '上传'
        });
        this.initForm()
    },
    methods: {
        async initForm() {
            this.form = [{
                key: "title_c",
                type: "text",
                label: "标题",
                isMust: true,//是否必填
                value: "",
                marginTop: 10
            }, {
                key: "panoramaurl",
                type: "text",
                label: "全景地址",
                placeholder: "请填写酷乐家/三维家720°全景地址",
                value: ""
            }, {
                key: "space",
                type: "customClass",
                label: "空间",
                isMultipleChoice: true,//是否多选
                value: [],
                list: await this.getCustomClass('picturespace'),
                marginTop: 10
            }, {
                key: "style",
                type: "customClass",
                label: "风格",
                isMultipleChoice: true,//是否多选
                value: [],
                list: await this.getCustomClass('picturestyle'),
                marginTop: 10
            }, {
                key: "spec",
                type: "customClass",
                label: "尺寸",
                isMultipleChoice: true,//是否多选
                value: [],
                list: await this.getCustomClass('picturespec'),
                marginTop: 10
            }, {
                key: "attachmentids",
                type: "upload",
                label: "图片/视频",
                placeholder: "可上传多个视频或图片",
                accept: "all",
                ownertable: "temporary",
                ownerid: 999,
                usetype: 'default',
                allowUpload: true,
                allowDelete: true,
                value: [],
                marginTop: 10
            }]
        },
        isUncomplete(uncomplete) {
            this.uncomplete = uncomplete;
        },
        submit() {
            this.$refs.form.submit().then(data => {
                this.onUpload = true;
                this.$Http.basic({
                    "id": 20240417104602,
                    "content": {
                        "sat_sharematerialid": 0,
                        "title": "",
                        "title_c": data.title_c,
                        "type": this.pageType, //4：设计，5：实景
                        "panoramaurl": data.panoramaurl,
                        "renderingsclass": {
                            "style": data.style,
                            "space": data.space,
                            "spec": data.spec
                        },
                        "isnationwide": 0, //是否全国精选
                        "sequence": 0,
                        "ispublish": 1//是否发布
                    },
                }).then(async res => {
                    this.onUpload = false;
                    console.log("新建", res)
                    if (this.cutoff(res.msg)) return;
                    if (data.files.temporarys.length) {
                        this.onUpload = true;
                        await this.$Http.basic({
                            "classname": "system.attachment.Attachment",
                            "method": "createFileLink",
                            "content": {
                                ownertable: 'sat_sharematerial',
                                ownerid: res.data.sat_sharematerialid,
                                usetype: 'default',
                                attachmentids: data.files.temporarys
                            }
                        }).then(async s => {
                            this.isSubmit = true;
                            this.onUpload = false;
                            console.log("绑定附加", s)
                            if (this.cutoff(s.msg)) return;
                            await this.$Http.basic({
                                "id": "20240425102102",
                                "content": {
                                    "ownertable": 'sat_sharematerial',
                                    "ownerid": res.data.sat_sharematerialid,
                                    "items": data.files.temporarys.map(v => {
                                        return {
                                            "attachmentid": v,
                                            "jumpurl": "",
                                            "sequence": 0
                                        }
                                    })
                                }
                            }).then(e => {
                                console.log("plc", e)
                                if (this.cutoff(e.msg)) return;
                            })
                        })
                    };
                    if (this.$Http.updateList) return this.$Http.updateList(res.data);
                    uni.showModal({
                        title: '提示',
                        content: '新建成功,是否立即查看',
                        cancelText: '返回列表',
                        confirmText: '查看详情',
                        success: ({ confirm }) => {
                            if (confirm) {
                                uni.redirectTo({
                                    url: `/packageA/fullView/detail?id=${res.data.sat_sharematerialid}`
                                });
                                this.$Http.setCount = null;
                            } else {
                                uni.navigateBack()
                            }
                        },
                    })
                })
                console.log("页面", data)
            })
        },
        onUploading(ing) {
            this.onUpload = ing;
        },

    },
}
</script>

<style lang="scss" scoped>
.footer {
    position: fixed;
    bottom: 0;
    width: 375px;
    height: 65px;
    background: #FFFFFF;
    box-shadow: 0px -2px 6px 1px rgba(0, 0, 0, 0.16);
    border-radius: 0px 0px 0px 0px;

    .but {
        width: 355px;
        height: 45px;
        background: #C30D23;
        border-radius: 5px;
        margin: 5px auto;
        text-align: center;
        line-height: 45px;
        font-size: 14px;
        color: #FFFFFF;
    }

    .forbidden {
        opacity: .6;
    }
}
</style>