<template>
  <div class="apply-info">
    <div class="apply-info-title">
      <p>最新求购信息</p>
      <span>海量求购，一网打尽</span>
      <div>
        <input type="text" class="form-control" v-model="keyWord" @keyup.13="searchList" placeholder="请输入要查找的内容" />
        <span @click="searchList">查询</span>
      </div>
    </div>
    <div class="apply-info-list">
      <p>
        <span>发布时间<img src="/images/applyPurchase/sort-arrow.png" alt=""></span>
        <span>买家</span>
        <span>型号</span>
        <span>品牌</span>
        <span>截止时间<img src="/images/applyPurchase/sort-arrow.png" alt=""></span>
        <span>已报价<img src="/images/applyPurchase/sort-arrow.png" alt=""></span>
        <span>操作</span>
      </p>
      <ul>
        <li v-for="purchaseMan in purchaseManList.content" :class="{'active': purchaseMan.active}">
          <span>{{purchaseMan.releaseDate | date}}</span>
          <span>{{purchaseMan.userName}}</span>
          <span>{{purchaseMan.code}}</span>
          <span>{{purchaseMan.brand}}</span>
          <span class="date-content"><span>剩余&nbsp;</span><span v-text="getDay(purchaseMan.remainingTime)"></span>&nbsp;天&nbsp;<span v-text="getHours(purchaseMan.remainingTime)"></span>&nbsp;小时</span>
          <span class="number-content"><img src="/images/applyPurchase/hot-fire.png" alt="" v-if="purchaseMan.offerAmount > 10"><span :style="purchaseMan.offerAmount > 10 ? 'color: #ff9a00': ''">{{purchaseMan.offerAmount || 0}}</span>&nbsp;条</span>
          <span class="btn-content">
            <a>联系买家</a>
            <a @click="sayPrice(purchaseMan)">我要报价</a>
          </span>
          <div class="expand-content">
            <div>
              生产日期：{{purchaseMan.produceDate || '-'}}
            </div>
            <div>
              封装：{{purchaseMan.encapsulation || '-'}}
            </div>
            <div>
              采购数量：<span>{{purchaseMan.amount || '-'}}</span><span v-if="purchaseMan.amount">个</span>
            </div>
            <div>
              单价预算：<span>{{purchaseMan.unitPrice ? (purchaseMan.currency == 'RMB' ? '￥' : '$') + purchaseMan.unitPrice : '-'}}</span>
            </div>
          </div>
          <div class="say-price">
            <div>报价</div>
            <div>
              <i>*</i>单价
             <!-- <select v-model="sayPriceObj.currency">
                <option value="RMB">￥</option>
                <option value="USD">$</option>
              </select>-->
              <div v-text="purchaseMan.currency == 'RMB' ? '￥' : '$'"></div>
              <input type="text" v-model="sayPriceObj.unitPrice" class="form-control">
            </div>
            <div>
              <i>*</i>交期&nbsp;
              <input type="number" v-model="sayPriceObj.minDay" @blur="onMinDayInput" class="form-control">&nbsp;-&nbsp;<input type="number" v-model="sayPriceObj.maxDay" @blur="onMaxDayInput" class="form-control">&nbsp;天
            </div>
            <div>
              生产日期&nbsp;<input v-model="sayPriceObj.produceDate" type="text" class="form-control">
            </div>
            <div>
              <span @click="cancelSayPrice(purchaseMan)">取消</span>
              <span @click="commitSayPrice(purchaseMan)">提交</span>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <page :total="totalCount" :page-size="pageSize"
          :current="nowPage" v-on:childEvent="listenPage"></page>
  </div>
