<!-- 会员机构管理-机构管理-商品限制设置 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption">
                <template #userid="{ item }">
                    <el-form-item :label="item.label" prop="userid">
                        <app-select-member v-model="item.value" :params="{ usertype: '2', roles: '7' }"
                            :placeholder="t('member.institution.goodscfg.tips2')" />
                    </el-form-item>
                </template>
                <template #goodsid="{ item }">
                    <el-form-item :label="item.label" prop="goodsid">
                        <app-select-goods ref="goodsRef" v-model="item.value"
                            :params="{ marketId: queryParams.marketid ?? -1, goodsStatusIds: '3,6,7' }" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['member_institution_goodscfg_add'])"
                    @click="openComponent" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small"
                    :data-list="getActionButtons(['member_institution_goodscfg_modify', 'member_institution_goodscfg_delete'])"
                    @click="(code: string) => openComponent(code, row)" circle />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onSearch" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ record }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { useMarket } from '@/hooks/market'
import { useEnum } from '@/hooks/enum'
import { queryMemberGoodsLimit } from '@/services/api/member'
import { i18n } from '@/stores'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import AppSelectGoods from '@pc/components/modules/select-goods/index.vue'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'

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

const { getMarketOptions } = useMarket()
const { global: { t } } = i18n
const goodsRef = ref() // 商品组件实例

const { componentMap, componentId, record, openComponent, closeComponent, getActionButtons } = useOperation<Member.MemberGoodsLimitRsp>({
    onClose: () => onSearch()
})

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(queryMemberGoodsLimit, {
    params: {
        pageNum: 1,
        pageSize: 20,
        roletype: 7
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'membername', label: 'member.institution.goodscfg.membername' },
    { field: 'roledisplay', label: 'member.institution.goodscfg.roledisplay' },
    { field: 'goodsdisplay', label: 'member.institution.goodscfg.goodsdisplay' },
    { field: 'isnodisplay', label: 'member.institution.goodscfg.nodisplay', formatValue: (val) => flagEnum.getEnumTypeName(val) },
    { field: 'iscannotbuy', label: 'member.institution.goodscfg.cannotsell', formatValue: (val) => flagEnum.getEnumTypeName(val) },
    { field: 'iscannotsell', label: 'member.institution.goodscfg.tips1', formatValue: (val) => flagEnum.getEnumTypeName(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { filterOption, queryParams, getQueryParams, resetFilters } = useDataFilter<Member.MemberGoodsLimitReq>({
    filters: [
        {
            field: 'userid',
            label: t('member.institution.goodscfg.userid')
        },
        {
            field: 'marketid',
            label: t('member.institution.goodscfg.marketid'),
            options: () => getMarketOptions([15, 97, 99], true),
            onChange: () => goodsRef.value.onReset()
        },
        {
            field: 'goodsid',
            label: t('member.institution.goodscfg.goodsid')
        }
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

const onSearch = () => {
    const qs = getQueryParams()
    run(qs)
}
</script>