<template>
  <view class="container">
    <u-sticky offsetTop="0rpx" customNavHeight="0rpx">
      <view class="search-view">
        <u--input 
          v-model="queryParams.name" 
          border="surround"
          placeholder="请输入名称或昵称" 
          placeholderStyle="color: #909399"
          color="#333333"
          customStyle="background: rgba(102, 126, 234, 0.08); border: 2rpx solid rgba(102, 126, 234, 0.3); border-radius: 24rpx; height: 66rpx"
          class="search-input"
          @blur="handleSearch" 
          suffixIcon="search" 
          suffixIconStyle="color: #667eea">
        </u--input>
        <view class="add-btn" @click="handleAdd()">
          <uni-icons type="plusempty" size="18" color="#667eea"></uni-icons>
          <text>新增</text>
        </view>
      </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">
            <view class="card-name-section">
              <view class="card-icon">
                <uni-icons type="person-filled" size="20" color="#ffffff"></uni-icons>
              </view>
              <view class="card-info">
                <text class="card-name">{{ item.name }}（{{ item.nickName }}）</text>
                <text class="card-code">{{ dictStr(item.sex, sexList) }} · {{ dictStr(item.type, typeList) }} · {{ item.age }}</text>
              </view>
            </view>
          </view>
          
          <view class="card-body">
            <view class="info-row">
              <view class="info-item" v-if="item.birthday">
                <text class="info-label">生日</text>
                <text class="info-value">{{ item.birthday }}</text>
              </view>
              <view class="info-item" v-if="item.identityCard">
                <text class="info-label">身份证</text>
                <text class="info-value">{{ item.identityCard }}</text>
              </view>
              <view class="info-item" v-if="item.height">
                <text class="info-label">身高（CM）</text>
                <text class="info-value">{{ item.height }}</text>
              </view>
              <view class="info-item" v-if="item.weight">
                <text class="info-label">体重（KG）</text>
                <text class="info-value">{{ item.weight }}</text>
              </view>
              <view class="info-item info-item-full" v-if="item.remark">
                <text class="info-label">备注</text>
                <text class="info-value">{{ item.remark }}</text>
              </view>
            </view>
          </view>
          
          <view class="operate" @click.stop>
            <view class="btn-edit" @click="handleEdit(item)">
              <uni-icons type="compose" size="16" color="#667eea"></uni-icons>
              <text>修改</text>
            </view>
            <view class="btn-delete" @click="handleDelete(item)">
              <uni-icons type="trash" size="16" color="#f5576c"></uni-icons>
              <text>删除</text>
            </view>
          </view>
        </view>
      </u-list-item>
      <view>
      </view>
      <u-loadmore :status="status" loadingIcon="semicircle" height="88" fontSize="32rpx" @loadmore="loadmore" />
    </u-list>
  </view>
         <!--返回首页按钮 -->
         <suspend></suspend>
</template>

<script setup>
import { listPerson, delPerson } from '@/api/health/person'
import { getDicts } from '@/api/system/dict/data.js'
import { timeHandler } from '@/utils/common.ts'
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 typeList = ref([])
const sexList = ref([])
const  flag= ref(true)
const data = reactive({
  filterPanel: false,
  queryParams: {
    name: null,
    type: null,
    nickName: null
   } 
})
const { filterPanel, queryParams} = toRefs(data)
const windowHeight = computed(() => {
			uni.getSystemInfoSync().windowHeight - 50
		})	
onLoad(() => {
    getList()
  });

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

  function handleSearch() {
      pageNum.value = 1
      listData.value = []
      getList()
     }

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

function loadmore() {
      pageNum.value += 1
      if (status.value == 'loadmore') {
        getList()
      }
    }
