<template>
  <view class="container">
    <u-sticky offsetTop="0rpx" customNavHeight="0rpx">
      <view class="search-view">
        <u--input v-model="queryParams.medicalName" border="false" type="select" @click="handleMedical"
          placeholder="请选择药品" class="search-input" suffixIcon="search" suffixIconStyle="color: #909399">
        </u--input>
      </view>
    </u-sticky>
    <u-list @scrolltolower="loadmore" :spaceHeight="116" lowerThreshold="100">
      <u-list-item v-for="(item, index) in listData" :key="index">
        <view class="list-item">
          <view class="item-header" @click="enterDetails(item)">
            <view class="card-name-section">
              <view class="card-icon">
                <uni-icons type="shop" size="20" color="#ffffff"></uni-icons>
              </view>
              <view class="card-info">
                <text class="card-name">{{ item.medicineName }} - 库存{{ item.leftCount }}{{ dictStr(item.unit, unitList) }}</text>
              </view>
            </view>
          </view>
          
          <!-- <view class="card-body">
            <view class="info-row">
              <view class="info-item">
                <text class="info-label">规格总数</text>
                <text class="info-value">{{ item.totalCount }}{{ dictStr(item.unit, unitList) }}</text>
              </view>
            </view>
          </view> -->
        </view>
      </u-list-item>
      <u-loadmore :status="status" loadingIcon="semicircle" height="88" fontSize="32rpx" @loadmore="loadmore" />
    </u-list>
    <u-picker itemHeight="88" :show="settingPickShow" :columns="settingColumns" keyName="settingName"
      @confirm="settingConfirm" @cancel="settingCancel"></u-picker>
      <u-picker itemHeight="88" :show="showMedical" :columns="medicalList" keyName="shortNameBrandPackaging" @cancel="handleMedicalCancel"
        @confirm="handleMedicalConfirm"></u-picker>
  </view>
         <!--返回首页按钮 -->
         <suspend></suspend>
</template>

<script setup>
import { listMedicineRealtimeStock } from '@/api/health/medicineStockIn'
import { listMedicineBasic } from '@/api/health/medicineBasic'
import { getDicts } from '@/api/system/dict/data.js'
import {onLoad,onShow} from "@dcloudio/uni-app";
import dayjs from 'dayjs'
    // 计算属性与监听属性是在vue中而非uniap中 需要注意！！！
import {reactive ,toRefs,ref,computed }from "vue";
const pageNum = ref(1)
const listData = ref([])
const isShow = ref(false)
const status = ref('loadmore')
const settingPickShow = ref(false)
const settingColumns = ref([])
const timeShow= ref(false)
const stateList = ref([])

const unitList = ref([])
const medicalList = ref([])
const showMedical = ref(false)
const  flag= ref(true)
const data = reactive({
  filterPanel: false,
  queryMedicalParams: {
    pageNum: 1,
    pageSize: 100
  },
  queryParams: {
    name: null,
    type: null,
    healthRecordId: null,
    dosingTime: null,
    dosage: null,
    medicineId: null,
    resource: null,
    place: null
   } 
})
const { filterPanel, queryMedicalParams, queryParams} = toRefs(data)
const windowHeight = computed(() => {
			uni.getSystemInfoSync().windowHeight - 50
		})	
onLoad(() => {
    getList()
  });

 onShow(() => {
    if (isShow.value) {
      listData.value=[]
      getList()
      isShow.value = false
    }
  });

  function dictStr(val, arr) {
      let str = ''
      arr.map(item => {
        if (item.dictValue === val) {
          str = item.dictLabel
        }
      })
      return str
    }

  function selectType(item) {
      queryParams.value.state = item.dictValue
      stateList.value.map(ele => {
        if (ele.dictValue == item.dictValue) {
          ele.selected = true
          Reflect.set(ele, 'selected', true)
        } else {
          Reflect.set(ele, 'selected', false)
        }
      })
    }

  function  openOrCloseDate(data) {
      timeShow.value = !timeShow.value
      flag.value = data
    }
