<template>
  <view class="blacklog-container">
    <uni-nav-bar
      fixed="true"
      statusBar="true"
      left-icon="back"
      title="我的待办"
      @clickLeft="back"
    ></uni-nav-bar>
    <view class="list-container">
      <view v-for="(item, index) in list" :key="index">
        <CardTime :time="item.createTime" />
        <!-- <KdCard :title="getTitle(item)" :type="getType(item)" :item="item" @handleDetail="handleDetail" /> -->
		
        <KdCard
          :title="item.title"
          :type="getType(item)"
          :item="item"
          @handleDetail="handleDetail"
        />
      </view>
    </view>
    <view v-show="list.length === 0" class="no_data">
      <u-empty mode="data" textSize="30"></u-empty>
    </view>
  </view>
</template>

<script>
import { get } from '@/utils/api.js'
import KdCard from '../components/KdCard/index.vue'
import CardTime from '../components/CardTime.vue'
let [page, size, isEnd] = [1, 10, true]

const config = {
  1: {
    //巡点检
    name: 'patrol'
  },
  2: {
    //保养
    name: 'maintenance'
  },
  3: {
    //维修
    name: 'repair'
  },
  4: {
    //盘点
    name: 'check'
  },
  5: {
    //出库
    name: 'OUT_WARE'
  },
  6: {
    //入库
    name: 'IN_WARE'
  },
  7: {
    //"库存调拨
    name: 'TRANSFER'
  },
  8: {
    //报损报溢
    name: 'REPORT_LOSS_OVERFLOW'
  }
}

export default {
  components: {
    KdCard,
    CardTime
  },
  data () {
    return {
      list: [],
      params: {
        handleStatus: 0,
        keywords: '',
        handleType: ''
      }
    }
  },
  //加载更多
  onReachBottom: function () {
    console.log('asdsad')
    if (isEnd) {
      return
    }

    // 显示加载图标
    uni.showLoading({
      title: '数据加载中'
    })
    this.getMoreLists()
  },
  onShow () {
    page = 1
    this.list = []
    this.getList()
  },

  methods: {
    scrolltolower () {},
    getTitle ({ devOpsType, planType, title }) {
      let str =
        {
          1: '巡点检',
          2: '保养',
          3: '维修',
          4: '盘点',
          5: '出库',
          6: '入库',
          7: '库存调拨',
          8: '报损报溢'
        }[planType?.code] || title

      str += ['计划审批', '工单验收'][devOpsType] || ''

      return str
    },
    getType ({ devOpsType, planType }) {
      let type = `backlog_${config[planType?.code]?.name}`
      if (devOpsType === 1) {
        return type
      }
      return type + 'Plan'
    },
    handleDetail ({
      devOpsType,
      planType,
      workOrderId,
      id,
      workOrderCode,
      handleType
    }) {
      let url = ''
      // 入库
      if (planType.code == 5) {
        url = `/pages/home/backlog/approve_out?type=approval&workOrderId=${workOrderId}&id=${id}&planType=${planType?.code}`
      } else if (planType.code == 6) {
        url = `/pages/home/backlog/approve_enter?type=approval&workOrderId=${workOrderId}&id=${id}&planType=${planType?.code}`
      } else if (planType.code == 7) {
        url = `/pages/home/backlog/approve_allot?type=approval&workOrderId=${workOrderId}&id=${id}&planType=${planType?.code}`
      } else if (planType.code == 8) {
        url = `/pages/home/backlog/approve_approve_allot?type=approval&workOrderId=${workOrderId}&id=${id}&planType=${planType?.code}&handleType=${handleType}`
      } else if (planType.code == 9) {
        url = `/pages/home/backlog/sparepartDetail?type=approval&workOrderCode=${workOrderCode}&id=${id}&workOrderId=${workOrderId}`
      } else if (planType.code == 10) {
        url = `/pages/home/backlog/planNatureApproval?type=approval&workOrderCode=${workOrderCode}&id=${id}&workOrderId=${workOrderId}&handleType=${handleType}&planType=${planType?.code}`
      }
	  
	  
      // 0 审批 1 验收
      else if (handleType) {
        // 工单验收
        /* PATROL(1, "巡点检"),
						MAINTAIN(2, "保养"),
						REPAIR(3, "维修"),
						INVENTORY(4, "盘点"),
						OUT_WARE(5, "出库"),
						IN_WARE(6, "入库"),
						TRANSFER(7, "库存调拨"),
						REPORT_LOSS_OVERFLOW(8,"报损报溢"); */
        switch (planType.code) {
          case 1:
            url = `/pages/tour_tally/detail/detail?type=approval&id=${workOrderId}&myHandleId=${id}`
            break
          case 2:
            break
          case 3: {
            let par = this.URLSearchParams({
              type: 'approval',
              myHandleId: id,
              workOrderCode: workOrderCode
            })
            url = `/pages/maintain_service/detail/detail?` + par
            break
          }
          case 4:
            url = `/pages/warehouse/workOrder/details/details?workOrderId=${workOrderId}&myHandleId=${id}`
            break
          default:
            break
        }
      } else {
        // 0 计划 1 工单
        if (devOpsType === 0) {
          url = `/pages/home/backlog/planApproval?type=approval&workOrderId=${workOrderId}&id=${id}&planType=${planType?.code}`
        }
      }

      uni.navigateTo({
        url
      })
    },
    /* 
			 handleType 	办理类型(0 审批 1 验收)  
			 devOpsType	运维管理类型(0 计划 1 工单)
			 planType.code	计划类型(1:巡点检;2:保养;3:维修;4:盘点;5:出入库) 
			*/
    getList () {
      let paging = {
        page,
        size
      }
      let par = Object.assign(paging, this.params)
      get(this.apiUrl + '/myHandle/getMyHandleList', par, true).then(res => {
        if (res?.success) {
          if (this.params.page === 1) {
            this.list = []
          }
          this.list.push(
            ...res.data.records.map(item => {
              let matchList =
                item.messageContent &&
                item.messageContent.match(/【[\S]+\s([\S\s]+)】/)
              if (matchList) {
                item.ruleName = matchList[1]
              }

              return item
            })
          )
          let pageTotal = res.data.pages

          page < pageTotal ? (isEnd = false) : (isEnd = true)
          console.log(isEnd)
        }
      })
    },
    getMoreLists: function () {
      //获取更多数据
      page++
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.blacklog-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.list-container {
  flex: 1;
  padding: 24rpx;
  background: $page-bg;
  // position: absolute;
  // top: 88rpx;
  // bottom: 0;
  // left: 0;
  // right: 0;

  /deep/.u-list {
    height: 100% !important;
  }
}

.no_data {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #999;
  font-size: 50rpx;
}
</style>
