<!--
 * @Author: PoJun
 * @Date: 2023-09-28 10:38:11
 * @LastEditors: PoJun
 * @LastEditTime: 2024-01-03 17:17:37
 * @Message: 角色认证界面
-->
<template>
    <view class="authentication">
        <uv-alert type="primary" description="选择角色,确认基本信息,更多信息待审核通过后联系管理员" center></uv-alert>

        <view class="authentication-cont">
            <uv-subsection mode="subsection" :list="tabs" :current="current" @change="change"></uv-subsection>

            <kirin-form v-if="current == 0" ref="form" :list="infoList"></kirin-form>
            <kirin-form v-else-if="current == 1" ref="form" :list="authenticationFormList"></kirin-form>

            <view class="uv-m-t-30">
                <uv-button type="primary" text="确定" @click="save"></uv-button>
            </view>
        </view>
    </view>
</template>

<script>
import { authenticationFormList } from "./constant";
import formBasicMixin from "@/utils/form-basic-mixin.js";
import { userJoinOrgAffair, supplierAffair } from "@/config/http.js";

export default {
    mixins: [formBasicMixin],
    data() {
        return {
            infoList: null,
            authenticationFormList,
            tabs: [{ name: "组织成员" }, { name: "供应商" }],
            current: 0,
        };
    },
    async onLoad() {
        await this.initDictList("sex,supplierType");
        this.initFormData();
    },
    methods: {
        async save() {
            const formData = this.$refs.form.getFormData();
            if (!formData) {
                return;
            }
            uni.showLoading({ mask: true });
            this.current == 0 ? await userJoinOrgAffair(formData) : await supplierAffair(formData);
            try {
                await this.$store.dispatch("UPDATELOGININFO");
                uni.$uv.route({ type: "back" });
            } finally {
                uni.hideLoading();
            }
        },
        change(index) {
            this.current = index;
        },
        initFormData() {
            const { OrgUnit } = this.vuex_userInfo.attr;
            this.infoList = [
                {
                    type: "input",
                    label: "组织名称",
                    code: "orgName",
                    sv: OrgUnit.name,
                    rv: OrgUnit.name,
                    canShow: true,
                    canEdit: false,
                },
                {
                    type: "input",
                    label: "用户名称",
                    code: "name",
                    sv: null,
                    rv: null,
                    canShow: true,
                    canEdit: true,
                    required: true,
                },
                {
                    type: "input",
                    label: "联系方式",
                    code: "phone",
                    sv: null,
                    rv: null,
                    canShow: true,
                    canEdit: true,
                    required: true,
                },
            ];
            this.infoList.forEach(item => {
                if (this.vuex_userInfo[item.code]) {
                    item.rv = item.sv = this.vuex_userInfo[item.code];
                    item.canEdit = item.required = false;
                }
            });
        },
    },
};
</script>

<style lang="scss">
.authentication {
    &-cont {
        padding: 25rpx;
    }
}
</style>
