<!--todo 页面逻辑重新过滤-->
<!-- 完善信息 -->
<template>

  <view>
    <view class="body" :style="{ bottom: footerHeight + 'px'}">
      <!-- 操作step -->
      <view class="u-p-30">
        <steps :list="steps" :current="current"></steps>
      </view>
      <!-- 身份选择 -->
      <view v-if="current === 0">
        <view class="u-p-30">
          <user-select :list="userSelect" @change="userSelectCallback"></user-select>
        </view>
      </view>
    </view>
    <!-- 底部按钮 -->
    <view class="footer" id="footer">
      <view class="footer-btn u-p-t-12 u-p-r-60 u-p-b-12 u-p-l-60">
<!--        :class="[assignType === -1 ? 'button&#45;&#45;submit&#45;&#45;disabled' : '' ]"-->
        <u-button type="primary" @click="next" shape="circle"
                  class="button--submit"
                  :custom-style=" assignType === -1 ? {background: '#b5b5b5 !important'} : {background: '#333 !important'}"
                  :disabled="assignType === -1"
                  v-if="current === 0">下一步
        </u-button>
      </view>
    </view>

    <u-toast ref="uToast" />
  </view>

</template>

<script>
import dom from "@/mixins/dom";
import goIndex from "./mixins/goIndex";
import UserSelect from "./components/UserSelect";
import Steps from "./components/Steps";

export default {
  name: "register",
  components: {Steps, UserSelect},
  mixins: [dom,goIndex],
  data(){
    return{
      current: 0,
      userSelect: [
        {
          title: "我是面料供应商",
          desc: "Fabric supplier",
          imgSrc: "/static/images/index/userSelect/type_1.png",
          activeImgSrc: "/static/images/index/userSelect/type_1_active.png",
        },
        {
          title: "我要选款",
          desc: "I want to choose",
          imgSrc: "/static/images/index/userSelect/type_2.png",
          activeImgSrc: "/static/images/index/userSelect/type_2_active.png",
        },
        {
          title: "我是设计机构",
          desc: "Design agency",
          imgSrc: "/static/images/index/userSelect/type_3.png",
          activeImgSrc: "/static/images/index/userSelect/type_3_active.png",
        },
        {
          title: "我是检测机构",
          desc: "Testing institutions",
          imgSrc: "/static/images/index/userSelect/type_4.png",
          activeImgSrc: "/static/images/index/userSelect/type_4_active.png",
        }
      ],
      assignType: -1, //身份类型: 1-我是面料供应商 2-我要选款 3-我是设计机构 4-我是检测机构
      steps: [
        {
          title: '第一步',
          desc: '选择身份'
        },
        {
          title: '第二步',
          desc: '完善信息'
        },
        {
          title: '第三步',
          desc: '完成'
        }
      ],

      status:{
        isBrandOwner: 0,   // 我要选款
        isDesignAgency: 0, // 完善设计机构信息
        isFabricSupplier: 0, //完善面料供应商信息
        isTestingFacility: 0, // 完善检测机构信息
      },
      comData:{
        pcBasicsId: '',          //PC端注册的企业id
        pcUserId: '',          //PC端注册的企业id
        corporateContactId: '',  //企业联系人id
        institutionAddressId: '',//企业/机构地址id
      },
    }
  },

  onLoad(options) {
    this.comData = {
      ...options,
      ...{
        pcUserId: uni.getStorageSync("pcUserId") || options.pcUserId,
        pcBasicsId: uni.getStorageSync("pcBasicsId") || options.pcBasicsId,
        tenantId: uni.getStorageSync("tenantId") || options.tenantId,
      }
    };
    uni.setStorageSync("pcUserId", this.comData.pcUserId)
    uni.setStorageSync("pcBasicsId",this.comData.pcBasicsId)
    uni.setStorageSync("tenantId",this.comData.tenantId)
    let {
      pcBasicsId,pcUserId
    } = this.comData;
    if(pcBasicsId && pcUserId){
      this.checkStatus();
    }
  },
  mounted() {
    this.uGetRect("#footer").then(res =>{
      this.footerHeight = res.height;
    })
  },
  methods:{
    uGetRect(selector, all) {
      return new Promise((resolve) => {
        uni.createSelectorQuery()
            .in(this)[all ? 'selectAll' : 'select'](selector)
            .boundingClientRect((rect) => {
              if (all && Array.isArray(rect) && rect.length) {
                resolve(rect)
              }
              if (!all && rect) {
                resolve(rect)
              }
            })
            .exec()
      })
    },
    userSelectCallback(type){
      this.assignType = type;
    },
    // 重新选择时 核对当前选中状态
    checkStatusByType(assignType){
      let{
        isBrandOwner,   // 我要选款
        isDesignAgency, // 完善设计机构信息
        isFabricSupplier, //完善面料供应商信息
        isTestingFacility, // 完善检测机构信息
      } = this.status;
      switch (assignType){
        case 1:
          if(isFabricSupplier){
            this.goIndex();
            return false;
          }else{
            return true
          }
        case 2:
          if(isBrandOwner){
            this.goIndex();
            return false;
          }else{
            return true
          }
        case 3:
          if(isDesignAgency){
            this.goIndex();
            return false;
          }else{
            return true
          }
        case 4:
          if(isTestingFacility){
            this.goIndex();
            return false;
          }else{
            return true
          }
      }
    },
    next(){
      if(this.checkStatusByType(this.assignType)){
        this.$Router.push({
          path: '/pages/user/register/perfectInformation',
          query:{
            ...this.comData,
            type: this.assignType
          }
        })
      }
    },
    checkStatus(){
      this.$http(
          "user.checkMagStatus",
          this.comData
      ).then(res =>{
        let { code ,data} = res;
        if(code === 200){
          // 1-面料供应商, 2-品牌商, 3-设计机构, 4-检测机构
          let { identityType} = data.userLastIdentity;
          if(identityType){
            this.assignType = identityType;
            this.status = data;
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
@import './css/index';
</style>