function loadmore() {
      pageNum.value += 1
      if (status.value == 'loadmore') {
        getList()
      }
    }
function getList() {
  listMedicineBasic(queryMedicalParams.value).then((response) => {
        medicalList.value = [response.rows] 
      })
       // 用药地点
       getDicts('medical_unit').then(res => {
        unitList.value = res.data
      })
         
      status.value = 'loading'
      listMedicineRealtimeStock({ pageSize: 10, pageNum: pageNum.value, ...queryParams.value }).then(res => {
        listData.value = listData.value.concat(res.rows)
        if (listData.value.length < res.total) {
          status.value = 'loadmore'
        } else {
          status.value = 'nomore'
        }
      }).catch(() => {
        status.value = 'nomore'
      })
    }

function settingConfirm(e) {
      queryParams.value.settingId = e.value[0].settingId
      queryParams.value.settingName = e.value[0].settingName
      settingPickShow.value = false
    }
function settingCancel() {
      settingPickShow.value = false
    }

    function handleMedical() {
        if (medicalList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '药品名称为空  ', type: 'warning'
          })
        } else {
          showMedical.value = true
        }
     }
  function   handleMedicalConfirm(e) {
    queryParams.value.medicalName = e.value[0].shortNameBrandPackaging
    queryParams.value.medicineId= e.value[0].id
        showMedical.value = false
        pageNum.value = 1
      listData.value = []
      getList()
  }
 function handleMedicalCancel() {
  showMedical.value = false
  queryParams.value.medicalName = null
  queryParams.value.medicineId = null
  pageNum.value = 1
  listData.value = []
  getList()
}



	
</script>

<style lang="scss" scoped>
page {
  height: 100%;
  overflow: auto;
}
.search-view {
  padding: 12rpx 32rpx;
  background-color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  z-index: 100;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);

  .search-input {
    background: rgba(102, 126, 234, 0.08);
    color: #333333;
    flex: 1;
    border-radius: 24rpx;
    border: 2rpx solid rgba(102, 126, 234, 0.3);
    height: 66rpx !important;
  }
}

.list-item {
  margin: 10rpx 24rpx;
  background-color: #fff;
  border-radius: 16rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
  transition: all 0.2s ease;
  
  &:active {
    transform: scale(0.98);
    box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0.06);
  }

  .item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16rpx 24rpx;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    
    .card-name-section {
      display: flex;
      align-items: center;
      flex: 1;
      min-width: 0;
      margin-right: 16rpx;
    }
    
    .card-icon {
      width: 40rpx;
      height: 40rpx;
      background: rgba(255, 255, 255, 0.25);
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 12rpx;
      flex-shrink: 0;
    }
    
    .card-info {
      flex: 1;
      min-width: 0;
      display: flex;
      align-items: center;
      
      .card-name {
        color: #ffffff;
        font-size: 30rpx;
        font-weight: 700;
        line-height: 1.3;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        letter-spacing: 0.5rpx;
      }
    }
  }

  .card-body {
    padding: 24rpx;
    background: #fff;
  }

  .info-row {
    display: flex;
    flex-wrap: wrap;
    gap: 24rpx;
    margin-bottom: 0;

    .info-item {
      flex: 0 0 calc(50% - 12rpx);
      display: flex;
      flex-direction: column;
      gap: 4rpx;
      min-width: 0;
      
      .info-label {
        font-size: 24rpx;
        color: #667eea;
        font-weight: 500;
        background: rgba(102, 126, 234, 0.08);
        padding: 6rpx 12rpx;
        border-radius: 8rpx;
        align-self: flex-start;
      }

      .info-value {
        font-size: 26rpx;
        color: #333;
        font-weight: 500;
        flex: 1;
        line-height: 1.5;
        word-break: break-all;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}
</style>