
import { Button, Navigator, ScrollView, Text, View } from "@tarojs/components"
import { memo, useEffect, useState } from "react"
import {weightListApi,weightDeleteApi} from "@/api/weightList"
import "./index.scss"
import Taro, { showModal } from "@tarojs/taro"
import { alert } from "@/common/common"
import useLogin from "@/use/useLogin"
const weightListManager = ()=>{
  useLogin()
  return(
    <View className="weight-list-manager">
      <WeightList />
    </View>
  )
}
interface Params{
  refresherEnabled?: boolean,//是否开启刷新
  onSelect?: (item:any,index:number)=>void,//列表选择

}
// 码单列表
const WeightList = (props:Params)=>{
  const {fetchData, state} = weightListApi();
  const getData = ()=>{
    fetchData();
  }
  useEffect(()=>{
    Taro.eventCenter.on("weightList:refresh", getData);
    getData();
    return ()=>{
      Taro.eventCenter.off("weightList:refresh", getData);
    }
  },[]);
  // 处理刷新
  const [refreshState, setRefreshState] = useState(false);
  const handleRefresh = async ()=>{
    setRefreshState(true);
    await fetchData();
    setRefreshState(false);
  }
  const data = Array.from({length:0});
  // 删除码单
  const {fetchData: deleteFetch} = weightDeleteApi()
  const handleDelete = (item:any)=>{
    showModal(({
      title: "提示",
      content: "是否删除码单？",
      async success(ev){
        if(ev.confirm){
          const result = await deleteFetch({id:item.id});
          if(result.success){
            alert.success("删除成功");
            getData();
          }else{
            alert.success(result.msg);
          }
        }
      }
    }))
  }

  return (
    <View className="weight-list-scroll-view">
      <ScrollView scrollY refresherEnabled enhanced refresherTriggered={refreshState} onRefresherRefresh={handleRefresh}>
        {
          // data.length>0?
          state?.data?.list?.length>0?
          state?.data?.list?.map((item,index)=>{
            return(
              <View onClick={()=>props.onSelect&&props.onSelect(item,index)} onLongPress={()=>handleDelete(item)} className={`weight-list-list ${item.is_default&&'address-active'}`}>
                <View className="weight-list-user">
                  {item.title}
                  {
                    item.is_default&&<Text className="weight-list-list-default">默认</Text>
                  }
                </View>
                <View className="weight-list-list-bottom">
                  <View className="weight-list-list-info">
                    {item.purchaser_name}
                    <Text className="weight-list-list-phone">{item.phone.replace(item.phone.substring(3,7), "****")}</Text>
                  </View>
                  <Navigator url={`/pages/weightListAdd/index?type=edit&id=${item.id}`} hoverClass="none" className="weight-list-edit">
                    <Text className="iconfont icon-bianji"></Text>
                  </Navigator>
                </View>
              </View>
            );
          }):
          <View className="weight-list-no-data">
            <View>您末添加任何码单信息</View>
            正在使用平台默认码单信息
          </View>
        }
      </ScrollView>
      <Navigator url="/pages/weightListAdd/index?type=add" hoverClass="none" className="add-weight-list">添加新码单信息</Navigator>
    </View>
  )
}

export default weightListManager;