<template>
  <view>
    <cu-custom bgColor="bg-gradual-blueness" :isBack="true" @back="goBack">
      <block slot="backText">返回</block>
      <block slot="content">点检项列表</block>
    </cu-custom>
    <scroll-view scroll-y>
      <view class="spot-list">
        <view class="spot-item" v-for="item in list" :key="item.ID"
              @click="$noMultipleClicks(() => goToList(item.ID))">
          <view class="spot-img">
            <image v-if="item.isChecked" src="../../../static/zt-over.png" mode=""></image>
            <image v-else src="../../../static/zt-ing.png" mode=""></image>
          </view>
          <view class="spot-cont">
            <view class="text f30">
              检查项：{{ item.CHECK_ITEMS }}
            </view>
            <view class="text f30">
              检查标准：{{ item.CHECK_STANDARD }}
            </view>
            <view class="text f30">
              检查编码：{{ item.CHECK_CODE }}
            </view>
          </view>
        </view>
      </view>
      <view class="cu-tabbar-height"></view>
    </scroll-view>
    <view class="bottom-fixed" @click="$noMultipleClicks(goNext)">
      <button class="cu-btn bg-green">下一步</button>
    </view>
  </view>
</template>

<script>
import { basePath, loginSession } from '@/common/tool.js';

export default {
  data() {
    return {
      initflag: false,
      listId: '',
      list: [],
      dataFlag: 'noData',
      noClick: true, // 禁止多次点击状态
      equipType: '',
      CHECK_ID: '', // 接收清单点检信息列表项的ID
      RECORD_INFO_ID: '',
      ids: [],
      completedCheckItems: [] // 完成点检的项
    }
  },
  onLoad(e) {
    this.listId = e.listId;
    this.equipType = e.equipType;
    this.listName = e.listName;
    this.baoBaoType = e.baoBaoType;
    this.CHECK_ID = e.CHECK_ID;
    this.getData();
    loginSession();
  },
  onShow() {
    this.getData();
    if (this.initflag) {
      this.initflag = false;
      this.list = [];
    }
  },
  methods: {
    getData() {
      const _this = this;
      uni.showLoading({
        title: '请稍候'
      });
      uni.request({
        url: `${basePath}/app/recordItems/goEditEquiptype`,
        method: 'POST',
        dataType: 'json',
        header: {
          'Content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          equipType: _this.equipType,
          CHECK_ID: _this.CHECK_ID,
        },
        success: (res) => {
          if (res.data.result === "success") {
            uni.hideLoading();
            const dataNewCheckItems = res.data.dataNew.map(item => item.CHECK_ITEMS);
            _this.list = res.data.pd.map(item => {
              const isChecked = dataNewCheckItems.includes(item.CHECK_ITEMS);
              const dataNewItem = res.data.dataNew.find(d => d.CHECK_ITEMS === item.CHECK_ITEMS);
              const RecordInfoId = dataNewItem ? dataNewItem.ID : '';
              return { ...item, isChecked, RecordInfoId };
            });
            _this.CHECKRECORD_ID = res.data.CHECKRECORD_ID;
            _this.ids = _this.list.map(item => item.ID.toString());
          } else {
            uni.showToast({
              icon: 'none',
              title: '系统异常',
              duration: 2000
            });
          }
        }
      });
    },
    goBack() {
      uni.redirectTo({
        url: '/pages/application/recordItems/index'
      })
    },
    goToList(currentID) {
      const currentItem = this.list.find(item => item.ID === currentID);
      const RecordInfoId = currentItem ? currentItem.RecordInfoId : '';
      uni.navigateTo({
        url: `/pages/application/recordItems/check-items-list?CHECK_ID=${this.CHECK_ID}&currentID=${currentID}&RecordInfoId=${RecordInfoId}&IDS=${this.ids.join(',')}&equipType=${this.equipType}`
      });
    },
    goNext() {
      for (let i = 0; i < this.list.length; i++) {
        if (!this.list[i].isChecked) {
          uni.showToast({
            icon: 'none',
            title: '清单未全部检查,无法提交',
            duration: 3000
          });
          return;
        }
      }
      uni.navigateTo({
        url: `/pages/application/recordItems/check-items-next?listId=${this.listId}&CHECKRECORD_ID=${this.CHECKRECORD_ID ? this.CHECKRECORD_ID : ''}&CHECK_ID=${this.CHECK_ID}&IDS=${this.ids.join(',')}`
      });
    },
  }
}
</script>

<style>
.spot-cont {
  margin-left: 20upx;
  flex: 1;
}

.spot-cont .text {
  line-height: 1.4;
}

.spot-img {
  width: 100upx;
  height: 100upx;
}

.spot-img image {
  width: 100%;
  height: 100%;
}

.spot-list .spot-item {
  display: flex;
  align-items: center;
  background-color: #fff;
  border-radius: 10upx;
  margin: 20upx;
  padding: 20upx;
}
</style>
