import React, { useState, useEffect, useRef } from 'react'; import { Modal, Button, message, Form, Input, Switch, Select, Tag } from 'antd'; import {useStore} from '../index'; import { authShopRoleListApi } from '@/common/api'; import { List, saveStandGroupApi } from '../api'; import { debounce } from 'lodash'; import ShopSelect from '@/components/ShopSelect'; const Option = Select.Option; interface RoleShop { roleCode?: string // 角色编码 roleName?: string // 角色名称 shopId?: number // 门店id shopName?: string // 门店名称 } export default function DetailModal() { const { visible, setVisible, shopList, roleList, setLoading: setPageLoading, currentItem, setCurrentItem } = useStore(); const [loading, setLoading] = useState(false); const [staffList, setStaffList] = useState([]); const [roleShop, setRoleShop] = useState({}); const [form] = Form.useForm(); const resetRef = useRef(true); // const [shopValue, setShopValue] = useState(); useEffect(() => { if (visible && currentItem.id) { const _staff = currentItem?.staffList?.map((i:any) => ({value: i.staffId, label: i.staffName})); // setShopValue([{value: currentItem.shopId, label: currentItem.shopName}]); form.setFieldsValue({ name: currentItem.name, roleCode: currentItem.roleCode, roleName: currentItem.roleName, shop: [{value: currentItem.shopId, label: currentItem.shopName}], staff: _staff, }); setRoleShop({roleCode: currentItem.roleCode, roleName: currentItem.roleName, shopId: currentItem.shopId, shopName: currentItem.shopName}); } }, [visible]); useEffect(() => { if (roleShop.roleCode && roleShop.shopId) { getStaffList(roleShop); // 编辑首次进入不重置表单人员 if (!(currentItem.id && resetRef.current)) { form.setFieldsValue({staff: []}); } resetRef.current = false; } else { setStaffList([]); form.setFieldsValue({staff: []}); } }, [roleShop.roleCode, roleShop.shopId]); // 获取员工列表 function getStaffList(params: any) { authShopRoleListApi({rangeValue: params.shopId, roleCode: params.roleCode}) .then(res => { setStaffList(res.data || []); }) .catch(e => message.error(e.message)); } function handleCancel() { setVisible(false); resetStatus(); } // 重置页面数据 function resetStatus() { form.resetFields(); setStaffList([]); setRoleShop({}); setCurrentItem({}); resetRef.current = true; } async function onFinish() { const params = await form.validateFields(); setLoading(true); const _staff = params.staff.map((v:any) => { const k = { staffId: v.value, staffName: v.label }; return k; }); const _params: List = { shopId: Array.isArray(params.shop)? params.shop[0].value: params.shop.value, shopName: Array.isArray(params.shop)? params.shop[0].label: params.shop.label, roleCode: params.roleCode, roleName: roleShop.roleName, staffList: _staff, name: params.name }; if (currentItem.id) { _params.id = currentItem.id; } saveStandGroupApi(_params) .then(res => { message.success(res.result); setLoading(false); setVisible(false); resetStatus(); setPageLoading(true); }) .catch(e => { message.error(e.message); setLoading(false); }); } return ( 取消 , , ]} >
{/* */} setRoleShop({ ...roleShop, shopId: item.value || undefined, shopName: item.label || undefined, })} // value={shopValue} />
); }