<template>
  <view class="box">
    <u-tabs :list="list1" lineColor="#feb048" :current="current" :scrollable="false" @change="changeTabs"></u-tabs>
    <view class="content">
      <view class="">
        <block class="" v-if="feedDataList.length>0">
          <view class="thing-card" v-for="(item, index) in feedDataList" :key="index">
            <view class="card_body">
              <view class="left">
                <u--image v-if="item.pic" @click="perviewFn(item.pic[0])" radius='10' :src="item.pic[0]" width="200rpx"
                  height="200rpx"></u--image>
              </view>
              <u--image src="/static/main/house/xia_yi_ji_btn.png" class="next" width="38rpx" height="38rpx" @click="navFeed(item, current)"></u--image>
              <view class="right" style="color: #7B7B7B;" @click="navFeed(item, current)">
                <view class="r-item">
                  <view class="item-title">操作类型:</view>
                  <view v-if="current==0">{{'喂食'}}</view>
                  <view v-else-if="current==1">{{'喂水'}}</view>
                  <view v-else-if="current==2">{{'消毒'}}</view>
                  <view v-else-if="current==3">{{'疫苗'}}</view>
                  <view v-else-if="current==4">{{'驱虫'}}</view>
                </view>
                <view class="r-item">
                  <view class="item-title">操作人员:</view>
                  <view>{{item.operator}}</view>
                </view>
                <view class="r-item">
                  <view class="item-title">操作日期:</view>
                  <view>{{item.create_time}}</view>
                </view>
                <view class="r-item" style="height: 80rpx;">
                  <view class="item-title">备注:</view>
                  <view class="row2">{{item.remark}}</view>
                </view>
              </view>
            </view>
          </view>
        </block>

        <view class="coneng-detail" v-else>
          <view class="">
            <image src="@/static/img/zw.png" mode="aspectFit"></image>
            <view class="">
              暂无数据
            </view>
          </view>
        </view>

      </view>

    </view>
  </view>
</template>

<script setup>
  import {
    ref,
    reactive
  } from "vue"

  import {
    onLoad,
    onShow,
    onReachBottom
  } from "@dcloudio/uni-app"
  import {
    dewormingLogLists,
    disinfectLogLists,
    feedingLogLists,
    waterLogLists,
    vaccinumLogLists
  } from "@/api/manage.js"
  
  
  const feedDataList = ref([])

  const current = ref(0);
  const list1 = reactive([{
    name: '喂食',
  }, {
    name: '喂水',
  }, {
    name: '消毒'
  }, {
    name: '疫苗'
  }, {
    name: '驱虫'
  }])

  //跳转
  const navTo = (url) => {
    uni.navigateTo({
      url
    })
  }
  const navFeed = (item, index) => {
    if(index==0) uni.navigateTo({
      url: `/pages/feedDetail/feedDetail`,
      success: (res) => {
        res.eventChannel.emit('setDatasItem', item);
      }
    })
    if(index==1) uni.navigateTo({
      url: `/pages/feedDetail/waterDetail`,
      success: (res) => {
        res.eventChannel.emit('setDatasItem', item);
      }
    })
    if(index==2) uni.navigateTo({
      url: `/pages/feedDetail/sterilizeDetail`,
      success: (res) => {
        res.eventChannel.emit('setDatasItem', item);
      }
    })
    if(index==3) uni.navigateTo({
      url: `/pages/feedDetail/vaccineDetail`,
      success: (res) => {
        res.eventChannel.emit('setDatasItem', item);
      }
    })
    if(index==4) uni.navigateTo({
      url: `/pages/feedDetail/expellingDetail`,
      success: (res) => {
        res.eventChannel.emit('setDatasItem', item);
      }
    })
  }


  const getNowTimeFn = () => {
    const now = new Date();
    const hour = now.getHours();
    const minute = now.getMinutes();
    const formattedHour = hour < 10 ? '0' + hour : hour;
    const formattedMinute = minute < 10 ? '0' + minute : minute;
    const currentTime = formattedHour + ':' + formattedMinute;
    return currentTime
  }
  //查看
  const perviewFn = (url) => {
    uni.previewImage({
      urls: [url]
    })
  }
  
  const where = ref({
    page_no: 1,
    page_size: 15,
    fence_house_id: '',
    animal_info_id: ''
  })
  const changeTabs = (e)=>{
    current.value = e.index;
    where.value.page_no = 1;
    feedDataList.value = [];
    if(e.name=='喂食') getList();
    if(e.name=='喂水') getList1();
    if(e.name=='消毒') getList2();
    if(e.name=='疫苗') getList3();
    if(e.name=='驱虫') getList4();
  }
  const getList = ()=>{
    feedingLogLists(where.value).then(res=>{
      res.data.lists = res.data.lists.map(item=>{
        item.pic = JSON.parse(item.pic||'[]');
        return item;
      })
      feedDataList.value = [...feedDataList.value, ...res.data.lists];
    })
  }
  const getList1 = ()=>{
    waterLogLists(where.value).then(res=>{
      res.data.lists = res.data.lists.map(item=>{
        item.pic = JSON.parse(item.pic||'[]');
        return item;
      })
      feedDataList.value = [...feedDataList.value, ...res.data.lists];
    })
  }
  const getList2 = ()=>{
    disinfectLogLists(where.value).then(res=>{
      res.data.lists = res.data.lists.map(item=>{
        item.pic = JSON.parse(item.pic||'[]');
        return item;
      })
      feedDataList.value = [...feedDataList.value, ...res.data.lists];
    })
  }
  const getList3 = ()=>{
    vaccinumLogLists(where.value).then(res=>{
      res.data.lists = res.data.lists.map(item=>{
        item.pic = JSON.parse(item.pic||'[]');
        return item;
      })
      feedDataList.value = [...feedDataList.value, ...res.data.lists];
    })
  }
  const getList4 = ()=>{
    dewormingLogLists(where.value).then(res=>{
      res.data.lists = res.data.lists.map(item=>{
        item.pic = JSON.parse(item.pic||'[]');
        return item;
      })
      feedDataList.value = [...feedDataList.value, ...res.data.lists];
    })
  }
  onLoad((options)=>{
    where.value.animal_info_id = options.id;
    where.value.fence_house_id = options.house_id;
    getList();
  })
  onReachBottom(()=>{
    where.value.page_no++;
    if(current.value==0) getList();
    if(current.value==1) getList1();
    if(current.value==2) getList2();
    if(current.value==3) getList3();
    if(current.value==4) getList4();
  })
