<template>
  <div class="publish-apply">
    <div class="good-purchaser">
      <p class="good-purchaser-title">
        <img src="/images/applyPurchase/good-purchaser-title.png" alt="">
      </p>
      <ul>
        <li v-for="(goodMan, index) in goodPurchaseMan.content">
          <i v-text="index + 1" :style="'background: url(/images/applyPurchase/level-' + getRankBg(index)  + '.png) center no-repeat;'"></i>
          <span v-text="goodMan.name"></span>
        </li>
      </ul>
    </div>
    <div class="publish-area">
      <div class="publish-form-area">
        <p>单个发布</p>
        <div>
          <div class="form-item">
            <span>
              <i>*</i>型号：
            </span>
            <input type="text" class="form-control" :class="{'error': !validObj.code}" v-model="applyObj.code" maxlength="30" @blur="checkCode" />
          </div>
          <div class="form-item">
            <span>
              <i>*</i>品牌：
            </span>
            <input type="text" class="form-control" :class="{'error': !validObj.brand}" maxlength="20" v-model="applyObj.brand" @blur="checkBrand" />
          </div>
          <div class="form-item">
            <span>
              单价预算：
            </span>
            <select v-model="applyObj.currency">
              <option value="RMB">￥</option>
              <option value="USD">$</option>
            </select>
            <input type="number" class="form-control" :class="{'error': !validObj.unitPrice}" v-model="applyObj.unitPrice" @blur="checkUnitPrice" @input="onUnitPriceInput" />
          </div>
          <div class="form-item">
           <span>
              封装：
            </span>
            <input type="text" class="form-control" maxlength="20" v-model="applyObj.encapsulation" />
          </div>
          <div class="form-item">
            <span>
              生产日期：
            </span>
            <input type="text" class="form-control" v-model="applyObj.produceDate" />
          </div>
          <div class="form-item">
            <span>
              采购数量：
            </span>
            <input type="number" class="form-control" :class="{'error': !validObj.amount}" v-model="applyObj.amount" @blur="checkAmount" @input="onAmountInput" />
          </div>
          <div class="form-item">
            <span>
              <i>*</i>截止日期：
            </span>
            <!--<input type="text" class="form-control" readonly :class="{'error': !validObj.deadline}" v-model="applyObj.deadline" @blur="checkDeadline" />-->
            <el-date-picker
              v-model="applyObj.deadline"
              type="date"
              :picker-options="pickerOptions"
              :class="{'error': !validObj.deadline}"
              size="mini">
            </el-date-picker>
          </div>
        </div>
        <a @click="goPublish()">发布求购</a>
      </div>
      <div class="publish-upload">
        <h1>批量发布</h1>
        <h2>3秒一键配单采购</h2>
        <img src="/images/applyPurchase/upload.png" alt="" />
        <h3>把Excel格式的BOM拖放到框中</h3>
        <img class="download-line" src="/images/applyPurchase/download.png" alt="">
        <img class="apply-logo" src="/images/applyPurchase/publish-apply.png" alt="">
      </div>
    </div>
    <div class="apply-rank">
      <table>
        <thead>
          <tr>
            <th width="62">排名</th>
            <th width="124">求购型号</th>
            <th width="57">数量</th>
            <th width="72">求购次数</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(rank, index) in purchaseRank">
            <td><div>NO.<span>{{index + 1}}</span><i>|</i></div></td>
            <td :title="rank.spCode"><div><span>{{rank.spCode}}</span><i>|</i></div></td>
            <td :title="rank.spAmount"><div><span>{{rank.spAmount}}</span><i>|</i></div></td>
            <td :title="rank.uuAmount"><div>{{rank.uuAmount}}</div></td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>
