import {useState} from "react";
import Taro, {useDidShow} from '@tarojs/taro'
import {Button, Cell, CellGroup, Space, Empty, ConfigProvider, Tag} from '@nutui/nutui-react-taro'
import {Del, Edit} from '@nutui/icons-react-taro'
import {View, Text} from '@tarojs/components'
import {ClinicPrescription} from "@/api/clinic/clinicPrescription/model";
import {
  pageClinicPrescription,
  removeClinicPrescription
} from "@/api/clinic/clinicPrescription";
import FixedButton from "@/components/FixedButton";
import {copyText} from "@/utils/common";

const ClinicPrescriptionList = () => {
  const [list, setList] = useState<ClinicPrescription[]>([])
  const [loading, setLoading] = useState<boolean>(false)

  const reload = () => {
    setLoading(true)
    pageClinicPrescription({
      // 添加查询条件
      doctorId: Taro.getStorageSync('UserId'),
    })
      .then(data => {
        setList(data?.list || [])
      })
      .catch(() => {
        Taro.showToast({
          title: '获取数据失败',
          icon: 'error'
        });
      })
      .finally(() => {
        setLoading(false)
      })
  }

  const onDel = async (item: ClinicPrescription) => {
    const res = await Taro.showModal({
      title: '确认删除',
      content: `确定要删除处方编号「${item.orderNo}」吗？`,
    })

    if (res.confirm) {
      try {
        await removeClinicPrescription(item.id)
        Taro.showToast({
          title: '删除成功',
          icon: 'success'
        });
        reload();
      } catch (error) {
        Taro.showToast({
          title: '删除失败',
          icon: 'error'
        });
      }
    }
  }

  const onEdit = (item: ClinicPrescription) => {
    Taro.navigateTo({
      url: `/clinic/clinicPrescription/add?id=${item.id}`
    })
  }

  const onDetail = (item: ClinicPrescription) => {
    Taro.navigateTo({
      url: `/clinic/clinicPrescription/detail?id=${item.id}`
    })
  }

  const getSexName = (sex?: number) => {
    return sex === 0 ? '男' : sex === 1 ? '女' : ''
  }

  useDidShow(() => {
    reload()
  });

  if (list.length === 0 && !loading) {
    return (
      <ConfigProvider>
        <View className={'h-full flex flex-col justify-center items-center'} style={{
          height: 'calc(100vh - 300px)',
        }}>
          <Empty
            style={{
              backgroundColor: 'transparent'
            }}
            description="暂无处方数据"
          />
          <Space style={{marginTop: '20px'}}>
            <Button
              type="primary"
              onClick={() => Taro.navigateTo({url: '/clinic/clinicPrescription/add'})}
            >
              新增处方
            </Button>
          </Space>
        </View>
      </ConfigProvider>
    )
  }

  return (
    <>
      <View className="p-3">
        {list.map((item) => (
          <CellGroup key={item.id} className="mb-3">
            <Cell
              title={item.orderNo}
              extra={
                <Tag type={'warning'} className="font-medium">待支付</Tag>
              }
              onClick={() => copyText(`${item.orderNo}`)}
            />
            <Cell
              title={'患者名称'}
              extra={
                <Space>
                  <Text className="font-medium">{item.realName}</Text>
                  <Text className="font-medium">{item.age}岁</Text>
                  <Text className="font-medium">{getSexName(item.sex)}</Text>
                </Space>
              }
            />
            {/*<Cell*/}
            {/*  title="处方类型"*/}
            {/*  extra={*/}
            {/*    <Tag type="info">*/}
            {/*      {getPrescriptionTypeText(item.prescriptionType)}*/}
            {/*    </Tag>*/}
            {/*  }*/}
            {/*/>*/}
            {item.diagnosis && (
              <Cell
                title="诊断结果"
                extra={
                  <Text className="text-gray-600 text-sm">
                    {item.diagnosis.length > 20
                      ? `${item.diagnosis.substring(0, 20)}...`
                      : item.diagnosis}
                  </Text>
                }
              />
            )}
            <Cell
              title="订单金额"
              extra={
                <Text className="text-red-500 font-medium">
                  ¥{item.orderPrice || '0.00'}
                </Text>
              }
            />
            <Cell
              title="创建时间"
              extra={
                <Text className="text-gray-500 text-xs">
                  {item.createTime}
                </Text>
              }
            />
            <Cell>
              <Space className="w-full justify-end">
                <Button
                  size="small"
                  icon={<Edit/>}
                  onClick={() => onDetail(item)}
                >
                  详情
                </Button>
                <Button
                  size="small"
                  icon={<Edit/>}
                  onClick={() => onEdit(item)}
                >
                  编辑
                </Button>
                <Button
                  size="small"
                  icon={<Del/>}
                  onClick={() => onDel(item)}
                >
                  删除
                </Button>
              </Space>
            </Cell>
          </CellGroup>
        ))}
      </View>

      <FixedButton
        text="开处方"
        onClick={() => Taro.navigateTo({url: '/clinic/clinicPrescription/add'})}
      />
    </>
  )
}

export default ClinicPrescriptionList;
