<template>
  <view class="container">
    <common-tabs :list="list" :current="current" @change="change" />
    <view class="p-top8">
      <template v-if="listData.length > 0">
        <view class="mx3 my2 p3 bg-color-white box p-relative" v-for="(item, index) in listData" :key="item.id" @click="handleDetail(item.id)" @longpress="showMask(item.id,$event)">
          <common-mask :item="item" :maskId="maskId" @hide="maskId = 0" @del="handleDel(item.id)" />
          <view class="d-flex j-between border-bottom pb-2 a-center">
            <view class="text-color-b f-size-26 text-overflow">{{ item.sbNo }} ({{ item.sbName }})</view>
            <my-badge :text="statusMap[item.status]" :status="dict[item.status]" />
          </view>
          <view class="mt-2 d-flex j-between">
            <view class="text-color-3 f-size-32 f-weight-bold">{{
                            item.requirement
                        }}</view>
          </view>
          <view class="f-size-26 text-color-6 f-weight-4 mt-2">{{
                        item.startTime
                    }} ({{ item.period + periodMap[item.periodType] }}) : {{ item.partName==null?'无':item.partName}}</view>
          <view class="d-flex j-end edit-box">
            <view class="text-color-3 f-size-32 edit" v-if="(item.status === 1||
                                item.status === 4) && !startFlag">
              <view @click.stop="handleExecute(item)">开始</view>
            </view>
            <view class="text-color-3 f-size-32 edit" v-if="(item.status === 1||
                                item.status === 2||
                                item.status === 4) && !startFlag">
              <view @click.stop="handleFinish(item)">执行</view>
            </view>
            <view class="text-color-3 f-size-32 edit" v-if="item.status === 1||
                                item.status === 2 ||
                                item.status === 4">
              <view @click.stop="submit(item)">完成</view>
            </view>
          </view>
        </view>
        <view class="py3">
          <u-loadmore :status="status" />
        </view>
      </template>
      <template v-else>
        <view class="empty">
          <u-empty text="暂无数据" mode="list"></u-empty>
        </view>
      </template>
    </view>
    <!-- <view class="p-fixed d-flex j-around a-center footer">
            <view
                class="main-color-bg text-color-white common"
                @tap="handleStart"
            >{{ nums }}</view
            >
        </view> -->
  </view>
</template>

<script>
import BaseTool from '../../utils/tool'

