<!-- 会员机构管理-机构管理-商品限制设置-编辑 -->
<template>
    <app-drawer title="编辑" width="900" v-model:show="show" :loading="loading" :refresh="refresh">
        <el-form ref="formRef" label-width="140px" :model="formData" :rules="formRules" :show-message="false">
            <fieldset class="g-fieldset el-form--horizontal">
                <legend class="g-fieldset__legend">机构</legend>
                <el-form-item label="经济会员" prop="userid">
                    <app-select-member v-model="formData.userid" />
                </el-form-item>
                <el-form-item label="市场" prop="marketid">
                    <el-select v-model="formData.marketid">
                        <template v-for="item in getMarketOptions()" :key="item.value">
                            <el-option :label="item.label" :value="item.value" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item label="商品" prop="goodsid">
                    <app-select-goods v-model="formData.goodsid"
                        :params="{ marketId: formData.marketid, goodsStatusIds: '3,6,7' }" />
                </el-form-item>
                <el-form-item label="是否不显示" prop="isnodisplay">
                    <el-radio-group v-model="formData.isnodisplay">
                        <template v-for="item in getConfirmationList()" :key="item.value">
                            <el-radio :label="item.label" :value="item.value" />
                        </template>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="是否不能建买" prop="iscannotbuy">
                    <el-radio-group v-model="formData.iscannotbuy">
                        <template v-for="item in getConfirmationList()" :key="item.value">
                            <el-radio :label="item.label" :value="item.value" />
                        </template>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="是否不能建卖" prop="iscannotsell">
                    <el-radio-group v-model="formData.iscannotsell">
                        <template v-for="item in getConfirmationList()" :key="item.value">
                            <el-radio :label="item.label" :value="item.value" />
                        </template>
                    </el-radio-group>
                </el-form-item>
            </fieldset>
        </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 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 { getConfirmationList } from '@/constants/common'
import { useMarket } from '@/hooks/market'

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

const { getMarketOptions } = useMarket()
const { global: { t } } = i18n
const formRef = ref<FormInstance>()
const show = ref(true)
const refresh = ref(false)
const loading = shallowRef(false)

const formData = ref<Partial<Model.AddLimitReq>>({
    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, } = props.record ?? {}
    if (autoid) {
        formData.value.autoid = autoid
    }
})
</script>