<template>
  <view>
    <base-popup
      :title="'关联项目'"
      ref="basePopup"
      @handleConfirmClick="handlerProjectConfirm"
      @handleCloseClick="resetDialog"
    >
      <view style="padding: 20rpx 30rpx 0 30rpx">
        <u-search
          v-if="showSearch"
          :placeholder="placeholder"
          v-model="CodeOrName"
          shape="square"
          clearabled
          :showAction="false"
          @change="contractSearch"
        ></u-search>
        <scroll-view
          :scroll-top="scrollTop"
          scroll-y="true"
          class="scroll-Y"
          @scrolltolower="scrolltolower"
          :lower-threshold="0"
          style="margin-top: 20rpx"
          v-if="!isLoading"
        >
          <!--单选-->
          <u-radio-group
            v-if="type == 'radio'"
            :borderBottom="true"
            iconPlacement="right"
            placement="column"
            @change="groupChange"
            v-model="radioValue"
          >
            <u-radio
              :customStyle="{ marginBottom: '12px' }"
              v-for="(item, index) in dataLists"
              :key="index"
              :name="item.projectID"
            >
              <view class="listData">
                <view class="content">
                  <text>项目名称：</text>
                  {{ item.projectName }}
                </view>
                <view class="flex time">
                  <view>
                    <text>项目任务：</text>
                    {{ item.projectTotalCount }}
                  </view>
                  <view>
                    <text>当前BUG：</text>
                    {{ item.bugTotalCount || 0 }}
                  </view>
                  <view>
                    <text>项目经理：</text>
                    {{ item.pmPersonName }}
                  </view>
                  <view>
                    <text>业务员：</text>
                    {{ item.salesName }}
                  </view>
                </view>
              </view>
            </u-radio>
          </u-radio-group>

          <!--多选-->
          <u-checkbox-group
            v-if="type == 'checkbox'"
            :borderBottom="true"
            placement="column"
            iconPlacement="right"
            @change="checkboxChange"
            v-model="checkboxValue"
          >
            <u-checkbox
              :customStyle="{ marginBottom: '12px', paddingBottom: '12px' }"
              v-for="(item, index) in dataLists"
              :key="index"
              :name="item.projectID"
              :label="item.cusName + ' ' + item.projectID"
            >
            </u-checkbox>
          </u-checkbox-group>
          <view class="contractNoData" v-show="noData">
            <u-divider text="我是有底线哒！" :hairline="true"></u-divider>
          </view>
        </scroll-view>
        <u-loadmore
          v-else
          status="loading"
          :loadingText="'数据加载中'"
          :marginTop="150"
        ></u-loadmore>
      </view>
    </base-popup>
  </view>
</template>

<script>
import { GetProjectListData } from "@/api/system/projectAdd.js";
import basePopup from "@/components/basePopup/index.vue";
export default {
  components: {
    basePopup,
  },
  props: {
    // 是否显示搜索
    showSearch: {
      default: true,
      type: Boolean,
    },
    placeholder: {
      default: "请输入项目名称",
      type: String,
    },
    type: {
      default: "checkbox",
      type: String,
    },
  },
  data() {
    return {
      noData: false,
      CodeOrName: "",
      scrollTop: 0,
      pages: {
        page: 1, //当前页码
        limit: 20, //每页显示多少
        CodeOrName: "",
      },
      dataLists: [],
      checkboxData: [],
      checkboxValue: [],
      radioData: "",
      radioValue: "",
      timer: null,
      isLoading: false,
    };
  },
  methods: {
    openDialog(radioValue = "") {
      this.GetProjectListData();
      setTimeout(() => {
        this.$nextTick(() => {
          this.radioValue = radioValue;
          if (radioValue) {
            this.groupChange(radioValue);
          }
          this.$refs.basePopup.isShowPopup = true;
        });
      }, 100);
    },
    // 搜索
    contractSearch(value) {
      if (this.timer !== null) clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.pages.CodeOrName = value;
        this.GetProjectListData();
        this.radioValue = "";
        this.checkboxValue = Object.assign([], []);
      }, 800);
    },
    resetDialog() {
      this.CodeOrName = "";
      this.pages.page = 1;
      this.pages.CodeOrName = "";
      this.radioValue = "";
      this.radioData = "";
      this.checkboxValue = Object.assign([], []);
    },
    checkboxChange(n) {
      this.checkboxData = [];
      n.forEach((key) => {
        this.dataLists.forEach((item) => {
          if (item.projectID == key) {
            this.checkboxData.push(item);
          }
        });
      });
    },
    //选择列表项触发
    groupChange(n) {
      this.radioData = this.dataLists.filter((item) => item.projectID === n);
    },
    // 滑动到底部触发，用于下拉加载新数据
    scrolltolower() {
      if (this.noData != true) {
        this.pages.page++;
        this.GetProjectListData();
      }
    },
    // 获取客户弹窗--列表
    async GetProjectListData() {
      this.isLoading = true;
      this.noData = false;
      let params = {
        ...this.pages,
      };
      const res = await GetProjectListData(params);
      this.isLoading = false;
      if (this.pages.page > 1) {
        this.dataLists.push(...res.data[1]);
      } else {
        this.dataLists = res.data[1];
      }
      if (res.data[1].length < 20) {
        this.noData = true;
      }
    },
    // 确认按钮点击事件
    handlerProjectConfirm() {
      if (this.type == "radio") {
        if (
          this.radioData == "" ||
          this.radioData == undefined ||
          this.radioData == null
        ) {
          uni.$u.toast("请选择数据");
          return;
        }
        this.$emit("handlerProjectConfirm", this.radioData);
        this.$refs.basePopup.isShowPopup = false;
        this.resetDialog();
      } else if (this.type == "checkbox") {
        if (this.checkboxData.length == 0) {
          uni.$u.toast("请选择数据");
          return;
        }
        this.$emit("handlerProjectConfirm", this.checkboxData);
        this.$refs.basePopup.isShowPopup = false;
        this.resetDialog();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.scroll-Y {
  height: 680rpx;
}
.contractNoData .u-divider {
  margin: 0;
}
.content {
  line-height: 1.5;
  font-size: 28rpx;
  margin-bottom: 10rpx;

  text {
    font-size: 28rpx;
    color: #979797;
  }
}
.time {
  font-size: 28rpx;
  flex-wrap: wrap;
  line-height: 1.5;

  text {
    display: inline-block;
    font-size: 26rpx;
    color: #979797;
    margin-bottom: 30rpx;
  }

  view {
    height: 48rpx;
  }
  view:nth-child(2n + 1) {
    width: 50%;
  }

  view:nth-child(2n) {
    width: 50%;
  }
  view:nth-child(4) {
    text:nth-child(2) {
      font-size: 28rpx;
    }
  }
}
</style>