<template>
  <view class="container">
    <view class="content">
      <view class="userInfoTopFixe">
        <UserInfo :user-info="currentUser" type="hospital"></UserInfo>
        <Screening 
          :screen="screen" 
          @setScreenData="setScreenData" 
          @queryRecords="queryRecords" 
          @resetScreen="resetScreen" 
          @changeStartDate="changeStartDate" 
          @changeEndDate="changeEndDate"
        ></Screening>
      </view>
      <view class="recordBox">
        <view v-if="recordList.length == 0" class="noData">
          <NoData :value="noDataValue"></NoData>
        </view>
        <view class="recordBox_list">
          <view 
            class="record_item border_bottom displayFlexBetween" 
            v-if="recordList.length > 0" 
            v-for="(item, index) in recordList" 
            :key="index" 
            @click="jumpDetail(item)"
          >
            <view class="record_left">
              <view class="name">{{item.DeptName}}</view>
              <view class="text">{{item.DischargeTime}} {{item.CompetentDoctor}}</view>
            </view>
            <image class="public_right_img public_right_img30" :src="iconUrl.icon_right"></image>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { common } from '@/utils';
import icon from '@/utils/icon';
import { queryInpatientList } from '@/pagesPatient/service/record';
import UserInfo from '@/pagesPersonal/st1/components/userInfo/userInfo.vue';
import Screening from '@/pagesPatient/st1/components/screening/screening.vue';
import NoData from '@/pages/st1/components/noData/noData.vue';

const { proxy } = getCurrentInstance() as any;
const app = getApp();

const iconUrl = ref(icon);
const currentUser = ref<any>({});
const noDataValue = ref("暂无数据");
const recordList = ref<any[]>([]);
const screen = ref({
  screenKey: 'screen',
  btnName: '',
  startDate: common.dateFormat(new Date(Date.now() - (30 * 24 * 60 * 60 * 1000))).formatYear, //开始时间 yyyy-mm-dd
  endDate: common.dateFormat(new Date(Date.now() + (1 * 24 * 60 * 60 * 1000))).formatYear, //结束时间 yyyy-mm-dd
  state: [],
  sourceType: [],
  memberId: [],
  screenTime: [
    { label: "近一个月", value: "30", check: true },
    { label: "近三个月", value: "90", check: false },
    { label: "近半年", value: "180", check: false },
  ],
  columns: []
});

onLoad((options: any) => {
  try {
    let user = options.userInfo ? JSON.parse(options.userInfo) : app.globalData.currentUser;
    currentUser.value = user;
    QueryInpatientList();
  } catch (e) {
    console.error('Error parsing userInfo:', e);
  }
});

const refresh = () => {
  QueryInpatientList();
};

const updateScreen = (data: any) => {
  if (data && data.screen) {
    screen.value = data.screen;
  }
};

// 设置筛选数据
const setScreenData = (val: any) => {
  updateScreen(val);
};

// 选择结束时间
const changeEndDate = (val: any) => {
  updateScreen(val);
  recordList.value = [];
  QueryInpatientList();
};

// 获取选中的筛选时间
const changeStartDate = (val: any) => {
  updateScreen(val);
  recordList.value = [];
  QueryInpatientList();
};

// 查询
const queryRecords = () => {
  recordList.value = [];
  QueryInpatientList();
};

// 重置
const resetScreen = (val: any) => {
  let screenData = common.deepCopy(val);
  if (screenData.screen) {
    screen.value = screenData.screen;
  } else {
    // If val IS screen object
    // But original logic: this.setData({ screen: screen })
    // We assume val contains screen key if it matches original structure
    // If val is just the screen object, assign directly?
    // Let's stick to updateScreen logic which checks for .screen
    // If resetScreen returns the whole data object including screen:
    updateScreen(screenData);
  }
  recordList.value = [];
  QueryInpatientList();
};

const QueryInpatientList = async () => {
  let reqData = {
    HosId: app.globalData.districtId || app.globalData.hosId,
    StartDate: screen.value.startDate,
    EndDate: screen.value.endDate,
    IgnoreHosId: true,
    MemberId: currentUser.value.memberId,
    CardType: currentUser.value.cardType,
    CardNo: currentUser.value.cardNo,
    Store: {
      cardEncryptionStore: currentUser.value.encryptionStore || '',
      baseMemberEncryptionStore: currentUser.value.baseMemberEncryptionStore
    }
  };
  
  let { resp, resData } = await queryInpatientList(reqData);
  if (resData.RespCode == "10000") {
    if (common.isNotEmpty(resData.Data)) {
      recordList.value = resp;
    }
  }
};

// 跳转详情页面
const jumpDetail = (item: any) => {
  let url = `/pagesPatient/st1/business/prescriptionManagement/dischargeMedicationDetails/dischargeMedicationDetails?queryBean=${JSON.stringify(item)}`;
  common.goToUrl(url);
};
</script>

<style lang="scss">
.noData {
  width: 100%;
  padding-top: 0 !important;
  position: absolute;
  top: 50%;
  margin-top: -200upx;
}
.recordBox {
  padding: 328upx 30upx 30upx;
}
.recordBox_list {
  background: #fff;
  border-radius: 24upx;
  padding-left: 30upx;
}
.record_item {
  position: relative;
  padding: 32upx 0;
}
.name {
  font-size: 32upx;
  color: #333;
  margin-bottom: 18upx;
  font-weight: 600;
}
.text {
  font-size: 26upx;
  color: #999;
}
</style>
