<template>
  <view class="mainBox">
    <uni-nav-bar
      v-if="way != 'todo' && way != 'done'"
      fixed="true"
      statusBar="true"
      left-icon="back"
      :title="
        pageName == 'myList'
          ? '我的质检工单'
          : pageName == 'myInspectionProjectTask'
            ? '我的质检任务单'
            : pageName == 'myInspectionProjectEntrusted'
              ? '我的质检受托单'
              : '质检工单'
      "
      @clickLeft="back"
    >
    </uni-nav-bar>

    <view class="top-wrapper">
      <uni-section>
        <uni-easyinput
          prefixIcon="search"
          style="width: 460rpx"
          v-model="keyWord"
          placeholder="名称、批次号、工单编码"
        >
        </uni-easyinput>
      </uni-section>
      <button class="search_btn" @click="doSearch">搜索</button>
    </view>
    <div style="height: 100rpx; width: 475rpx"></div>
    <view class="wrapper">
      <u-list @scrolltolower="scrolltolower" class="listContent">
        <view
          v-for="(item, index) in tableList"
          :key="index + item.id"
          style="position: relative"
        >
          <view class="item">
            <view class="herder_item">
              <view class="herder_text"></view>
              <view class="herder_view">
                {{ getDictValue("质检计划类型", item.qualityType + "") }}/{{
                  item.code
                }}/{{ item.productCode }}/{{ item.productName }}
                <text v-if="!pageName || pageName == 'myList'">
                  ({{
                    item.isPendingSample == 1
                      ? "待收样"
                      : statusList.find((row) => row.value == item.status)
                          .label
                  }})
                </text>
                <text v-else>
                  ({{
                    statusList1.find((row) => row.value == item.status).label
                  }})
                </text>
                <text>
                  ({{ item.recordingMethod == 1 ? "按质检项检" : "按样品检" }})
                </text>
                <text v-if="item.productLineName">
                  {{ item.productLineName }}
                </text>
              </view>
            </view>
            <view class="text">检验项目：</view>
            <view class="item_value" v-if="item.recordingMethod == 1">
              <view
                @click="
                  goTo(item.id, row.id, item.sampleMeasureUnit, item, row)
                "
                v-for="(row, _index) in item.templateList"
                :key="item.inspectionName + index + '_' + _index"
                :style="{
                  background:
                    row.status == 1
                      ? '#19be6b'
                      : row.status == 2
                        ? '#ff9900'
                        : row.status == 3
                          ? '#ff9900'
                          : '#909399',
                }"
              >
                {{ row.inspectionName }}
              </view>
            </view>
            <view class="item_value" v-else>
              <view
                @click="
                  goTo(item.id, row.id, item.sampleMeasureUnit, item, row)
                "
                v-for="(row, _index) in item.qualitySampleList"
                :key="item.sampleCode + index + '_' + _index"
                :style="{
                  background:
                    row.status == 1
                      ? '#19be6b'
                      : row.status == 2
                        ? '#ff9900'
                        : row.status == 3
                          ? '#ff9900'
                          : '#909399',
                }"
              >
                {{ row.sampleCode }}
              </view>
            </view>
            <view
              class="text btnlist"
              v-if="
                ![1, 2].includes(item.status) &&
                (pageName == 'myList' || !pageName)
              "
            >
              操作：
              <view style="display: flex">
                <u-button
                  v-if="item.isPendingSample == 1"
                  type="primary"
                  text="收样"
                  @click="sampleCollection(item)"
                ></u-button>
                <u-button
                  style="margin-left: 5px"
                  v-if="
                    item.sampleQuantity < item.total &&
                    item.isPendingSample != 1 &&
                    (pageName == 'myList' || !pageName)
                  "
                  type="primary"
                  text="请样"
                  @click="addSampleOpen(item)"
                ></u-button>
                <u-button
                  style="margin-left: 5px"
                  v-if="item.recordingMethod != 1"
                  type="primary"
                  text="批量质检"
                  @click="goTo(item.id, '', item.sampleMeasureUnit, item, {})"
                >
                </u-button>
              </view>
            </view>
           
            <!-- <view
              class="text btnlist"
              v-if="
                ![2].includes(item.status) && pageName && pageName != 'myList'
              "
            >
              操作：
              <view style="display: flex">
                <u-button
                  style="margin-left: 5px"
                  v-if="item.recordingMethod != 1"
                  type="primary"
                  text="批量质检"
                  @click="goTo(item.id, '', item.sampleMeasureUnit, item, {})"
                >
                </u-button>
              </view>
            </view> -->
          </view>
        </view>
        <view style="width: 100%; height: 40rpx"></view>
        <view style="margin-top: 20vh" v-if="tableList.length == 0">
          <u-empty iconSize="150" textSize="32" text="暂无数据"> </u-empty>
        </view>
      </u-list>
    </view>

    <u-toast ref="uToast"></u-toast>
    <addSample ref="addSampleRef" @reload="doSearch"></addSample>
  </view>
