<template>
  <div class="app-container">
    <el-form
      ref="queryRef"
      :model="queryParams"
      :inline="true"
      label-width="100px"
    >
      <el-form-item
        label="年份："
        prop="visitsYear"
      >
        <el-date-picker
          v-model="visitsYear"
          type="year"
          placeholder="请选择年份"
          format="YYYY"
          value-format="YYYY"
        />
      </el-form-item>
      <el-form-item
        label="时间类型："
        prop="dateType"
      >
        <el-select
          v-model="queryParams.dateType"
          placeholder=""
          clearable
          style="width: 150px"
          @change="dateTypeChange"
        >
          <el-option
            v-for="dateType in dateTypeOptions"
            :key="dateType.value"
            :label="dateType.label"
            :value="dateType.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="queryParams.dateType=='1'"
        label="就诊时间"
        prop="visitTime1"
      >
        <el-date-picker
          
          v-model="queryParams.visitTime1"
          type="datetime"
          placeholder="请选择就诊时间"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
       
      <el-form-item
        v-if="queryParams.dateType=='2'"
        label="就诊时间"
        prop="visitTime2"
      >
        <el-date-picker

          v-model="queryParams.visitTime2"
          type="week"
          format="[Week] ww"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="请选择就诊时间"
          @change="visitTimeChange"
        />
      </el-form-item>
      <el-form-item
        v-if="queryParams.dateType=='3'"
        label="就诊时间"
        prop="visitTime3"
      >
        <el-date-picker
         
          v-model="queryParams.visitTime3"
          type="month"
          placeholder="请选择就诊时间"
        />
      </el-form-item>
      <!-- <el-form-item label="报损日期：">
        <el-date-picker
          v-model="occurrenceTime"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 300px"
          value-format="YYYY-MM-DD"
        />
      </el-form-item> -->
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <!-- v-hasPermi="['system:user:import']" -->
        <el-button
          type="primary"
          plain
          icon="Search"
          @click="handleQuery"
        >
          查询
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- v-hasPermi="['system:user:export']" -->
        <el-button
          type="warning"
          plain
          icon="CircleClose"
          @click="handleClear"
        >
          重置
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="purchaseinventoryList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
      />
   
      <el-table-column
        key="itemQuantity"
        label="就诊人数"
        align="center"
        prop="itemQuantity"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="itemQuantity"
        label="发热人数"
        align="center"
        prop="itemQuantity"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="itemQuantity"
        label="腹泻人数"
        align="center"
        prop="itemQuantity"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column
        label="报损日期"
        align="center"
        key="approvalTime"
        prop="approvalTime"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.approvalTime) }}</span>
        </template>
      </el-table-column> -->
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="numberOfVisits">
import {getReportLossPage,} from "./statisticalManagent";

const { proxy } = getCurrentInstance();
const purchaseinventoryList = ref([]);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const occurrenceTime = ref([]);
const dateTypeOptions = ref([]);
const locationIdList = ref([]);
const visitsYear = formatDate(new Date(),1)
const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    dateType:"1",
    visitTime1: formatDate(new Date(),2), 
    visitTime2: formatDate(new Date(),3), 
    visitTime3: formatDate(new Date(),4), 

  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);
function visitTimeChange(val){
  getWeekDataList(val)
}
function getWeek(){
  if(new Date().getDay()==1){ //当前是星期一
    let weekTime = formatDate(new Date(),5,1)
    return weekTime
  }else if(new Date().getDay()==2){ //当前是星期二
    let weekTime = formatDate(new Date(),5,2)
    return weekTime
  }else if(new Date().getDay()==3){ //当前是星期三
    let weekTime = formatDate(new Date(),5,3)
    return weekTime
  }else if(new Date().getDay()==4){ //当前是星期四
    let weekTime = formatDate(new Date(),5,4)
    return weekTime
  }else if(new Date().getDay()==5){ //当前是星期五
    let weekTime = formatDate(new Date(),5,5)
    return weekTime
  }else if(new Date().getDay()==6){ //当前是星期六
    let weekTime = formatDate(new Date(),5,6)
    return weekTime
  }else if(new Date().getDay()==7){ //当前是星期日
    let weekTime = formatDate(new Date(),5,7)
    return weekTime
  }  
}

function getWeekDataList(val) {
  let oneDayTime = 1000 * 60 * 60 * 24
  let today =val?new Date(val):new Date(getWeek())
  let todayDay = today.getDay() || 7
  let startDate =new Date(today.getTime() + oneDayTime * (todayDay - 1))
  let dateList = []
  for (let i = 0; i < 7; i++) {
    let temp =new Date(startDate.getTime() - i * oneDayTime)
    let year = temp.getFullYear()
    let month = temp.getMonth() + 1 < 10 ? '0' + (temp.getMonth() + 1) : temp.getMonth() + 1
    let day = temp.getDate() < 10 ? '0' + temp.getDate() : temp.getDate()
    dateList[i] = `${year}-${month}-${day}`
  }
  console.log(dateList,"选中周的时间")
  return dateList
}
function getPharmacyCabinetLists() {
  dateTypeOptions.value = [{value:"1",label:"每天"},{value:"2",label:"每周"},{value:"3",label:"每月"}]
  queryParams.value.dateType = "1"
  // getInit().then((response) => {
  //   chrgitmLv_enumTextOptions.value = response.data.chrgitmLvOptions 
  // })
}
function dateTypeChange(val){
  console.log(val,queryParams.value.visitTime2,"!@1")
  if(val == "2"){ //每周
    getWeekDataList()
  }
}
/** 查询调拨管理项目列表 */
function getList() {
  loading.value = true;
  getReportLossPage(queryParams.value).then((res) => {
    loading.value = false;
    purchaseinventoryList.value = res.data.records
    total.value = res.data.total;
  });
}
function formatDate(cellValue,yearType,value) {
  if (cellValue == null || cellValue == "") return "";
  var date = new Date(cellValue) 
  var year = date.getFullYear()
  var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() 
  var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() 
  var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() 
  var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  // var nowDayOfWeek = date.getDay(); // 今天本周的第几天
  // var nowDay = nowDayOfWeek || 7;
  // var startWeek = new Date(year, month, day + 1 - nowDay)
  // console.log(startWeek,"startWeek")
  if(yearType==2||yearType==1||yearType==3){
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
  }else if(yearType==5){
    day = date.getDate() < 10 ? '0' + date.getDate()-value : date.getDate()-value
    console.log(day,"day")
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
  }else{
    return year + '-' + month
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNo = 1;
  getList();
}

/** 清空条件按钮操作 */
function handleClear() {
  // 清空查询条件
  proxy.resetForm("queryRef");
  getList();
}

/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

getList();
getPharmacyCabinetLists()
</script>
<style scoped>
.custom-tree-node {
  display: flex;
  align-items: center;
}

</style>