import {useEffect, useState} from "react";
import {Search} from '@nutui/icons-react-taro'
import {Button, Input, InfiniteLoading} from '@nutui/nutui-react-taro'
import {pageHjmCar} from "@/api/hjm/hjmCar";
import {HjmCar} from "@/api/hjm/hjmCar/model";
import Taro from '@tarojs/taro'
import './location.scss'
import BestSellers from "./BestSellers";
import {getUserInfo} from "@/api/layout";

/**
 * 文章终极列表
 * @constructor
 */
const List = () => {
  const [keywords, setKeywords] = useState<string>('')
  const [list, setList] = useState<HjmCar[]>([])

  const onKeywords = (keywords: string) => {
    setKeywords(keywords)
  }

  const reload = async () => {
    // 搜索条件
    const where = {status: 1, deleted: 0, keywords}

    // 读取用户信息
    const user = await getUserInfo();

    // 判断身份
    const roleCode = Taro.getStorageSync('RoleCode');
    if(roleCode == 'kuaidiyuan'){
      // @ts-ignore
      where.driverId = user.userId;
    }
    if(roleCode == 'zhandian'){
      // @ts-ignore
      where.organizationId = user.organizationId;
    }
    if(roleCode == 'kuaidi'){
      // @ts-ignore
      where.organizationParentId = user.organizationId;
    }
    if(roleCode == 'Installer'){
      // @ts-ignore
      where.installerId = user.userId;
    }
    if(roleCode == 'user'){
      return false;
    }

    // 获取车辆列表
    pageHjmCar(where).then(res => {
      setList(res?.list || [])
    })
  }

  useEffect(() => {
    reload()
  }, [])


  return (
    <>
      <div className={'fixed z-20 top-5 left-0 w-full'}>
        <div className={'px-4'}>
          <div
            style={{
              display: 'flex',
              alignItems: 'center',
              background: '#fff',
              padding: '0 10px',
              borderRadius: '20px'
            }}
          >
            <Search/>
            <Input
              placeholder="车辆编号"
              value={keywords}
              onChange={onKeywords}
              onConfirm={reload}
            />
            <div
              className={'flex items-center'}
            >
              <Button type="warning" onClick={reload}>
                查询
              </Button>
            </div>
          </div>
        </div>
      </div>
      <InfiniteLoading
        className={'w-full fixed left-0 top-20'}
      >
        <BestSellers data={list}/>
      </InfiniteLoading>
    </>
  )
}
export default List
