<template>
  <h-view id="confirmList">
    <h-header :proportion="[5, 1, 1]" class="bar-custom">
      <div slot="left" class="h-header-btn">
        <img src="@/assets/userBind/arrow.png" @click="$routeGo()" />
        <span>合同签订</span>
      </div>
    </h-header>
    <Tab :title="text" @getTabNum="getTabNum" />
    <div class="search has-header">
      <input v-model="keyWord" placeholder="请输入合同号/产品名称/承租人名称" />
    </div>
    <scroll ref="scroll" :updateData="[showList]" :pullUp="true" @pullingUp="getList">
      <div v-for="(item,index) in showList" :key="index" class="item">
        <div class="left">
          <span @click="changePage(item)">
            <img src="@/assets/contractStart/icon.png" />
          </span>
          <div v-show="isRadio" class="check-box" @click.stop="clickRadio(item)">
            <div v-show="!item.checked"></div>
            <img v-show="item.checked" src="@/assets/contractCreate/selected.png" alt="已选择" />
          </div>
        </div>
        <div class="right" @click="changePage(item)">
          <div class="right-top">
            <span>合同号</span>
            <span>{{ item.project_number }}</span>
          </div>
          <div class="right-bottom">
            <ul>
              <li>
                {{ item.bp_type_n }}
                <span>{{ item.bp_name }}</span>
              </li>
              <li>
                产品名称
                <span>{{ item.division_n }}</span>
              </li>
              <li>
                合同金额
                <span class="number">{{ item.total_price | currency }}</span>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </scroll>
    <div class="check-bottom">
      <div class="choose" v-show="isRadio">
        <div class="radio" @click="allChoose(!chooseAll)">
          <img v-show="chooseAll" src="@/assets/login/radio.png" alt="radio" />
          <div class="no-check" v-show="!chooseAll"></div>
          <span :style="{ color: chooseAll ? '#1d3fff' : 'rgb(212,214,212)' }">全选</span>
        </div>
        <button class="cancel" @click="cancelRadio">× 取消批量</button>
        <button class="sign" @click="sign">签订电子合同</button>
      </div>

      <div class="button-box" v-show="!tabNum && !isRadio">
        <div class="go-radio" @click="isRadio = true">
          <img src="@/assets/login/write.png" alt="icon" />
          <span>批量签署</span>
        </div>
      </div>
    </div>
    <div v-if="showList.length === 0">
      <div class="display">
        <img src="@/assets/messageCenter/noMsg.png" alt />
      </div>
    </div>
  </h-view>