<script>
  export default {
    data () {
      return {
        applyObj: {
          code: '',
          brand: '',
          unitPrice: '',
          currency: 'RMB',
          encapsulation: '',
          produceDate: '',
          amount: '',
          deadline: ''
        },
        validObj: {
          code: true,
          brand: true,
          unitPrice: true,
//          encapsulation: true,
//          produceDate: true,
          amount: true
//          deadline: true
        },
        pickerOptions: {
          disabledDate (time) {
            // 大于等于今天 小于三个月后
            return time.getTime() < Date.now() - 1000 * 60 * 60 * 24 || time.getTime() > Date.now() + 1000 * 60 * 60 * 24 * 30 * 3
          }
        }
      }
    },
    computed: {
      goodPurchaseMan () {
        return this.$store.state.applyPurchase.goodPurchaseMan.goodPurchaseMan.data
      },
      purchaseRank () {
        return this.$store.state.applyPurchase.purchaseApplyRank.purchaseApplyRank.data
      }
    },
    methods: {
      emptyForm: function () {
        for (let attr in this.applyObj) {
          this.applyObj[attr] = attr === 'currency' ? 'RMB' : ''
        }
      },
      getRankBg: function (index) {
        return index === 0 ? 1 : index < 3 ? 2 : 3
      },
      goPublish: function () {
        if (this.checkAll()) {
          this.$http.post('/seek/saveOneSeekPurchase', this.applyObj)
            .then(response => {
              this.$message.success('发布成功')
              this.emptyForm()
              this.$store.dispatch('applyPurchase/loadPurchaseManList', {page: 1, count: 10})
            }, error => {
              console.log(error)
              this.$message.error('发布失败')
            })
        } else {
          this.$message.error('请填写正确的信息')
        }
      },
      checkCode: function () {
        this.validObj.code = this.applyObj.code && this.applyObj.code !== ''
        return this.validObj.code
      },
      checkBrand: function () {
        this.validObj.brand = this.applyObj.brand && this.applyObj.brand !== ''
        return this.validObj.brand
      },
      checkUnitPrice: function () {
        this.validObj.unitPrice = this.applyObj.unitPrice === '' ? true : this.applyObj.unitPrice > 0 && this.applyObj.unitPrice < 100000000
        return this.validObj.unitPrice
      },
//      checkEncapsulation: function () {
//        this.validObj.encapsulation = this.applyObj.encapsulation && this.applyObj.encapsulation !== ''
//      },
//      checkProduceDate: function () {
//        this.validObj.produceDate = this.applyObj.produceDate && this.applyObj.produceDate !== ''
//      },
      checkAmount: function () {
        this.validObj.amount = this.applyObj.amount === '' ? true : this.applyObj.amount > 0 && this.applyObj.amount < 100000000
        return this.validObj.amount
      },
      checkAll: function () {
        return this.checkCode() && this.checkBrand() && this.checkUnitPrice() && this.checkAmount() && this.checkDeadline()
      },
      checkDeadline: function () {
        this.validObj.deadline = this.applyObj.deadline && this.applyObj.deadline !== ''
        return this.validObj.deadline
      },
      onUnitPriceInput: function () {
        if (this.applyObj.unitPrice < 0) {
          this.applyObj.unitPrice = 0
        } else if (this.applyObj.unitPrice.toString().indexOf('.') !== -1 && this.applyObj.unitPrice.toString().split('.')[1].length > 6) {
          this.applyObj.unitPrice = Number(this.applyObj.unitPrice).toFixed(6)
        }
      },
      onAmountInput: function () {
        if (this.applyObj.amount < 0) {
          this.applyObj.amount = 0
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .publish-apply {
    background: url('/images/applyPurchase/banner.png') center center/cover no-repeat;
    height: 583px;
    text-align: center;
    padding-top: 290px;
    >div {
      display: inline-block;
      border: 1px solid #3975f4;
      height: 267px;
      margin-right: 6px;
      vertical-align: middle;
      background: #fff;
    }
    .good-purchaser {
      width: 225px;
      .good-purchaser-title {
        height: 55px;
        line-height: 55px;
        background: #3975f4;
        margin: 0;
      }
      ul {
        padding: 0 22px 0 10px;
        li {
          line-height: 42px;
          span {
            float: right;
            display: inline-block;
            width: 152px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          i {
            display: inline-block;
            float: left;
            width: 25px;
            color: #fff;
            font-size: 16px;
            font-style: normal;
          }
        }
      }
    }
    .publish-area {
      width: 549px;
      .publish-form-area {
        width: 243px;
        float: left;
        height: 100%;
        p {
          padding-top: 18px;
          font-size: 26px;
          color: #3975f4;
          margin-bottom: 7px;
        }
        >div {
          text-align: left;
          margin-left: 14px;
          .form-item {
            margin-bottom: 5px;
            span {
              width: 80px;
              text-align: right;
              display: inline-block;
              i {
                position: relative;
                top: 2px;
                right: 3px;
                color: #e41515;
              }
            }
            select {
              width: 32px;
              position: absolute;
              height: 20px;
              background: url('/images/applyPurchase/select.png')no-repeat right;
              & + input {
                padding-left: 34px;
              }
            }
            input {
              width: 111px;
              height: 20px;
              line-height: 20px;
              border-radius: 2px;
              padding: 0 3px;
              box-shadow: none;
              -webkit-box-shadow: none;
              -moz-box-shadow: none;
              &.error {
                border-color: #f4645f!important;
              }
            }
          }
        }
        >a {
          width: 90px;
          height: 25px;
          line-height: 25px;
          background: #3975f4;
          color: #fefefe;
          font-size: 16px;
          display: block;
          margin: 0 auto;
          border-radius: 3px;
          cursor: pointer;
        }
      }
      .publish-upload {
        background: url('/images/applyPurchase/publish-apply-bg.png')no-repeat;
        background-size: cover;
        width: 304px;
        height: 100%;
        padding-left: 22px;
        float: right;
        color: #fff;
        position: relative;
        h1 {
          font-size: 26px;
          margin: 22px 0 0 0;
        }
        h2 {
          font-size: 16px;
          margin: 10px 0 23px 0;
        }
        h3 {
          font-size: 16px;
          margin: 20px 0 13px 0;
        }
        .download-line {
          cursor: pointer;
        }
        .apply-logo {
          position: absolute;
          left: -20px;
          top: 86px;
        }
      }
    }
    .apply-rank {
      width: 317px;
      margin-right: 0;
      background: url('/images/applyPurchase/rank-title.png') no-repeat;
      background-color: #fff;
      background-size: 319px 74px;
      background-position: -3px -2px;
      table {
        margin: 76px auto 0;
        width: 98%;
        thead {
          background: #e0e0e0;
          height: 26px;
          line-height: 26px;
          border-radius: 3px;
          tr {
            th {
              font-size: 16px;
              font-weight: bold;
              text-align: center;
            }
          }
        }
        tbody {
          tr {
            height: 25px;
            line-height: 25px;
            &:first-child {
              td {
                padding-top: 10px;
              }
            }
            td {
              &:nth-child(1) {
                color: #f6682f;
                font-size: 12px;
                >div {
                  width: 62px;
                  span {
                    font-size: 16px;
                  }
                }
              }
              &:nth-child(2) {
                >div {
                  width: 122px;
                }
              }
              &:nth-child(3) {
                >div {
                  width: 55px;
                }
              }
              &:nth-child(4) {
                >div {
                  width: 72px;
                }
              }
              >div {
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
                padding: 0 8px;
                position: relative;
                i {
                  font-style: normal;
                  float: right;
                  color: #8b8b8b;
                  font-size: 14px;
                  position: absolute;
                  right: 0;
                }
              }
            }
          }
        }
      }
    }
  }
  .el-date-editor--date{
    width: 110px;
    &.error {
      input {
        border: 1px solid #f4645f !important;
      }
    }
  }
  .el-icon-date {
    display: none;
  }
  .el-input__inner {
    height: 20px;
    border-radius: 0;
    border: 1px solid #c9c9c9;
  }
</style>