</script>

<style lang="scss" scoped>
  page{
    background-color: $theme-bg-color;
  }
  .box {
    width: 750rpx;
    background-color: $theme-bg-color;
    padding: 0;

    .content {
      width: 693.93rpx;
      margin: auto;
      padding-top: 20rpx;

      .add-btn {
        display: flex;
        justify-content: space-between;
      }

      .add-thing {
        height: 90rpx;
        width: 335rpx;
        border-radius: 42.06rpx 42.06rpx 42.06rpx 42.06rpx;
        line-height: 90rpx;
        text-align: center;
        color: white;
        background-color: $theme-main-color;
      }
    }

    // padding: 20rpx;
    .time {
      display: flex;
      font-size: 26.29rpx;
      color: #737373;
      margin-top: 10rpx;
    }

    .video {
      // margin-top: 10rpx;
      border-radius: 14rpx;
      overflow: hidden;
    }
  }

  .coneng-detail {
    width: 478rpx;
    height: 341rpx;
    // background: #FFFFFF;
    border-radius: 6px 6px 6px 6px;
    opacity: 1;
    font-size: 25rpx;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    color: #737373;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    margin: 70rpx auto;

    image {
      width: 280rpx;
      height: 142rpx;
      margin-bottom: 20rpx;


    }
  }

  .thing-card {
    width: 693.93rpx;
    box-sizing: border-box;
    background-color: #fff;
    border-radius: 20rpx;
    overflow: hidden;
    margin-bottom: 30rpx;
    box-shadow: 1rpx 1rpx 10rpx 1rpx rgba(0, 0, 0, 0.1);

    .head {
      height: 90rpx;
      border-radius: 20rpx 20rpx 0 0;
      background-color: $theme-main-color;
      display: flex;
      justify-content: space-between;
      line-height: 90rpx;
      color: white;
      padding: 0 30rpx;
    }

    .card_body {
      padding: 20rpx;
      display: flex;
      position: relative;

      .next {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        right: 20rpx;
      }

      .left {
        flex-shrink: 0;
        position: relative;

        .qr-code {
          position: absolute;
          right: 10rpx;
          bottom: 10rpx;
        }
      }

      .right {
        flex: 1;
        display: flex;
        justify-content: space-between;
        flex-direction: column;
        margin-left: 20rpx;

        .name {
          font-weight: bold;
          color: #333;
        }

        .r-item {
          font-size: 28rpx;
          display: flex;

          .item-title {
            color: #333;
            flex-shrink: 0;
            margin-right: 10rpx;
          }

          .row2 {
            display: -webkit-box;
            -webkit-box-orient: vertical;
            overflow: hidden;
            -webkit-line-clamp: 2;
            /* 设置为需要显示的行数 */
          }
        }
      }
    }
  }

  .tit {
    position: relative;
    padding-left: 20rpx;
    font-weight: bold;
    margin-bottom: 20rpx;
  }

  .tit-more {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .more {
      display: flex;
      font-size: 28rpx;
      font-weight: 400;
    }
  }

  .tit::before {
    content: "";
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    left: 0;
    width: 3px;
    /* 左边框的宽度 */
    height: 30rpx;
    background-color: #FFB049;
  }

  .code-btn {
    border: 1px solid $theme-main-color;
    border-radius: 30rpx;
    padding: 3rpx 20rpx;
    color: $theme-main-color;
    position: absolute;
    right: 20rpx;
  }

  .actBtn {
    background: linear-gradient(to right, #FEFFFF 0%, #E8FAF2 100%);
  }
</style>