<!-- 会员机构管理-机构管理-商品限制设置-编辑 -->
<template>
    <app-drawer :title="t('member.institution.goodscfg.edit.title')" width="900" v-model:show="show" :loading="loading" :refresh="refresh">
        <el-form ref="formRef" class="el-form--horizontal" label-width="140px" :model="formData" :rules="formRules"
            :show-message="false">
            <el-form-item :label="t('member.institution.goodscfg.edit.userid')" prop="userid">
                <app-select-member ref="useridRef" v-model="formData.userid" :params="{ usertype: '2', roles: '7' }" />
            </el-form-item>
            <el-form-item :label="t('member.institution.goodscfg.edit.marketid')" prop="marketid">
                <el-select v-model="formData.marketid" @change="goodsRef.onReset()">
                    <template v-for="item in getMarketOptions([15, 97, 99], true)" :key="item.value">
                        <el-option :label="item.label" :value="item.value" />
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item :label="t('member.institution.goodscfg.edit.goodsid')" prop="goodsid">
                <app-select-goods ref="goodsRef" v-model="formData.goodsid"
                    :params="{ marketId: formData.marketid ?? -1, goodsStatusIds: '3,6,7' }" />
            </el-form-item>
            <el-form-item :label="t('member.institution.goodscfg.edit.isnodisplay')" prop="isnodisplay">
                <el-radio-group v-model="formData.isnodisplay">
                    <template v-for="item in flagEnum.getEnumOptions()" :key="item.value">
                        <el-radio :label="item.label" :value="item.value" />
                    </template>
                </el-radio-group>
            </el-form-item>
            <el-form-item :label="t('member.institution.goodscfg.edit.iscannotbuy')" prop="iscannotbuy">
                <el-radio-group v-model="formData.iscannotbuy">
                    <template v-for="item in flagEnum.getEnumOptions()" :key="item.value">
                        <el-radio :label="item.label" :value="item.value" />
                    </template>
                </el-radio-group>
            </el-form-item>
            <el-form-item :label="t('member.institution.goodscfg.edit.iscannotsell')" prop="iscannotsell">
                <el-radio-group v-model="formData.iscannotsell">
                    <template v-for="item in flagEnum.getEnumOptions()" :key="item.value">
                        <el-radio :label="item.label" :value="item.value" />
                    </template>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="onCancel(false)">{{ t('operation.cancel') }}</el-button>
            <el-button type="primary" @click="onSubmit">{{ t('operation.save') }}</el-button>
        </template>
    </app-drawer>
</template>

<script lang="ts" setup>
import { ref, shallowRef, PropType, onMounted } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { addLimit } from '@/services/api/member'
import { useEnum } from '@/hooks/enum'
import AppDrawer from '@pc/components/base/drawer/index.vue'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'
import AppSelectGoods from '@pc/components/modules/select-goods/index.vue'
import { i18n } from '@/stores'
import { useMarket } from '@/hooks/market'

const props = defineProps({
    record: {
        type: Object as PropType<Member.MemberGoodsLimitRsp>
    }
})

const flagEnum = useEnum('flag') // 确认状态

const { getMarketOptions } = useMarket()
const { global: { t } } = i18n
const formRef = ref<FormInstance>()
const useridRef = ref() // 会员组件实例
const goodsRef = ref() // 商品组件实例

const show = ref(true)
const refresh = ref(false)
const loading = shallowRef(false)

const formData = ref<Member.AddLimitReq>({
    roletype: 7,
    iscannotbuy: 0,
    iscannotsell: 0,
    isnodisplay: 0,
})

// 表单验证规则
const formRules: FormRules = {
    userid: [{ required: true }],
    marketid: [{ required: true }],
    goodsid: [{ required: true }],
}

const onCancel = (isRefresh = false) => {
    show.value = false
    refresh.value = isRefresh
}

const onSubmit = () => {
    const rawData = { ...formData.value }
    formRef.value?.validate((valid) => {
        if (valid) {
            loading.value = true
            addLimit({
                data: rawData
            }).then(() => {
                ElMessage.success(t('common.tips3'))
                onCancel(true)
            }).catch((err) => {
                ElMessage.error(t('common.tips4') + err)
            }).finally(() => {
                loading.value = false
            })
        }
    })
}

onMounted(() => {
    const { autoid, goodsid, marketid, userid, iscannotbuy, iscannotsell, isnodisplay } = props.record ?? {}
    if (autoid) {
        formData.value = {
            autoid,
            goodsid,
            marketid,
            roletype: 7,
            userid,
            iscannotbuy,
            iscannotsell,
            isnodisplay
        }
        useridRef.value.onReset(userid)
        goodsRef.value.onReset()
    }
})
</script>