</template>
<script>
import Tab from '@/pages/carConfirm/tab'
export default {
  components: {
    Tab,
  },
  data() {
    return {
      text: { first: '待签订', second: '已签订' },
      tabNum: 0,
      keyWord: '',
      status: '',
      pagenumNew: 1,
      pagenumApproved: 1,
      lists: [],
      showList: [],
      undo: [],
      done: [],

      chooseAll: false,
      isRadio: false,
    }
  },
  watch: {
    tabNum: {
      handler(newVal, oldVal) {
        if (newVal === 0) {
          this.status = 'SUBMIT'
          this.showList = this.undo
        } else if (newVal === 1) {
          this.status = 'APPROVED'
          this.showList = this.done
        }
        this.getList()
      },
      immediate: true,
    },
    keyWord(newVal, oldVal) {
      this.search()
    },
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.pagenumNew = 1
      vm.pagenumApproved = 1
      vm.lists = []
    })
  },
  methods: {
    sign() {
      let list = this.showList.filter(item => item.checked).map(item => {item.checked = true; return item});
      if (!list.length) return hlsPopup.showLongCenter('请选择合同');
      window.localStorage.setItem('handleContact', JSON.stringify(list));
      this.$router.push({ path: '/batchSign', query: { name: 'confirmList' } });
    },
    clickRadio(item) {
      item.checked = !item.checked;

      this.chooseAll = this.showList.every(item => item.checked);
      this.$forceUpdate();
    },
    allChoose(status) {
      this.chooseAll = status;
      this.lists = this.lists.map(item => {
        item.checked = status;
        return item;
      })
    },
    cancelRadio() {
      this.isRadio = false;   // 关闭多选菜单
      this.allChoose(false);  // 关闭全选按钮
    },
    search() {
      let vm = this
      let randomString = Math.floor(Math.random() * 21)
      let url =
        process.env.basePath +
        'car_confirm_list_query' +
        '&index' +
        `'${randomString}'`
      let param = {
        // user_phone: window.localStorage.getItem('user_phone'),
        bp_id: JSON.parse(window.localStorage.now_user_bp_bind_id).bp_id,
        confirm_status: vm.status,
        searchInput: vm.keyWord,
      }
      vm.$post(url, param).then(function (res) {
        if (res.result === 'S') {
          vm.lists = res.lists
          vm.selectShowList()
        } else {
          hlsPopup.showLongCenter(res.message)
        }
      })
    },
    selectShowList() {
      let vm = this
      vm.undo = []
      vm.done = []
      this.lists.forEach(item => {
        if (item.confirm_status === 'SUBMIT') {
          vm.undo.push(item)
        } else if (item.confirm_status === 'APPROVED') {
          vm.done.push(item)
        }
      })
      if (vm.tabNum === 0) {
        this.undo = this.undo.map(item => {
          item.checked = false;
          return item;
        });
        this.showList = this.undo
      } else {
        this.showList = this.done
      }
    },
    getTabNum(i) {
      this.tabNum = i;
      this.isRadio = false;
      this.$refs.scroll.update(false)
      this.$refs.scroll.scrollToTop()
    },
    changePage(e) {
      this.$router.push({
        name: 'ConfirmDetail',
        query: {
          project_id: e.project_id,
          confirm_status: e.confirm_status,
          check_id: e.check_id,
          confirm_id: e.confirm_id,
          business_type: e.business_type,
        },
      })
    },
    getList() {
      let vm = this
      let randomString = Math.floor(Math.random() * 21)
      let url =
        process.env.basePath +
        'car_confirm_list_query' +
        '&index' +
        `'${randomString}'`
      let param = {
        // user_phone: window.localStorage.getItem('user_phone'),
        bp_id: JSON.parse(window.localStorage.now_user_bp_bind_id).bp_id,
        pagesize: 10,
        pagenum: vm.status === 'APPROVED' ? vm.pagenumApproved : vm.pagenumNew,
        confirm_status: vm.status,
        searchInput: vm.keyWord,
      }
      hlsPopup.showLoading('请稍候')
      vm.$post(url, param).then(function (res) {
        vm.hlsPopup.hideLoading()
        if (res.result === 'S') {
          let returnData = []
          returnData = res.lists
          if (returnData.length === 0) {
            vm.$refs.scroll.update(true)
          } else if (returnData.length > 0 && returnData.length < 10) {
            vum.forEach(returnData, function (data, index, array) {
              vm.lists.push(array[index])
            })
            vm.status === 'APPROVED' ? vm.pagenumApproved++ : vm.pagenumNew++
            vm.$refs.scroll.update(true)
          } else if (returnData.length === 10) {
            vum.forEach(returnData, function (data, index, array) {
              vm.lists.push(array[index])
            })
            vm.status === 'APPROVED' ? vm.pagenumApproved++ : vm.pagenumNew++
            vm.$refs.scroll.update(false)
          }
          vm.selectShowList()

          vm.allChoose(false)
        } else {
          hlsPopup.showLongCenter(res.message)
        }
      })
    },
  },
}
</script>
<style lang="less">
#confirmList {
  .display {
    position: fixed;
    width: 100%;
    height: 100%;
    // top: 0;
    img {
      width: 100%;
      height: 100%;
      // margin-top: -70px;
    }
  }
  .check-bottom {
    width: 100%;
    height: 1.2rem;
    // background-color: #fff;
    position: absolute;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;

    .button-box {
      width: 100%;
      height: 100%;
      background: #fff;

      display: flex;
      align-items: center;
      justify-content: center;
    }

    .go-radio {
      width: 90%;
      color: #fff;
      background-color: #1d3fff;
      padding: 8px 10px;
      border-radius: 5px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 0.32rem;
      img {
        width: 16px;
        margin-right: 4px;
      }
    }

    .choose {
      width: 100%;
      display: flex;
      background: #fff;
      justify-content: space-evenly;
      align-items: center;
      height: 100%;
      font-size: 0.3rem;
    }

    .radio {
      display: flex;
      align-items: center;
      color: #1d3fff;
      img {
        width: 24px;
      }
      .no-check {
        width: 17px;
        height: 17px;
        border-radius: 8.5px;
        border: 1px solid #edeef1;
        background-color: #fff;
        margin-right: 7.1px;
      }
    }
    .cancel {
      background-color: #ffcece;
      color: red;
      padding: 7px 15px;
      border-radius: 3px;
    }
    .sign {
      color: #fff;
      background-color: #1d3fff;
      padding: 7px 30px;
      border-radius: 3px;
    }
  }

  .search {
    background-color: #fff;
    padding: 8px 12px;
    position: absolute;
    width: 100%;
    height: 1.02rem;
    z-index: 100;
    margin-top: 52px;
    margin-bottom: 8px;
    input {
      background: url("../../assets/contractStart/search1.png") 320px no-repeat;
      background-size: 16px 16px;
      background-color: rgba(239, 239, 239, 0.55);
      padding-left: 12px;
      height: 36px;
      line-height: 36px;
      border-radius: 4px;
      width: 100%;
      font-family: PingFangSC-Regular;
      font-size: 14px;
      color: #888c8f;
    }

    input::placeholder {
      font-family: PingFangSC-Regular;
      font-size: 14px;
      color: #888c8f;
      letter-spacing: 0;
    }

    input:focus {
      background: url("../../assets/contractStart/search2.png") 320px no-repeat;
      background-size: 16px 16px;
      background-color: rgba(239, 239, 239, 0.55);
      border: 2px solid #bcc6ff;
    }
  }
  .item {
    width: 359px;
    height: 148px;
    background-color: #fff;
    border-radius: 2px;
    margin: 0 auto;
    margin-top: 9px;
    .left {
      display: flex;
      flex-direction: column;
      width: 15%;
      height: 100%;
      float: left;

      .check-box {
        width: 100%;
        height: 100px;
        display: flex;
        justify-content: center;
        align-items: center;

        div {
          width: 0.32rem;
          height: 0.32rem;
          border: 0.02rem solid #1d3fff;
          border-radius: 0.06rem;
        }

        img {
          width: 0.32rem;
        }
      }

      span {
        display: block;
        width: 30px;
        height: 30px;
        background-color: #e8e9ed;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-left: 10px;
        margin-top: 8px;
        img {
          width: 11px;
          height: 14px;
        }
      }
    }
    .right {
      height: 100%;
      width: 85%;
      float: right;
      .right-top {
        width: 285px;
        height: 45px;
        font-family: PingFangSC-Semibold;
        font-size: 15px;
        color: #4b4a4b;
        letter-spacing: 0;
        line-height: 45px;
        border-bottom: 1px solid #f3f3f7;
        span:last-child {
          float: right;
        }
      }
      .right-bottom {
        width: 285px;
        margin-top: 10px;
        .number {
          font-family: Verdana-Bold;
          font-size: 14px;
          color: #4b4a4b;
          letter-spacing: 0;
          font-weight: bold;
        }
        li {
          font-family: PingFangSC-Regular;
          font-size: 14px;
          color: #4b4a4b;
          letter-spacing: 0;
          height: 30px;
          width: 100%;
          span {
            float: right;
          }
        }
      }
    }
  }
  .content {
    position: absolute;
    top: 0;
  }
  .scrollContent {
    padding-top: 2.92rem;
    padding-bottom: 20px;
  }
  .tab-style {
    width: 100%;
    position: absolute;
  }
}
.platform-ios {
  #confirmList {
    .scrollContent {
      padding-top: 3.32rem;
      padding-bottom: 1.2rem;
    }
  }
}

@media (device-width: 375px) and (device-height: 812px) and (-webkit-min-device-pixel-ratio: 3) {
  .platform-ios {
    #confirmList {
      .scrollContent {
        padding-top: 3.72rem;
        padding-bottom: 1.2rem;
      }

      .check-bottom {
        bottom: 32px;
      }
    }
  }
}
// iPhoneXR适配
@media (device-width: 414px) and (device-height: 896px) {
  .platform-ios {
    #confirmList {
      .scrollContent {
        padding-top: 3.72rem;
        padding-bottom: 1.2rem;
      }
      .search {
        input {
          background: url("../../assets/contractStart/search1.png") 320px
            no-repeat;
          background-size: 16px 16px;
          background-color: rgba(239, 239, 239, 0.55);
          padding: 8px 12px;
          border-radius: 4px;
          width: 100%;
          font-family: PingFangSC-Regular;
          font-size: 14px;
          color: #888c8f;
        }
      }

      .check-bottom {
        bottom: 32px;
      }
    }
  }
}
</style>