</template>
<script>
  import Page from '~components/common/page/pageComponent.vue'
  export default {
    data () {
      return {
        pageSize: 10,
        nowPage: 1,
        sayPriceObj: {
          currency: 'RMB',
          unitPrice: '',
          minDay: '',
          maxDay: '',
          produceDate: '',
          spId: ''
        },
        keyWord: ''
      }
    },
    components: {
      Page
    },
    filters: {
      date: function (date) {
        const d = new Date(Number(date))
        const year = d.getFullYear()
        const monthTemp = d.getMonth() + 1
        const month = monthTemp < 10 ? '0' + monthTemp : '' + monthTemp
        const hour = d.getHours() < 10 ? '0' + d.getHours() : '' + d.getHours()
        const minutes = d.getMinutes() < 10 ? '0' + d.getMinutes() : '' + d.getMinutes() + ' '
        const day = d.getDate() < 10 ? '0' + d.getDate() : '' + d.getDate() + ' '
        return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes
      },
      phone: function (str) {
        return str.substring(0, 3) + '****' + str.substring(7, 11)
      }
    },
    computed: {
      purchaseManList () {
        return this.$store.state.applyPurchase.purchaseManList.purchaseManList.data
      },
      totalCount () {
        return this.purchaseManList.totalElements
      }
    },
    methods: {
      getDay: function (timeStamp) {
        return Math.floor(timeStamp / (1000 * 60 * 60 * 24))
      },
      getHours: function (timeStamp) {
        return Math.floor((timeStamp / (1000 * 60 * 60)) % 24)
      },
      listenPage: function (page) {
        this.nowPage = page
        this.resetList()
      },
      sayPrice: function (purchaseMan) {
        purchaseMan.active = true
      },
      cancelSayPrice: function (purchaseMan) {
        purchaseMan.active = false
      },
      commitSayPrice: function (purchaseMan) {
        let valid = this.sayPriceObj.currency && this.sayPriceObj.unitPrice && this.sayPriceObj.minDay && this.sayPriceObj.maxDay
        if (valid) {
          this.sayPriceObj.spId = purchaseMan.id
          this.sayPriceObj.currency = purchaseMan.currency
          this.$http.post('/seek/offer/saveOffer', this.sayPriceObj).then(response => {
            this.$message.success('发布成功')
            this.nowPage = 1
            this.resetSayPrice()
            this.resetList()
          }, error => {
            console.log(error)
            this.$message.error('系统错误')
          })
        } else {
          this.$message.error('请填写正确的信息')
        }
      },
      resetList: function () {
        this.$store.dispatch('applyPurchase/loadPurchaseManList', {page: this.nowPage, count: this.pageSize, keyWord: this.keyWord})
      },
      resetSayPrice: function () {
        this.sayPriceObj = {
          currency: 'RMB',
          unitPrice: '',
          minDay: '',
          maxDay: '',
          produceDate: '',
          spId: ''
        }
      },
      onMinDayInput: function () {
        this.sayPriceObj.minDay = Math.floor(this.sayPriceObj.minDay)
        if (this.sayPriceObj.maxDay) {
          if (this.sayPriceObj.maxDay < this.sayPriceObj.minDay) {
            this.sayPriceObj.minDay = this.sayPriceObj.maxDay
          }
        } else if (this.sayPriceObj.minDay < 0) {
          this.sayPriceObj.minDay = 0
        }
      },
      onMaxDayInput: function () {
        this.sayPriceObj.maxDay = Math.floor(this.sayPriceObj.maxDay)
        if (this.sayPriceObj.minDay) {
          if (this.sayPriceObj.maxDay < this.sayPriceObj.minDay) {
            this.sayPriceObj.maxDay = this.sayPriceObj.minDay
          }
        } else if (this.sayPriceObj.maxDay < 0) {
          this.sayPriceObj.maxDay = 0
        }
      },
      searchList: function () {
        this.resetList()
      }
    }
  }