</template>

<script>
import dictMixns from "@/mixins/dictMixins";
import addSample from "./addSample.vue";
import {
  getList,
  getRequestentrustList,
  sampleCollection,
  getTaskmonadList,
  verificationQualityInspector,
  checkByQualityWorkOrderId,
} from "@/api/inspectionWork/index.js";
export default {
  components: {
    addSample,
  },
  mixins: [dictMixns],
  props: {
    pageName: {
      default: "",
    },
    way: {
      default: "route",
    },
    produceFilter: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      statusList: [
        {
          value: 0,
          label: "未报工",
        },
        {
          value: 1,
          label: "已报工",
        },
        {
          value: 2,
          label: "已关闭",
        },
        {
          value: 3,
          label: "待请样",
        },
      ],
      statusList1: [
        {
          value: 1,
          label: "未报工",
        },
        {
          value: 2,
          label: "已报工",
        },
      ],
      tableList: [],
      page: 1,
      size: 10,
      isEnd: false,
      keyWord: "",
      userInfo: {},
    };
  },
  computed: {},
  created() {
    this.requestDict("质检计划类型");
  },
  onLoad() {},

  methods: {
    buildQueryData(baseData = {}) {
      const data = {
        ...baseData,
      };
      if (this.produceFilter && this.produceFilter.produceSourceTaskId) {
        data.produceSourceTaskId = this.produceFilter.produceSourceTaskId;
      }
      if (this.produceFilter && this.produceFilter.workOrderCode) {
        data.workOrderCodeList = [this.produceFilter.workOrderCode];
      }
      return data;
    },
    successInit() {
      uni.showLoading({
        title: "加载中",
      });

      let data = this.buildQueryData({
        pageNum: 1,
        size: this.tableList.length,
        keyWord: this.keyWord,
        // recordingMethod: 1,
      });
      if (this.pageName) {
        data.currentLoginUserId = this.userInfo.userId;
      }
      if (this.pageName == "myInspectionProjectEntrusted") {
        data.approvalStatus = 2;
      }
      if (this.way == "todo") {
        data.status = 0;
      }
      if (this.way == "done") {
        data.status = 1;
      }
      let api =
        this.pageName == "myInspectionProjectTask"
          ? getTaskmonadList
          : this.pageName == "myInspectionProjectEntrusted"
            ? getRequestentrustList
            : getList;
      api(data)
        .then((res) => {
          this.tableList = res.list;
        })
        .then(() => {
          uni.hideLoading();
        });
    },
    doSearch() {
      this.isEnd = false;
      this.page = 1;
      this.getList();
    },
    //获取列表信息
    getList() {
      this.userInfo = uni.getStorageSync("userInfo");
      if (this.isEnd || !this.userInfo.userId) {
        return;
      }
      uni.showLoading({
        title: "加载中",
      });

      let data = this.buildQueryData({
        pageNum: this.page,
        size: this.size,
        keyWord: this.keyWord,
        // recordingMethod: 1,
      });
      if (this.pageName) {
        data.currentLoginUserId = this.userInfo.userId;
      }
      if (this.pageName == "myInspectionProjectEntrusted") {
        data.approvalStatus = 2;
      }
      if (this.way == "todo") {
        data.status = 0;
      }
      if (this.way == "done") {
        data.status = 1;
      }
      let api =
        this.pageName == "myInspectionProjectTask"
          ? getTaskmonadList
          : this.pageName == "myInspectionProjectEntrusted"
            ? getRequestentrustList
            : getList;

      api(data)
        .then((res) => {
          if (this.page === 1) {
            this.tableList = res.list;
          } else {
            this.tableList.push(...res.list);
          }
          this.page += 1;
          this.isEnd = this.tableList.length >= res.count;
        })
        .then(() => {
          uni.hideLoading();
        });
    },
    async sampleCollection(row) {
      if (!this.pageName) {
        const code = await verificationQualityInspector(row.id);
        if (code == "-1") {
          return;
        }
      }
      sampleCollection({
        id: row.id,
      }).then((res) => {
        this.$refs.uToast.show({
          type: "success",
          icon: false,
          message: "收样成功",
        });
        this.doSearch();
      });
    },
    goTo(workId, projectId, sampleMeasureUnit, item, row) {
      if (this.pageName == "myList" || !this.pageName) {
        if (item.isPendingSample == 1 && row.status != 1) {
          this.$refs.uToast.show({
            type: "error",
            icon: false,
            message: "请先完成收样！",
          });
          return;
        }
        if (item.status == 3) {
          this.$refs.uToast.show({
            type: "error",
            icon: false,
            message: "请先完成请样！",
          });
          return;
        }
      }
      if (item.recordingMethod == 1) {
        uni.navigateTo({
          url:
            "/pages/qms/inspectionWork/inspectionProjectReport?workId=" +
            workId +
            "&projectId=" +
            projectId +
            "&pageName=" +
            this.pageName +
            "&sampleMeasureUnit=" +
            sampleMeasureUnit,
        });
      } else {
        uni.navigateTo({
          url:
            "/pages/qms/inspectionWork/sampleList?workId=" +
            workId +
            "&projectId=" +
            projectId +
            "&pageName=" +
            this.pageName,
        });
      }
    },
    async addSampleOpen(row) {
      const code = await verificationQualityInspector(row.id);
      if (code == "-1") {
        return;
      }
      const is = await checkByQualityWorkOrderId(row.id);
      if (is) {
        this.$refs.uToast.show({
          type: "error",
          icon: false,
          message: "此工单存在未处理完的请样记录，请检查！",
        });
        return;
      }
      this.$refs.addSampleRef.open("add", row);
    },
    scrolltolower() {
      if (this.isEnd) {
        return;
      }
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.mainBox {
  background-color: #f3f8fb;
}

// .wrapper{

// }
.top-wrapper {
  background-color: #fff;
  display: flex;
  width: 750rpx;
  height: 88rpx;
  padding: 16rpx 32rpx;
  align-items: center;
  // gap: 16rpx;
  position: absolute;
  z-index: 999;
  // top: 44px;
  // // #ifdef APP-PLUS
  // top: 140rpx;

  // // #endif
  /deep/.uni-section {
    margin-top: 0px;
  }

  /deep/.uni-section-header {
    padding: 0px;
  }

  .search_btn {
    width: 120rpx;
    height: 70rpx;
    line-height: 70rpx;
    padding: 0 24rpx;
    background: $theme-color;
    font-size: 32rpx;
    color: #fff;
    margin: 0;
    margin-left: 26rpx;
  }

  .menu_icon {
    width: 44rpx;
    height: 44rpx;
    margin-left: 14rpx;
  }
}

.item {
  width: 720rpx;
  // height:400rpx;
  margin: 20rpx auto 0;
  padding: 26rpx;
  border-radius: 30rpx;

  .text {
    margin-top: 20rpx;
  }

  background: #fff;
}

.item_value {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  margin-left: -10rpx;

  > view {
    font-size: 24rpx;
    color: #fff;
    padding: 12rpx;
    border-radius: 10rpx;
    background: #979797;
    margin-left: 10rpx;
    margin-top: 20rpx;
  }
}

.herder_item {
  display: flex;
  // align-items: center;
  font-size: 28rpx;
  font-weight: bold;

  .herder_view {
    width: calc(100% - 20rpx);
    word-break: break-all;
  }

  .herder_text {
    min-width: 10rpx;
    height: 32rpx;
    border-radius: 10rpx;
    background: #00c0a1;
    margin-right: 12rpx;
    margin-top: 5rpx;
  }
}

.btnlist {
  display: flex;
  align-items: center;
}

/deep/.u-button {
  height: 60rpx;
  width: auto;
}
</style>
