<template>
    <view>
        <my_form ref="form" :form="form" @onUploading="onUploading" @isUncomplete="isUncomplete" requiredFieldOnly
            @interrupt="interrupt" />
        <view style="height: 70px;" />
        <view class="footer">
            <view class="add" @click="uncomplete || loading ? '' : submit()" :class="uncomplete ? 'forbidden' : ''"
                hover-class=" navigator-hover">
                <u-loading-icon v-if="loading" />
                <block v-else>
                    提交
                </block>
            </view>
        </view>
    </view>
</template>

<script>
import { formattedFiles } from "../../utils/settleFiles"
export default {
    data() {
        return {
            sa_storeid: 0,
            form: [],
            uncomplete: true,
            loading: false,
        }
    },
    async onLoad(options) {
        console.log(options)
        uni.setNavigationBarTitle({
            title: options.title || '新增门店'
        });
        let form = [{
            key: "storename",
            type: "text",
            label: "门店名称",
            isMust: true,//是否必填
            value: "",
        }, {
            key: "area",
            type: "text",
            label: "面积(m²)",
            isMust: true,//是否必填
            value: "",
            inputmode: 'digit',
        }, {
            key: "leader_hrid",
            type: "route",
            path: "/team/userCenter/selectUser?title=选择负责人",
            isRadio: true,
            label: "负责人姓名",
            isMust: true,//是否必填
            value: [],
            showValue: [],
            interrupt: true
        }, {
            key: "phonenumber",
            type: "text",
            label: "负责人电话",
            isMust: true,//是否必填
            value: "",
            inputmode: 'number',
            disabled: true,
            verify: [this.getReg("phonenumber")]
        }, {
            key: "sale_phonenmber",
            type: "text",
            label: "门店售前电话",
            isMust: false,//是否必填
            value: "",
            inputmode: 'number',
            verify: [this.getReg("phonenumber")]
        }, {
            key: "service_phonenmber",
            type: "text",
            label: "门店售后电话",
            isMust: false,//是否必填
            value: "",
            inputmode: 'number',
            verify: [this.getReg("phonenumber")]
        }, {
            key: "storetype",
            type: "radio",
            label: "门店类型",
            value: "直营",
            options: [{
                label: "直营",
                name: "直营",
                marginRight: 60
            }, {
                label: "分销",
                name: "分销",
            }]
        }, {
            key: "markettype",
            type: "customClass",
            label: "门店卖场类型",
            isMust: true,//是否必填
            isMultipleChoice: false,//是否多选
            value: '',// 多选[] 单选 ""
            list: await this.getCustomClass('storetype')
        }, {
            key: "selectMap",
            type: "route",
            path: "/select/place/index",
            label: "门店地址",
            keys: ["latitude", "longitude", "province", "city", "county", "address"],
            isRadio: true,
            showValue: [],
            isMust: true,//是否必填
            value: {},
        }, {
            key: "attachmentids",
            type: "upload",
            label: "门店照片",
            accept: "image",
            placeholder: "仅单张",
            ownertable: "temporary",
            ownerid: 999,
            usetype: 'default',
            allowUpload: true,
            allowDelete: true,
            maxCount: 1,
            value: []
        }];

        if (options.data) {
            let data = JSON.parse(options.data);
            this.sa_storeid = data.sa_storeid;
            form = form.map(v => {
                switch (v.key) {
                    case 'leader_hrid':
                        v.value = [data.leader_hrid]
                        v.showValue = [data.name]
                        break;
                    case 'selectMap':
                        v.value = {}
                        v.keys.forEach(key => {
                            v.value[key] = data[key]
                        })
                        v.showValue = [this.getCity(data)]
                        break;
                    case 'attachmentids':
                        v.value = formattedFiles(data.attinfos);
                        break;
                    default:
                        v.value = data[v.key] || ''
                        break;
                }
                return v
            })
        }

        this.form = form;
    },
    methods: {
        isUncomplete(uncomplete) {
            this.uncomplete = uncomplete;
        },
        onUploading(e) {
            if (e) {
                this.copyUncomplete = this.uncomplete;
                this.uncomplete = true;
            } else {
                this.uncomplete = this.copyUncomplete;
            }
        },
        interrupt(item, selected, index) {
            switch (item.key) {
                case "leader_hrid":
                    console.log(selected)
                    item.showValue = [selected.name];
                    item.value = [selected.sys_enterprise_hrid];
                    this.$refs.form.setItem(index, item)
                    this.$refs.form.setValue(index + 1, selected.phonenumber, true)
                    break;
                case "selectMap":
                    item.showValue = [this.getCity(selected)];
                    item.value = selected;
                    this.$refs.form.setItem(index, item, true)
                    break;
            }
        },
        submit() {
            this.loading = true;
            let that = this;
            this.$refs.form.submit().then(data => {
                data.leader_hrid = data.leader_hrid.value;
                this.$Http.basic({
                    "id": 20240410095302,
                    "content": {
                        "sa_storeid": this.sa_storeid,
                        ...data
                    }
                }).then(async res => {
                    console.log("创建门店", res)
                    if (this.cutoff(res.msg)) return;
                    if (data.files.temporarys.length) await this.$Http.basic({
                        "classname": "system.attachment.Attachment",
                        "method": "createFileLink",
                        "content": {
                            ownertable: 'sa_store',
                            ownerid: res.data.sa_storeid,
                            usetype: 'default',
                            attachmentids: data.files.temporarys
                        }
                    })
                    if (data.files.linksids.length) await this.$Http.basic({
                        "classname": "system.attachment.Attachment",
                        "method": "deleteFileLink",
                        "content": {
                            linksids: data.files.linksids
                        }
                    })
                    this.loading = false;
                    this.$Http.uploadStoreList && this.$Http.uploadStoreList();
                    if (this.sa_storeid == 0) {
                        uni.showModal({
                            title: '创建成功',
                            content: '门店新增成功!是否立即前往',
                            cancelText: '返回',
                            confirmText: '立即前往',
                            success: ({ confirm }) => {
                                if (confirm) {
                                    uni.redirectTo({
                                        url: '/store/center/detail?id=' + res.data.sa_storeid,
                                    })
                                } else {
                                    uni.navigateBack();
                                }
                            },
                        })
                    } else {
                        this.$Http.uploadStoreDetail && this.$Http.uploadStoreDetail();
                        uni.showToast({
                            title: "编辑门店成功",
                            icon: "none",
                            duration: 1500,
                            mask: true
                        })
                        setTimeout(() => {
                            uni.navigateBack();
                        }, 800)
                    }
                })
            })
        },
    }
}
</script>

<style lang="scss" scoped>
.footer {
    position: fixed;
    bottom: 0;
    width: 100vw;
    height: 65px;
    background: #FFFFFF;
    box-shadow: 0px -2px 6px 1px rgba(0, 0, 0, 0.16);
    box-sizing: border-box;
    padding: 5px 10px;
    z-index: 999;

    .add {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 45px;
        background: #C30D23;
        border-radius: 5px;
        font-family: PingFang SC, PingFang SC;
        font-size: 14px;
        color: #FFFFFF;
    }

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