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";

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

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

  const reload = () => {
    // 搜索条件
    const where = {status: 1, deleted: 0, keywords}
    // 判断身份
    const roleCode = Taro.getStorageSync('RoleCode');
    if(roleCode == 'kuaidiyuan'){
      // @ts-ignore
      where.driverId = Taro.getStorageSync('UserId')
    }
    if(roleCode == 'zhandian'){
      // @ts-ignore
      where.organizationId = Taro.getStorageSync('OrganizationId');
    }
    if(roleCode == 'kuaidi'){
      if(Taro.getStorageSync('OrganizationId') == Taro.getStorageSync('OrganizationParentId')){
        // @ts-ignore
        where.organizationParentId = Taro.getStorageSync('OrganizationParentId');
      }else {
        // @ts-ignore
        where.organizationId = Taro.getStorageSync('OrganizationId');
      }
    }
    if(roleCode == 'Installer'){
      // @ts-ignore
      where.installerId = Taro.getStorageSync('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
