<template>
  <h-view id="contract-signing">
    <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>
    <div class="tab-style has-header">
      <div :class="{ 'done': num === 1, 'undone': num !== 1 }" @click="num = 1; isRadio = false">
        <img :src="num === 1 ? check1 : uncheck1" />
        <span>待确认</span>
      </div>
      <div :class="{ 'done': num === 2, 'undone': num !== 2 }" @click="num = 2; isRadio = false">
        <img :src="num === 2 ? check2 : uncheck2" />
        <span>已确认</span>
      </div>
    </div>
    <!-- 搜索 -->
    <div class="search">
      <input v-model="searchInput" type="text" placeholder="请输入合同号/承租人名称" />
    </div>

    <scroll ref="scroll" :updateData="[showLists]" :pullUp="true" @pullingUp="loadMore">
      <div class="wrap">
        <div v-for="(item,index) in showLists" :key="index" class="contract-lists">
          <div class="pro-code" @click="goDetails(item)">
            <img src="@/assets/distributorSign/fileIcon.png" alt class="file-icon" />
            <p>
              <span>合同号</span>
              <span>{{ item.project_number }}</span>
            </p>
            <img v-if="item.read_check === 'N'" src="@/assets/constractSigning/unread.png" alt class="unread" />
            <img v-if="item.read_check === 'Y'" src="@/assets/constractSigning/read.png" alt class="unread" />
            <img src="@/assets/constractSigning/righticon.png" alt class="arrow" />
          </div>
          <div class="box">
            <div
              v-show="isRadio"
              style=" flex: 0 1 50px"
              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
              class="text"
              style="flex: 0 1 calc(100vw - 55px)"
              :style="{ marginLeft: !isRadio ? '50px' : '0' }"
            >
              <div>
                <p>承租人</p>
                <p>{{ item.bp_name }}</p>
              </div>
              <div>
                <p>合同名称</p>
                <p>{{ item.project_name }}</p>
              </div>
              <div>
                <p>合同租金</p>
                <p class="money">{{ item.total_price | currency }}</p>
              </div>
              <div>
                <p>产品名称</p>
                <p>{{ item.division_n }}</p>
              </div>
              <div>
                <p>申请时间</p>
                <p>{{ dateConverse(item.confirm_start_date) }}</p>
              </div>
            </div>
          </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="signContract">签订征信授权书</button>
      </div>

      <div class="button-box" v-show="num == 1 && !isRadio">
        <div class="go-radio" @click="isRadio = true">
          <img src="@/assets/login/write.png" alt="icon" />
          <span>批量签署</span>
        </div>
      </div>
    </div>
    <div v-if="showLists.length === 0">
      <div class="display">
        <img src="@/assets/messageCenter/noMsg.png" alt />
      </div>
    </div>
  </h-view>
</template>

<script>
import Check1 from '@/assets/constractSigning/no-done.png'
import Check2 from '@/assets/constractSigning/yes-done.png'
import unCheck1 from '@/assets/constractSigning/no-undone.png'
import unCheck2 from '@/assets/constractSigning/yes-undone.png'