</script>
<style lang="scss" scoped>
  .apply-info {
    width: 1190px;
    margin: 0 auto;
    .apply-info-title {
      border-bottom: 1px solid #3975f4;
      >p {
        display: inline-block;
        width: 156px;
        height: 33px;
        line-height: 33px;
        color: #fff;
        background: #3975f4;
        font-size: 18px;
        text-align: center;
        border: {
          top-left-radius: 3px;
          top-right-radius: 3px;
        }
        margin: 0;
      }
      >span {
        color: #999;
        margin-left: 16px;
      }
      >div {
        float: right;
        height: 25px;
        line-height: 25px;
        >input {
          width: 241px;
          height: 25px;
          line-height: 25px;
          font-size: 13px;
          border: {
            top-right-radius: 0;
            bottom-right-radius: 0;
          }
        }
        >span {
          display: inline-block;
          width: 69px;
          color: #fff;
          background: #3975f4;
          text-align: center;
          margin: 0 6px 0 -1px;
          border: {
            top-right-radius: 3px;
            bottom-right-radius: 3px;
          }
          cursor: pointer;
        }
        >a {
          background: #ffa200;
          color: #fff;
          width: 79px;
          border-radius: 3px;
          text-align: center;
          display: inline-block;
        }
      }
    }
    .apply-info-list {
      >p {
        background: #ededed;
        height: 40px;
        line-height: 40px;
        margin: 9px 0 0 0;
        span {
          display: inline-block;
          text-align: center;
          img {
            width: 15px;
            height: 14px;
            margin:  0 0 3px 5px;
          }
          &:nth-child(1) {
            width: 186px;
          }
          &:nth-child(2) {
            width: 160px;
          }
          &:nth-child(3) {
            width: 173px;
          }
          &:nth-child(4) {
            width: 147px;
          }
          &:nth-child(5) {
            width: 152px;
          }
          &:nth-child(6) {
            width: 158px;
          }
          &:nth-child(7) {
            width: 214px;
          }
        }
      }
      >ul {
        margin-bottom: 29px;
        li {
          min-height: 67px;
          line-height: 67px;
          border: 1px solid #ededed;
          >span {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            display: inline-block;
            text-align: center;
            color: #3c3c3c;
            vertical-align: middle;
            &:nth-child(1) {
              width: 186px;
            }
            &:nth-child(2) {
              width: 160px;
            }
            &:nth-child(3) {
              width: 173px;
            }
            &:nth-child(4) {
              width: 147px;
            }
            &:nth-child(5) {
              width: 152px;
            }
            &:nth-child(6) {
              width: 158px;
            }
            &:nth-child(7) {
              width: 212px;
            }
            &.date-content {
              span {
                &:first-child {
                  font-size: 12px;
                  color: #666;
                }
                color: #f71026;
              }
            }
            &.number-content {
              span {
                color: #5392f9;
                &.active {
                  color: #ff9a00;
                }
              }
              >img {
                margin-bottom: 5px;
                margin-right: 2px;
              }
            }
            &.btn-content {
              a {
                display: inline-block;
                width: 71px;
                height: 28px;
                line-height: 28px;
                color: #fff;
                background: #3c7cf5;
                border-radius: 3px;
                cursor: pointer;
                &:first-child {
                  background: #ffa200;
                  margin-right: 10px;
                }
              }
            }
          }
          .expand-content {
            display: none;
            justify-content: space-between;
            padding: 0 236px 0 36px;
            background: #fffbf0;
            box-shadow: 1.5px 3px 7px 0 rgb( 178, 178, 179 );
            div {
              display: inline-block;
              span {
                color: #f71026;
              }
            }
          }
          &:hover {
            .expand-content {
              display: flex;
            }
          }
          .say-price {
            background-color: #e7eef9;
            display: none;
            >div {
              display: inline-block;
              position: relative;
              height: 64px;
              line-height: 64px;
              &:nth-child(1) {
                width: 282px;
                background: url('/images/applyPurchase/say-price.png')no-repeat;
                text-align: center;
                margin-right: 44px;
              }
              &:nth-child(2) {
                margin-right: 39px;
                input {
                  width: 118px;
                  height: 26px;
                  border: 1px solid #5392f9;
                }
                /*select {
                  position: absolute;
                  top: 19px;
                  width: 32px;
                  height: 26px;
                  background: url('/images/applyPurchase/arrow-down.png')no-repeat right center;
                  -webkit-box-shadow: none;
                  -moz-box-shadow: none;
                  box-shadow: none;
                  border: {
                    left: none;
                    top: none;
                    bottom: none;
                    right: 1px solid #5392f9;
                    bottom-left-radius: 4px;
                    top-left-radius: 4px;
                  }
                  color: #5392f9;
                  font: small-caption;
                  padding-left: 3px;
                  & + input {
                    padding-left: 34px;
                  }
                }*/
                div {
                  position: absolute;
                  top: 19px;
                  left: 38px;
                  width: 32px;
                  height: 26px;
                  line-height: 26px;
                  text-align: center;
                  border: {
                    left: none;
                    top: none;
                    bottom: none;
                    right: 1px solid #5392f9;
                    bottom-left-radius: 4px;
                    top-left-radius: 4px;
                  }
                  color: #5392f9;
                  & + input {
                    padding-left: 34px;
                  }
                }
              }
              &:nth-child(3) {
                margin-right: 37px;
                input {
                  width: 32px;
                  height: 26px;
                }
              }
              &:nth-child(4) {
                margin-right: 132px;
                input {
                  width: 118px;
                  height: 26px;
                }
              }
              &:nth-child(5) {
                span {
                  width: 71px;
                  height: 28px;
                  line-height: 28px;
                  text-align: center;
                  display: inline-block;
                  cursor: pointer;
                  &:first-child {
                    background: #dedddd;
                    margin-right: 10px;
                  }
                  &:last-child {
                    background: #fa4701;
                    color: #fff;
                  }
                }
              }
              i {
                color: #e41515;
                position: relative;
                top: 2px;
                right: 3px;
              }
            }
          }
          &.active {
            border-color: #f71026;
            .expand-content {
              display: flex;
              -webkit-box-shadow: none;
              -moz-box-shadow: none;
              box-shadow: none;
            }
            .say-price {
              display: block;
            }
          }
        }
      }
    }
    .page-wrap {
      text-align: right;
      float: none;
    }
  }
</style>