export default {
  data() {
    return {
      list: [],
      current: 1,
      sbId: '',
      nums: '开始计时',
      // 加载更多
      status: 'loadmore',
      page: 1,
      timer: null,
      limit: 10,
      type: 1,
      filter: -1,
      startFlag: false,
      loadMore: true,
      listData: [],
      periodMap: null,
      statusMap: null,
      sbStatusMap: null,
      dict: null,
      maskId: 0,
      searchType: null
    }
  },
  onPullDownRefresh() {
    // 请求结束取消刷新
    this.page = 1
    this.loadMore = true
    this.listData = []
    this.initData()
    uni.stopPullDownRefresh()
  },
  onReachBottom() {
    if (this.loadMore) {
      this.page++
      this.initData()
    }
  },
  onLoad(options) {
    this.sbId = options.sbId
    this.filter = options.filter
    this.searchType = options.searchType
    if (uni.getStorageSync('check-job-start')) {
      this.startFlag = true
      // 设置计时：
      var oldTime = uni.getStorageSync('check-job-start')
      // 设置计时：
      var hour, minute, second // 时 分 秒
      hour = minute = second = 0 // 初始化
      var millisecond = 0 // 毫秒
      var ms = Math.abs(new Date() - new Date(oldTime))
      // 两个日期相减得到毫秒数  1s=1000ms
      var s = ms / 1000 // 毫秒转为秒
      hour = Math.floor(s / 3600) // 小时
      minute = Math.floor((s % 3600) / 60) // 分
      second = Math.floor(s % 60) // 秒
      this.timer = setInterval(() => {
        millisecond = millisecond + 50
        if (millisecond >= 1000) {
          millisecond = 0
          second = second + 1
        }
        if (second >= 60) {
          second = 0
          minute = minute + 1
        }
        if (minute >= 60) {
          minute = 0
          hour = hour + 1
        }
        // console.log("-------"+hour+'时'+minute+'分'+second+'秒');
        this.nums = hour + '时' + minute + '分' + second + '秒'
      }, 50)
    }
    this.initData()
    this.periodMap = this.$DictCache.getLabelByValueMapByType(this.$DictCache.TYPE.CHECK_PLAN_PERIOD_TYPE)
    this.statusMap = this.$DictCache.getLabelByValueMapByType(this.$DictCache.TYPE.CHECK_JOB_STATUS)
    this.sbStatusMap = this.$DictCache.getLabelByValueMapByType(this.$DictCache.TYPE.CHECK_JOB_SB_STATUS)
    this.list = [{ label: '全部', value: '' }].concat(this.$DictCache.getChildrenList(this.$DictCache.TYPE.CHECK_JOB_STATUS))
    this.dict = this.$DictCache.COLOR.CHECK_JOB_STATUS
  },
  onHide() {
    this.maskId = 0
  },
  methods: {
    showMask(id, e) {
      this.maskId = id
      // #ifdef H5
      e.preventDefault()
      // #endif
    },
    handleDel(id) {
      const that = this
      const params = []
      params.push(id)
      uni.showModal({
        title: '提示',
        content: '您确认删除吗?',
        success: res => {
          if (res.confirm) {
            this.$Http.deleteCheckJobs(params).then(res => {
              uni.showToast({ title: '删除成功' })
              this.page = 1
              this.loadMore = true
              this.listData = []
              this.initData()
            })
          } else if (res.cancel) {
          }
        }
      })
    },
    // 详情
    handleDetail(id) {
      // if (index == 0) {
      // 	uni.navigateTo({
      // 		url: '../maintain-add/maintain-add'
      // 	});
      // } else {
      // 	uni.navigateTo({
      // 		url: '../maintain-detail/maintain-detail'
      // 	});
      // }
      if (this.maskId !== 0) {
        return (this.maskId = 0)
      }
      uni.navigateTo({
        url: '/pages/maintain-detail/maintain-detail-ignore?detailId=' + id
      })
    },
    handleExecute(item) {
      this.$Http.executeJobIgnores(item).then(res => {
        item.status = this.$DictCache.VALUE.CHECK_JOB_STATUS.EXECUTING
        uni.showToast({ title: '已开始，请及时完成任务' })
      })
    },
    handleFinish(item) {
      uni.navigateTo({
        url: '/pages/maintain-add/maintain-add?detailId=' + item.id
      })
    },
    // tab
    change(index, value) {
      this.current = index
      if (index === 0) {
        this.type = ''
      } else {
        this.type = value
      }
      this.page = 1
      this.loadMore = true
      this.listData = []
      this.initData()
    },
    handleStart() {
      clearInterval(this.timer)
      uni.showToast({
        title: '已开始计时'
      })
      this.startFlag = true
      uni.setStorageSync('check-job-start', this.$BaseTool.Date.formatter(new Date(), BaseTool.Date.PICKER_NORM_DATETIME_PATTERN))
      var hour, minute, second // 时 分 秒
      hour = minute = second = 0 // 初始化
      var millisecond = 0 // 毫秒
      this.timer = setInterval(() => {
        millisecond = millisecond + 50
        if (millisecond >= 1000) {
          millisecond = 0
          second = second + 1
        }
        if (second >= 60) {
          second = 0
          minute = minute + 1
        }
        if (minute >= 60) {
          minute = 0
          hour = hour + 1
        }
        // console.log("-------"+hour+'时'+minute+'分'+second+'秒');
        this.nums = hour + '时' + minute + '分' + second + '秒'
      }, 50)
    },
    // 提交
    submit(item) {
      item.actualStartTime = uni.getStorageSync('check-job-start')
      item.status = 3
      this.$Http.finishJobIgnores(item).then(res => {
        uni.showToast({
          title: '已完成'
        })
        this.initData()
      })
    },
    initData() {
      const that = this
      that.status = 'loading'
      this.$Http
        .getCheckJobPageIgnores({
          pageNum: this.page,
          pageSize: this.limit,
          type: 2,
          searchType: this.searchType,
          filter: this.filter,
          sbId: this.sbId,
          status: this.type
        })
        .then(res => {
          const data = res.data.rows
          if (data && data.length > 0 && data.length < that.limit) {
            that.status = 'nomore'
            that.loadMore = false
          } else if (data.length == that.limit) {
            that.status = 'loadmore'
          } else {
            that.status = 'nomore'
          }
          that.listData = [...that.listData, ...data]
        })
    }
  }
}
</script>

<style lang="scss">
page {
  background-color: #f4f4f4;
}

.box {
  padding-right: 0;
  border-radius: 20rpx;
}

.edit-box {
  padding: 0 30rpx;
  margin-top: 40rpx;

  .edit {
    padding: 8rpx 40rpx;
    border: 1px solid #ddd;
    border-radius: 20rpx;
    font-size: 26rpx;
    color: #666;
    margin-left: 30rpx;
  }
}

.text-overflow {
  width: 60%;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.container {
  padding-bottom: 100rpx;

  .footer {
    bottom: 0;
    left: 0;
    right: 0;
    background-color: #fff;
    z-index: 10000;
  }

  .title {
    border-bottom: 1rpx solid #f4f4f4;
  }

  .img {
    width: 100rpx;
    height: 100rpx;
  }

  .p-fixed {
    border-top: 1rpx solid #f4f4f4;
    height: 100rpx;

    .common {
      padding: 15rpx 200rpx;
      border-radius: 30rpx;
      background-color: #0082ff;
      color: #fff;
    }
  }
}
</style>