export default {
  data() {
    return {
      num: 1,
      pagenum: 1,
      submitPagenum: 1,
      approvedPagenum: 1,
      searchInput: '',
      check1: Check1,
      check2: Check2,
      uncheck1: unCheck1,
      uncheck2: unCheck2,
      user_bp_type: '', // 客户类型
      submitLists: [], // 待签约
      approvedLists: [], // 已签约
      showLists: [],
      lists: [],
      approvedFlag: false,

      chooseAll: false,
      isRadio: false,
    }
  },
  watch: {
    'num': {
      handler(newVal, oldVal) {
        // this.$refs.scroll.update(false)
        if (newVal === 2) {
          if (!this.approvedFlag) {
            this.contractList()
          }
          this.$refs.scroll.scrollToTop()
          this.showLists = this.approvedLists
        } else if (newVal === 1) {
          this.showLists = this.submitLists
          this.$refs.scroll.scrollToTop()
        }
      },
    },
    searchInput() {
      let vm = this
      if (vm.timeout) {
        clearTimeout(vm.timeout)
      }
      vm.timeout = setTimeout(() => {
        vm.search()
      }, 1000)
    },
  },
  activated() {

  },
  created() {

  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (from.name === 'MyInfo' || from.name === 'MyContract') {
        vm.num = 1
        vm.pagenum = 1
        vm.submitPagenum = 1
        vm.approvedPagenum = 1
        vm.searchInput = ''
        vm.approvedFlag = false
        vm.submitLists = []
        vm.approvedLists = []
        vm.user_bp_type = vm.$route.params.user_bp_type
        vm.$refs.scroll.update(false)
        vm.$refs.scroll.scrollToTop()
      } else if (from.name === 'ContractDetail') {
        if (window.localStorage.backFlag === 'true') {
          vm.searchInput = ''
          vm.submitPagenum = 1
          vm.approvedPagenum = 1
          vm.approvedFlag = false
        }
      }
      vm.contractList()
    })
  },
  methods: {
    signContract() {
      let list = this.showLists.filter(item => item.checked).map(item => {item.checked = false; return item});
      if (!list.length) return hlsPopup.showLongCenter('请选择合同');
      if(list.findIndex(item => item.read_check === 'N') > -1) return hlsPopup.showLongCenter('选中合同中有文本未读，请先阅读');
      window.localStorage.setItem('handleContact', JSON.stringify(list));
      this.$router.push({ path: '/batchSign', query: { name: 'contract' } });
    },
    clickRadio(item) {
      item.checked = !item.checked;

      this.chooseAll = this.showLists.every(item => item.checked);
      this.$forceUpdate();
    },
    allChoose(status) {
      this.chooseAll = status;
      this.showLists = this.showLists.map(item => {
        item.checked = status;
        return item;
      });
    },
    cancelRadio() {
      this.isRadio = false;   // 关闭多选菜单
      this.allChoose(false);  // 关闭全选按钮
    },
    dateConverse(date) {
      return date.replace(/\//g, '-')
    },
    // 合同查询
    contractList() {
      let vm = this
      if (vm.num === 1) {
        let url = $config.basePath + 'prj_confirm_list_query'
        let param = {
          // user_phone: window.localStorage.user_phone,
          bp_id: JSON.parse(window.localStorage.now_user_bp_bind_id).bp_id,
          pagesize: 10,
          pagenum: vm.submitPagenum,
          confirm_status: 'SUBMIT',
          searchInput: vm.searchInput,
        }
        vm.hlsPopup.showLoading('数据加载中')
        vm.hlsHttp.post(url, param).then(function (res) {
          vm.hlsPopup.hideLoading()
          if (res.result === 'S') {
            vm.submitLists = res.lists.map(item => {
              item.checked = false;
              return item;
            })
            vm.showLists = vm.submitLists
            if (res.lists.length > 0 && res.lists.length < 10) {
              vm.$refs.scroll.update(true)
            } else if (res.lists.length === 10) {
              vm.$refs.scroll.update(false)
            }
          } else {
            hlsPopup.showLongCenter(res.message)
          }
        })
      } else if (vm.num === 2) {
        let url = $config.basePath + 'prj_confirm_list_query'
        let param = {
          pagesize: 10,
          pagenum: vm.approvedPagenum,
          searchInput: vm.searchInput,
          confirm_status: 'APPROVED',
          // user_phone: window.localStorage.user_phone,
          bp_id: JSON.parse(window.localStorage.now_user_bp_bind_id).bp_id,
        }
        vm.hlsPopup.showLoading('数据加载中')
        vm.hlsHttp.post(url, param).then(function (res) {
          vm.hlsPopup.hideLoading()
          if (res.result === 'S') {
            console.log(res.lists)
            vm.approvedLists = res.lists.map(item => {
              item.checked = false;
              return item;
            })
            vm.approvedFlag = true
            vm.showLists = vm.approvedLists
            if (res.lists.length > 0 && res.lists.length < 10) {
              vm.$refs.scroll.update(true)
            } else if (res.lists.length === 10) {
              vm.$refs.scroll.update(false)
            }
          } else {
            hlsPopup.showLongCenter(res.message)
          }
        })
      }
    },

    loadMore() {
      let vm = this
      if (vm.num === 1) {
        vm.submitPagenum++
        let url = $config.basePath + 'prj_confirm_list_query'
        let param = {
          // user_phone: window.localStorage.user_phone,
          bp_id: JSON.parse(window.localStorage.now_user_bp_bind_id).bp_id,
          pagesize: 10,
          pagenum: vm.submitPagenum,
          confirm_status: 'SUBMIT',
          searchInput: vm.searchInput,
        }
        vm.hlsPopup.showLoading('请稍候')
        vm.hlsHttp.post(url, param).then(function (res) {
          vm.hlsPopup.hideLoading()
          let returnData = []
          if (res.result === 'S') {
            returnData = res.lists
            if (returnData.length === 0) {
              vm.$refs.scroll.update(true)
            } else if (returnData.length > 0 && returnData.length < 10) {
              returnData.forEach((data, index, array) => {
                vm.submitLists.push(array[index])
              })
              vm.$refs.scroll.update(true)
            } else if (returnData.length === 10) {
              returnData.forEach((data, index, array) => {
                vm.submitLists.push(array[index])
              })
              vm.$refs.scroll.update(false)
            }
            vm.showLists = vm.submitLists

            vm.allChoose(false)
          } else {
            hlsPopup.showLongCenter(res.message)
          }
        })
      } else if (vm.num === 2) {
        vm.approvedPagenum++
        let url = $config.basePath + 'prj_confirm_list_query'
        let param = {
          // user_phone: window.localStorage.user_phone,
          bp_id: JSON.parse(window.localStorage.now_user_bp_bind_id).bp_id,
          pagesize: 10,
          pagenum: vm.approvedPagenum,
          confirm_status: 'APPROVED',
          searchInput: vm.searchInput,
        }
        vm.hlsPopup.showLoading('请稍候')
        vm.hlsHttp.post(url, param).then(function (res) {
          vm.hlsPopup.hideLoading()
          let returnData = []
          if (res.result === 'S') {
            returnData = res.lists
            if (returnData.length === 0) {
              vm.$refs.scroll.update(true)
            } else if (returnData.length > 0 && returnData.length < 10) {
              returnData.forEach((data, index, array) => {
                vm.approvedLists.push(array[index])
              })
              vm.$refs.scroll.update(true)
            } else if (returnData.length === 10) {
              returnData.forEach((data, index, array) => {
                vm.approvedLists.push(array[index])
              })
              vm.$refs.scroll.update(false)
            }
            vm.showLists = vm.approvedLists
          } else {
            hlsPopup.showLongCenter(res.message)
          }
        })
      }
    },
    async submitSearch() {
      let vm = this
      vm.submitPagenum = 1
      let url = $config.basePath + 'prj_confirm_list_query'
      let param = {
        // user_phone: window.localStorage.user_phone,
        bp_id: JSON.parse(window.localStorage.now_user_bp_bind_id).bp_id,
        pagesize: 10,
        pagenum: vm.submitPagenum,
        confirm_status: 'SUBMIT',
        searchInput: vm.searchInput,
      }
      vm.hlsPopup.showLoading('请稍候')
      let res = await vm.$post(url, param)
      if (res.result === 'S') {
        vm.hlsPopup.hideLoading()
        vm.submitLists = res.lists.map(item => {
          item.checked = false;
          return item;
        })
        if (vm.num === 1) {
          vm.showLists = vm.submitLists
        }
        // vm.showLists = vm.submitLists
        if (res.lists.length >= 0 && res.lists.length < 10) {
          vm.$refs.scroll.update(true)
        } else if (res.lists.length === 10) {
          vm.$refs.scroll.update(false)
        }
        return true
      } else {
        hlsPopup.showLongCenter(res.message)
        return false
      }
    },
    approvedearch() {
      let vm = this
      let url = $config.basePath + 'prj_confirm_list_query'
      vm.approvedPagenum = 1
      let param = {
        // user_phone: window.localStorage.user_phone,
        bp_id: JSON.parse(window.localStorage.now_user_bp_bind_id).bp_id,
        pagesize: 10,
        pagenum: vm.approvedPagenum,
        confirm_status: 'APPROVED',
        searchInput: vm.searchInput,
      }
      vm.hlsHttp.post(url, param).then(function (res) {
        vm.approvedLists = res.lists.map(item => {
          item.checked = false;
          return item;
        })
        if (vm.num === 2) {
          vm.showLists = vm.approvedLists
        }
        // vm.showLists = vm.approvedLists
        if (res.lists.length >= 0 && res.lists.length < 10) {
          vm.$refs.scroll.update(true)
        } else if (res.lists.length === 10) {
          vm.$refs.scroll.update(false)
        }
      })
    },
    async search() {
      let res = await this.submitSearch()
      if (res) {
        this.approvedearch()
      }
    },

    goDetails(item) {
      this.$router.push({
        name: 'ContractDetail',
        params: {
          item: item,
          user_bp_type: this.user_bp_type,
          num: this.num,
          check_id: item.check_id,
        },
      })
    },
  },
}
</script>