function getList() {
       // 类型
       getDicts('person_type').then(res => {
        typeList.value = res.data
      })
      // 类型
        getDicts('sys_user_sex').then(res => {
        sexList.value = res.data
      })
      status.value = 'loading'
      listPerson({ 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 handleEdit(item) {
      uni.navigateTo({ url: `/pages/health/person/addEdit?id=${item.id}` })
      isShow.value = true
    }
    function handleAdd() {
      uni.navigateTo({ url: `/pages/health/person/addEdit` })
      isShow.value = true
    }
    function handleDelete(item) {
      uni.showModal({
        title: '确认删除',
        content: '确定要删除这条记录吗？',
        confirmText: '删除',
        cancelText: '取消',
        confirmColor: '#f5576c',
        cancelColor: '#909399',
        success: function (res) {
          if (res.confirm) {
            delPerson(item.id).then(() => {
              uni.showToast({
                title: '删除成功',
                icon: 'success'
              })
              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);
  gap: 24rpx;

  .search-input {
    background: rgba(102, 126, 234, 0.08);
    color: #333333;
    flex: 1;
    margin-right: 0;
    border-radius: 24rpx;
    border: 2rpx solid rgba(102, 126, 234, 0.3);
    height: 66rpx !important;
    display: flex;
    align-items: center;
    
    /* H5端placeholder样式 */
    :deep(input::placeholder) {
      color: #909399 !important;
      font-size: 14px !important;
    }
    
    :deep(input::-webkit-input-placeholder) {
      color: #909399 !important;
      font-size: 14px !important;
    }
    
    :deep(input::-moz-placeholder) {
      color: #909399 !important;
      font-size: 14px !important;
    }
    
    /* 输入文字颜色 */
    :deep(.u-input__content__field-wrapper__field) {
      color: #333333 !important;
    }
    
    :deep(input) {
      color: #333333 !important;
    }
  }
  
  .add-btn {
    display: flex;
    align-items: center;
    gap: 6rpx;
    padding: 12rpx 24rpx;
    background: rgba(102, 126, 234, 0.08);
    border-radius: 24rpx;
    border: 2rpx solid rgba(102, 126, 234, 0.3);
    transition: all 0.3s ease;
    flex-shrink: 0;
    
    &:active {
      transform: scale(0.95);
      background: rgba(102, 126, 234, 0.12);
    }
    
    text {
      color: #667eea;
      font-size: 28rpx;
      font-weight: 600;
    }
    
    uni-icons {
      color: #667eea;
    }
  }
}

.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;
    }
    
    .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;
      flex-direction: row;
      align-items: center;
      gap: 12rpx;
      
      .card-name {
        color: #ffffff;
        font-size: 30rpx;
        font-weight: 700;
        line-height: 1.3;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        flex-shrink: 0;
        letter-spacing: 0.5rpx;
      }
      
      .card-code {
        color: rgba(255, 255, 255, 0.9);
        font-size: 28rpx;
        font-weight: 500;
        line-height: 1.3;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        flex: 1;
        min-width: 0;
        letter-spacing: 0.3rpx;
      }
    }
  }

  .card-body {
    padding: 24rpx;
    background: #fafbfc;
    border: 2rpx solid #f0f2f5;
    margin: 15rpx 16rpx 2rpx;
    border-radius: 12rpx;
  }

  .info-row {
    display: flex;
    flex-wrap: wrap;
    margin: 0 -12rpx;

    .info-item {
      width: 50%;
      padding: 0 12rpx;
      box-sizing: border-box;
      display: flex;
      flex-direction: column;
      margin-bottom: 20rpx;
      
      &.info-item-full {
        width: 100%;
      }

      .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;
        margin-bottom: 8rpx;
      }

      .info-value {
        font-size: 26rpx;
        color: #333;
        font-weight: 500;
        flex: 1;
        line-height: 1.5;
        word-break: break-all;
      }
      
      &:not(.info-item-full) .info-value {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }

  .operate {
    display: flex;
    justify-content: flex-end;
    padding: 16rpx 24rpx 24rpx;
    gap: 16rpx;

    .btn-edit,
    .btn-delete {
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 6rpx;
      padding: 0 24rpx;
      height: 64rpx;
      border-radius: 12rpx;
      font-size: 26rpx;
      font-weight: 500;
      transition: all 0.3s ease;
      
      &:active {
        transform: scale(0.95);
      }
    }
    
    .btn-edit {
      background: rgba(102, 126, 234, 0.1);
      color: #667eea;
      border: 1rpx solid rgba(102, 126, 234, 0.3);
    }
    
    .btn-delete {
      background: rgba(245, 87, 108, 0.1);
      color: #f5576c;
      border: 1rpx solid rgba(245, 87, 108, 0.3);
    }
  }
}
</style>