import{i as e,t}from"./react-B8IZ02wI.js";import{t as n}from"./select-CDwQ-YxO.js";import{k as r,m as i}from"./authStore-BvQBJ9ky.js";import{C as a,Sn as o,ct as s,n as c,pt as l,st as u}from"./index-Bg7bo8aA.js";var d=e(t(),1);function f(e){let t=String(e.phoneNumber||``).trim(),n=String(e.simCode||``).trim();return t&&n&&t!==n?`${t}（${n}）`:t||n||`未知号码`}function p(e){let t=Number(e.id);return Number.isFinite(t)?{id:t,slotType:String(e.slotType||``),phoneNumber:String(e.phoneNumber||``).trim(),simCode:String(e.simCode||``).trim()}:null}function m(e,t,n){let r=e.find(e=>!(String(e.slotType)!==t||n!=null&&Number(e.id)===n));return r?p(r):null}function h(e,t){return[`SLOT1`,`SLOT2`].map(n=>{let r=a(n),i=t?.excludeSimId==null?void 0:e.find(e=>String(e.slotType)===n&&Number(e.id)===t.excludeSimId);if(i){let e=p(i);return{value:n,label:e?`${r} · 本卡 ${f(e)}`:`${r} · 本卡`}}let o=m(e,n,t?.excludeSimId);return o?{value:n,label:`${r} · ${f(o)}`}:{value:n,label:`${r} · 空闲`}})}var g=c();function _({open:e,displaced:t,preferExcludeDeviceId:a,onClose:c,onSuccess:p}){let[_]=l.useForm(),y=l.useWatch(`device_id`,_),b=l.useWatch(`slot_type`,_),[x,S]=(0,d.useState)([]),[C,w]=(0,d.useState)([]),[T,E]=(0,d.useState)(!1);(0,d.useEffect)(()=>{!e||!t||(_.resetFields(),w([]),i.list({page:1,size:300}).then(e=>{let t=e.data.list||[];S(t);let n=t.find(e=>a==null||Number(e.id)!==a);n&&_.setFieldsValue({device_id:n.id,slot_type:`SLOT1`})}).catch(()=>S([])))},[e,t,a,_]),(0,d.useEffect)(()=>{if(!e||!y){w([]);return}i.simCards(y).then(e=>{let n=e.data||[];w(n);let r=!m(n,`SLOT1`,t?.id);_.setFieldsValue({slot_type:r?`SLOT1`:`SLOT2`})}).catch(()=>w([]))},[e,y,t?.id,_]);let D=(0,d.useMemo)(()=>h(C,{excludeSimId:t?.id}),[C,t?.id]),O=(0,d.useMemo)(()=>b?m(C,b,t?.id):null,[C,b,t?.id]);return(0,g.jsx)(u,{title:`原卡重新绑定设备`,open:e&&!!t,onCancel:c,onOk:()=>_.submit(),confirmLoading:T,okText:`绑定`,destroyOnHidden:!0,children:t&&(0,g.jsxs)(g.Fragment,{children:[(0,g.jsx)(o,{type:`info`,showIcon:!0,style:{marginBottom:16},message:`原卡 ${f(t)} 已从设备解绑，可在此快速装到其他设备。`}),(0,g.jsxs)(l,{form:_,layout:`vertical`,onFinish:async e=>{if(!t)return;let n=m(C,e.slot_type,t.id),i=async()=>{E(!0);try{await r.rebind(t.id,e),s.success(`原卡 ${f(t)} 已重新装机`),c(),p?.()}catch(e){s.error(e instanceof Error?e.message:`重新绑定失败`)}finally{E(!1)}};if(n){u.confirm({title:`目标卡槽已有卡`,content:`${v(e.slot_type)}当前为 ${f(n)}，确认后该号码将解绑，再装入原卡 ${f(t)}。`,okText:`确认换绑`,okType:`danger`,cancelText:`取消`,onOk:i});return}await i()},children:[(0,g.jsx)(l.Item,{name:`device_id`,label:`目标设备`,rules:[{required:!0,message:`请选择设备`}],children:(0,g.jsx)(n,{showSearch:!0,optionFilterProp:`label`,placeholder:`选择设备`,options:x.map(e=>({value:e.id,label:`${e.deviceCode} ${e.brand||``} ${e.model||``}`.trim()}))})}),(0,g.jsx)(l.Item,{name:`slot_type`,label:`装入卡槽`,rules:[{required:!0,message:`请选择卡槽`}],children:(0,g.jsx)(n,{options:D})}),O&&(0,g.jsx)(o,{type:`warning`,showIcon:!0,message:`该卡槽当前为 ${f(O)}，绑定后该号码将解绑。`})]})]})})}function v(e){return e===`SLOT2`?`卡槽2`:`卡槽1`}export{f as i,h as n,m as r,_ as t};