<style lang="less" type="text/less">
#contract-signing {
  .unread {
    width: 34px;
    height: 20px;
    margin: 0 20px;
  }
  .display {
    width: 100%;
    height: 100%;
    position: fixed;
    // top: 0;
    img {
      width: 100%;
      height: 100%;
      // margin-top: -70px;
    }
  }
  .tab-style {
    display: flex;
    width: 100%;
    justify-content: center;
    padding-top: 12px;
    padding-bottom: 8px;
    align-items: center;
    background-color: #fff;
    box-shadow: 0 2px 3px 0 rgba(220, 220, 221, 0.5);
    position: absolute;
    z-index: 999;

    div {
      position: relative;
      height: 32px;
      width: 172px;
      border-radius: 16px;
      border-radius: 16px;
      font-family: PingFangSC-Semibold;
      font-size: 14px;
      letter-spacing: 0.43px;
      margin: 0 4px;
      line-height: 32px;
    }
    .undone {
      background: rgba(220, 220, 221, 0.26);
      color: #383f45;
    }
    .done {
      background: rgba(0, 70, 156, 0.1);
      color: #00469c;
    }

    img {
      height: 20px;
      display: block;
      float: left;
      margin: 6px 0 0 43px;
    }

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

    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;
    }
  }

  .wrap {
    width: 100%;
    padding: 8px;
    .contract-lists {
      width: 100%;
      background: #fff;
      margin-bottom: 8px;
      border-radius: 2px;
      .pro-code {
        height: 44px;
        width: 100%;
        display: flex;
        flex-direction: row;
        align-items: center;

        .file-icon {
          width: 30px;
          height: 30px;
          margin-left: 10px;
        }
        p {
          height: 44px;
          line-height: 44px;
          margin-left: 10px;
          flex: 1;
          font-family: PingFangSC-Semibold;
          font-size: 15px;
          color: #4b4a4b;
          letter-spacing: 0;
          border-bottom: 1px solid #f3f3f7;
        }
        .arrow {
          height: 16px;
          width: 16px;
          margin-right: 10px;
          margin-left: -8px;
        }
      }
      .box {
        // margin-left: 50px;
        // margin-right: 19px;
        display: flex;

        .check-box {
          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;
          }
        }

        .text {
          margin-right: 19px;
          div {
            padding: 8px 0;
            line-height: 18px;
            display: flex;
            justify-content: space-between;
            position: relative;
            .money {
              font-family: Verdana-Bold;
              font-size: 14px;
              color: #4b4a4b;
              letter-spacing: 0;
              font-weight: 600;
            }
            p {
              word-break: break-all;
              word-wrap: break-word;
              white-space: normal;
              font-family: PingFangSC-Regular;
              font-size: 14px;
              letter-spacing: 0;
            }
            p:nth-of-type(1) {
              width: 32%;
              left: 0;
              color: rgba(75, 74, 75, 0.6);
            }
            p:nth-of-type(2) {
              right: 0;
              color: #4b4a4b;
            }
          }
        }
      }
    }
  }
  .content {
    position: absolute;
    top: 0;
  }
  .scrollContent {
    padding-top: 3rem;
    padding-bottom: 20px;
  }
}
.platform-ios {
  #contract-signing {
    .scrollContent {
      padding-top: 3.4rem;
      padding-bottom: 1.2rem;
    }
  }
}
// iPhoneX适配
@media (device-width: 375px) and (device-height: 812px) and (-webkit-min-device-pixel-ratio: 3) {
  .platform-ios {
    #contract-signing {
      .scrollContent {
        padding-top: 3.8rem;
        padding-bottom: 1.2rem;
      }

      .check-bottom {
        bottom: 32px;
      }
    }
  }
}
// iPhoneXR适配
@media (device-width: 414px) and (device-height: 896px) {
  .platform-ios {
    #contract-signing {
      .scrollContent {
        padding-top: 3.8rem;
        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>
