<template>
  <div class="container">
    <el-row>
      <el-col :span="12">
        <h2 class="bg-left title">创建点价确认单(
          <template>
            <span v-if="this.$route.query.contractType == 1">期货销售合同</span>
            <span v-if="this.$route.query.contractType == 2">期货采购合同</span> </template>)
        </h2>
      </el-col>
      <el-col :span="12" class="bg-right">
        <el-button class="bg-bottom" type="primary" size="small" @click="returnsales()"><img width="6" height="10"
            style="vertion-align: bottom; margin-right: 3px" src="../../../public/img/lujing.png" alt="" />返回
        </el-button>
      </el-col>
    </el-row>

    <div class="wenzi">
      <h3>所属合同</h3>
      <p>
        &nbsp;&nbsp;<template>
          <span v-if="this.$route.query.contractType == '1'">销售合同</span>
          <span v-if="this.$route.query.contractType == '2'">采购合同</span>
        </template>(
        <template>{{ this.$route.query.contractNo }}</template>
        )
      </p>
    </div>
    <div class="center">
      <ws-form ref="deptBudgetList" :model="deptBudgetList">
        <!--基本信息-->

        <ws-info-table>
          <!--点价单编号-->
          <ws-form-item label="点价单编号" span="1" prop="priceListNo" class="readonly">
            <ws-input v-model="deptBudgetList.priceListNo" placeholder="请输入点价单编号" maxlength="20" size="small" />
          </ws-form-item>
          <!--品种合约及方向-->
          <ws-form-item label="品种合约及方向" span="1" prop="varietyContractDirection" class="readonly">
            <ws-input v-model="deptBudgetList.varietyContractDirection" placeholder="请输入品种合约及方向" maxlength="100"
              size="small" />
          </ws-form-item>

          <!--买方联络人-->
          <ws-form-item label="买方联络人" span="1" prop="buyer">
            <!-- <ws-input v-model="deptBudgetList.buyer" placeholder="请输入买方联络人" maxlength="120" size="small" /> -->
            <el-autocomplete class="inline-input" v-model="deptBudgetList.buyer" :fetch-suggestions="querySearch1"
              placeholder="请输入买方联络人" @select="handleSelect"></el-autocomplete>
          </ws-form-item>
          <!--成交数量-->
          <ws-form-item label="成交数量" span="1" prop="transactionsNumberHand" class="readonly">
            <ws-input @input="transactionsNumberHandChange" v-model="deptBudgetList.transactionsNumberHand"
              placeholder="请输入" maxlength="100" size="small" />
            &nbsp;&nbsp;&nbsp;手&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;即&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <ws-input readonly="readonly" v-model="deptBudgetList.transactionsNumber" placeholder="自动计算" maxlength="100"
              size="small" />&nbsp;&nbsp;&nbsp;吨
          </ws-form-item>

          <!--买方联络人电话-->
          <ws-form-item label="买方联络人电话" span="1" prop="buyerPhone">
            <!-- <ws-input v-model="deptBudgetList.buyerPhone" placeholder="请输入买方联络人电话" maxlength="120" size="small" /> -->
            <el-autocomplete class="inline-input" v-model="deptBudgetList.buyerPhone" :fetch-suggestions="querySearch3"
              placeholder="买方联络人电话" @select="handleSelect"></el-autocomplete>
          </ws-form-item>

          <!--成交价格(元/吨)-->
          <ws-form-item label="成交价格(元/吨)" span="1" prop="transactionPrice">
            <ws-input v-model="deptBudgetList.transactionPrice" placeholder="请输入成交价格" maxlength="120" size="small" />
          </ws-form-item>

          <!--卖方联络人-->
          <ws-form-item label="卖方联络人" span="1" prop="seller">
            <!-- <ws-input v-model="deptBudgetList.seller" placeholder="请输入卖方联络人" maxlength="120" size="small" /> -->
            <el-autocomplete class="inline-input" v-model="deptBudgetList.seller" :fetch-suggestions="querySearch2"
              placeholder="请输入卖方名称" @select="handleSelect"></el-autocomplete>
          </ws-form-item>

          <!--成交日期-->
          <ws-form-item label="成交日期" span="1" prop="transactionDate">
            <ws-date-picker v-model="deptBudgetList.transactionDate" type="date" placeholder="请选择成交日期"
              value-format="yyyy-MM-dd" />
          </ws-form-item>

          <!--卖方联络人电话-->
          <ws-form-item label="卖方联络人电话" span="1" prop="sellerPhone">
            <!-- <ws-input v-model="deptBudgetList.sellerPhone" placeholder="请输入卖方联络人电话" maxlength="100" size="small" /> -->
            <el-autocomplete class="inline-input" v-model="deptBudgetList.sellerPhone" :fetch-suggestions="querySearch4"
              placeholder="请输入卖方电话" @select="handleSelect"></el-autocomplete>
          </ws-form-item>

          <!--备注-->
          <ws-form-item label="备注" span="1" prop="remarks">
            <ws-input v-model="deptBudgetList.remarks" placeholder="请输入备注,不超过200字" maxlength="120" size="small" />
          </ws-form-item>
        </ws-info-table>
        <ws-upload ref="upload" :comp-id="compId" :appendix-ids="deptBudgetList.addressUrl" @onChange="onChange"
          accept=".jpg, .jpeg, .png, .pdf, .doc, .zip, .rar" />
      </ws-form>
    </div>
    <div style="text-align: right; padding: 10px" class="center">
      <el-button class="bg-bottom" type="primary" size="small" @click="submit()">提交</el-button>
    </div>
  </div>
</template>
<script>
  import {
    addPriceConfirmationSheet
  } from '@/model/contarct/index'
  import WsUpload from '@/components/WsUpload'
  import {
    regionData
  } from 'element-china-area-data'

  export default {
    name: 'viewSpareMoney',
    components: {
      WsUpload,
    },
    watch: {
      vesselId(val) {
        this.getVesselData()
      },
      isShow(val) {
        this.showType = val
      },
    },
    data() {
      return {
        restaurants1: [],
        restaurants2: [],
        restaurants3: [],
        restaurants4: [],
        //弹出框
        dialogViewSpareMoney: false,
        dialogApproveFormVisible: false,
        // 船舶类型
        monetaryKey: null,
        // 表格显示数据
        tableDate: [],
        // 是否显示
        showType: true,
        // 年
        year: '',
        options_: regionData,
        selectedOptions: [],
        appendixIdss: '',
        selectedOptions1: [],
        // 提交类型
        submitType: true,
        size: 10,
        unitList: [],
        enclosureTypeFlag: 0,
        priceConfirmationSheet: {
          contractNo: this.$route.query.contractNo,
          contractType: this.$route.query.contractType,
          varietyContractDirection: '',
          buyer: '',
          seller: '',
          sellerPhone: '',
          buyerPhone: '',
          // enclosureTypeFlag: 0,
          transactionsNumberHand: '',
          transactionsNumber: '',
          transactionPrice: '',
          transactionDate: '',
          remarks: '',
          priceListNo: '',
          addressUrl: '',
        },
        deptBudgetList: {},
        compId: localStorage.getItem('ws-pf_compId'),
      }
    },
    mounted() {
      this.restaurants1 = JSON.parse(localStorage.getItem('deptBudgetList_data1'));
      this.restaurants2 = JSON.parse(localStorage.getItem('deptBudgetList_data2'));
      this.restaurants3 = JSON.parse(localStorage.getItem('deptBudgetList_data3'));
      this.restaurants4 = JSON.parse(localStorage.getItem('deptBudgetList_data4'));
      if (!this.restaurants1) this.restaurants1 = [];
      if (!this.restaurants2) this.restaurants2 = [];
      if (!this.restaurants3) this.restaurants3 = [];
      if (!this.restaurants4) this.restaurants4 = [];
    },
    methods: {
      querySearch1(queryString, cb) {
        var restaurants1 = this.restaurants1;
        var results = queryString ? restaurants1.filter(this.createFilter(queryString)) : restaurants1;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      querySearch2(queryString, cb) {
        var restaurants2 = this.restaurants2;
        var results = queryString ? restaurants2.filter(this.createFilter(queryString)) : restaurants2;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      querySearch3(queryString, cb) {
        var restaurants3 = this.restaurants3;
        var results = queryString ? restaurants3.filter(this.createFilter(queryString)) : restaurants3;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      querySearch4(queryString, cb) {
        var restaurants4 = this.restaurants4;
        var results = queryString ? restaurants4.filter(this.createFilter(queryString)) : restaurants4;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      handleSelect(item) {
        console.log(item);
      },
      // 关闭 dialog时 处理文件url 初始化upload组件
      handleClose() {
        this.dialogViewSpareMoney = false
      },
      handleChange(value) {
        this.selectedOptions = value
      },
      handleChange1(value) {
        this.selectedOptions1 = value
      },
      returnsales() {
        this.priceConfirmationSheet = {}
        this.deptBudgetList = {}
        this.$router.push({
          path: 'priceConfirmationSheet',
          query: {
            contractNo: this.$route.query.contractNo,
            contractType: this.$route.query.contractType
          }
        })
      },
      onChange() {
        this.$refs.upload
          .handleSaveBill()
          .then(async response => {
            this.deptBudgetList.addressUrl = response
          })
          .catch(res => {
            EventBus.$emit('error', (JSON.parse(res) || {}).message)
            this.$refs.upload.clearFiles()
          })
      },
      //计算成交数量(吨)
      transactionsNumberHandChange(e) {
        this.deptBudgetList.transactionsNumber = e * 10
      },
      submit() {
        if (!this.deptBudgetList.priceListNo) {
          this.$message({
            message: '点价单编号不能为空!',
            type: 'warning',
          })
          return
        }
        if (
          this.deptBudgetList.priceListNo.length < 1 ||
          this.deptBudgetList.priceListNo.length > 20
        ) {
          this.$message({
            message: '点价单编号输入错误!',
            type: 'warning',
          })
          return
        }
        if (!this.deptBudgetList.varietyContractDirection) {
          this.$message({
            message: '品种合约及方向不能为空!',
            type: 'warning',
          })
          return
        }
        if (
          this.deptBudgetList.varietyContractDirection.length < 1 ||
          this.deptBudgetList.varietyContractDirection.length > 20
        ) {
          this.$message({
            message: '品种合约及方向输入错误!',
            type: 'warning',
          })
          return
        }
        if (!this.deptBudgetList.buyer) {
          this.$message({
            message: '买方联络人不能为空!',
            type: 'warning',
          })
          return
        }
        if (
          this.deptBudgetList.buyer.length < 2 ||
          this.deptBudgetList.buyer.length > 20
        ) {
          this.$message({
            message: '买方联络人姓名输入错误!',
            type: 'warning',
          })
          return
        }
        if (!this.deptBudgetList.buyerPhone) {
          this.$message({
            message: '买方联络人电话不能为空!',
            type: 'warning',
          })
          return
        }
        if (isNaN(this.deptBudgetList.buyerPhone)) {
          this.$message({
            message: '卖方联络人电话输入有误!',
            type: 'warning',
          })
          return
        }
        if (!this.deptBudgetList.seller) {
          this.$message({
            message: '卖方联络人不能为空!',
            type: 'warning',
          })
          return
        }

        if (!this.deptBudgetList.sellerPhone) {
          this.$message({
            message: '卖方联络人电话不能为空!',
            type: 'warning',
          })
          return
        }
        if (isNaN(this.deptBudgetList.sellerPhone)) {
          this.$message({
            message: '卖方联络人电话输入有误!',
            type: 'warning',
          })
          return
        }
        if (!this.deptBudgetList.transactionsNumberHand) {
          this.$message({
            message: '成交数量不能为空!',
            type: 'warning',
          })
          return
        }

        if (
          isNaN(this.deptBudgetList.transactionsNumberHand) ||
          (String(this.deptBudgetList.transactionsNumberHand).indexOf('.') !=
            -1 &&
            String(this.deptBudgetList.transactionsNumberHand).length -
            (String(this.deptBudgetList.transactionsNumberHand).indexOf('.') +
              1) >
            2) ||
          this.deptBudgetList.transactionsNumberHand < 1 ||
          this.deptBudgetList.transactionsNumberHand > 10000
        ) {
          this.$message({
            message: '输入成交数量有误!',
            type: 'warning',
          })
          return
        }
        if (
          this.deptBudgetList.transactionPrice < 0 ||
          this.deptBudgetList.transactionPrice > 100000000 ||
          (String(this.deptBudgetList.transactionPrice).indexOf('.') != -1 &&
            String(this.deptBudgetList.transactionPrice).length -
            (String(this.deptBudgetList.transactionPrice).indexOf('.') + 1) >
            2)
        ) {
          this.$message({
            message: '成交价格输入错误',
            type: 'warning',
          })
          return
        }
        if (this.deptBudgetList.transactionPrice) {
          if (isNaN(this.deptBudgetList.transactionPrice)) {
            this.$message({
              message: '成交价格输入错误!',
              type: 'warning',
            })
            return
          }
        }
        if (!this.deptBudgetList.transactionPrice) {
          this.$message({
            message: '成交价格不能为空！',
            type: 'warning',
          })
          return
        }
        if (!this.deptBudgetList.transactionDate) {
          this.$message({
            message: '成交日期不能为空!',
            type: 'warning',
          })
          return
        }
        this.deptBudgetList.contractType = this.$route.query.contractType,
          this.deptBudgetList.contractNo = this.$route.query.contractNo,

          this.priceConfirmationSheet = this.deptBudgetList
        this.$confirm(`确定提交确认单信息`, {
            cancelButtonText: '取消',
            confirmButtonText: '确定',
            type: 'warning',
          })
          .then(() => {
            addPriceConfirmationSheet(this.priceConfirmationSheet)
              .toPromise()
              .then((response) => {
                // 存储买方、卖方、买方电话、卖方电话
                let _data = {
                  byer: this.deptBudgetList.buyer,
                  seller: this.deptBudgetList.seller,
                  buyerPhone: this.deptBudgetList.buyerPhone,
                  sellerPhone: this.deptBudgetList.sellerPhone
                }
                if (this.restaurants1.length <= 20) {
                  this.restaurants1 = this.restaurants1.filter(function(val) {
                    if (val.value != _data.byer) {
                      return val
                    }
                  })
                  this.restaurants1.unshift({
                    value: _data.byer
                  })
                } else {
                  this.restaurants1.unshift({
                    value: _data.byer
                  }).pop({
                    value: _data.byer
                  })
                }
                localStorage.setItem('deptBudgetList_data1', JSON.stringify(this.restaurants1));
                if (this.restaurants2.length <= 20) {
                  this.restaurants2 = this.restaurants2.filter(function(val) {
                    if (val.value != _data.seller) {
                      return val
                    }
                  })
                  this.restaurants2.unshift({
                    value: _data.seller
                  })
                } else {
                  this.restaurants2.unshift({
                    value: _data.seller
                  }).pop({
                    value: _data.seller
                  })
                }
                localStorage.setItem('deptBudgetList_data2', JSON.stringify(this.restaurants2));
                if (this.restaurants2.length <= 20) {
                  this.restaurants3 = this.restaurants3.filter(function(val) {
                    if (val.value != _data.buyerPhone) {
                      return val
                    }
                  })
                  this.restaurants3.unshift({
                    value: _data.buyerPhone
                  })
                } else {
                  this.restaurants3.unshift({
                    value: _data.buyerPhone
                  }).pop({
                    value: _data.buyerPhone
                  })
                }
                localStorage.setItem('deptBudgetList_data3', JSON.stringify(this.restaurants3));
                if (this.restaurants4.length <= 20) {
                  this.restaurants4 = this.restaurants3.filter(function(val) {
                    if (val.value != _data.sellerPhone) {
                      return val
                    }
                  })
                  this.restaurants4.unshift({
                    value: _data.sellerPhone
                  })
                } else {
                  this.restaurants4.unshift({
                    value: _data.sellerPhone
                  }).pop({
                    value: _data.sellerPhone
                  })
                }
                localStorage.setItem('deptBudgetList_data4', JSON.stringify(this.restaurants4));
                this.$message.success('添加成功')
                this.deptBudgetList = {}
                this.priceConfirmationSheet = {}
                this.$router.push({
                  path: 'priceConfirmationSheet',
                  query: {
                    contractNo: this.$route.query.contractNo,
                    contractType: this.$route.query.contractType
                  }
                })
              })
          })
          .catch(() => {
            return false
          })
      },
      resetForm(deptBudgetList) {
        this.$refs[deptBudgetList].resetFields()
      },
    },
  }
</script>

<style lang="scss" scoped>
  /deep/.ws-info-table .el-form-item .el-form-item__content {
    padding: 0 25px;
  }

  /deep/.ws-info-table .el-form-item {
    border-right: 1px solid #cdd2dc;
    border-bottom: 1px solid #cdd2dc;
  }

  .title {
    position: relative;
  }

  .title::before {
    content: '';
    display: inline-block;
    width: 5px;
    height: 30px;
    background: #5473e8;
    position: absolute;
    left: 0;
  }

  .el-button--primary {
    background-color: #5878e8;
    border-color: #5878e8;
  }

  .el-col {
    background: #f6f7fc;
  }

  .readonly {
    position: relative;
  }

  /deep/.ws-info-table .el-form-item .el-form-item__content {
    padding: 0 25px;
    border-left: 1px solid #cdd2dc;
    background: #fafbfc;
  }

  /deep/.ws-info-table .el-form-item .el-form-item__label {
    width: 140px;
    text-align: center;
    background: #f0f2f6;
    // border: 1px solid #cdd2dc;
  }

  .button-container {
    display: flex;
    flex-wrap: nowrap;
    justify-content: space-between;
    align-items: center;
    background-color: #fff;
    width: 100%;
    height: 50px;
    padding: 0 10px;

    &>div {
      margin-left: 10px;
      display: flex;
      flex-wrap: nowrap;
      flex-direction: row;

      &>span {
        line-height: 50px;
      }
    }

    /deep/.auditFlow-box {
      position: unset;
      margin-left: 10px;

      &/deep/.auditFlow-icon {
        width: auto;
        padding-right: 30px;
      }

      &/deep/.auditFlow-main {
        position: absolute;
      }
    }
  }

  .box-app {
    display: inline-block;
    float: left;
    margin-left: 30px;
    line-height: 50px;
  }

  /deep/.el-dialog {
    .el-form-item {
      margin-bottom: 0 !important;

      .el-input--medium {
        textarea {
          min-height: 100px !important;
        }
      }
    }
  }

  .collapse-bottom {
    margin-bottom: 20px;
  }

  .shangchuan {
    margin-left: 22px;
  }

  .input-main .textarea .el-textarea__inner {
    width: 100%;
    z-index: 1;
  }

  .bg-left {
    padding-left: 30px;
  }

  .bg-right {
    padding-right: 10px;
    text-align: right;
  }

  .bg-bottom {
    margin: 16px 0px;
  }

  .wenzi {
    width: 70%;
    margin: 0 auto;
  }

  .wenzi h3 {
    display: inline-block;
    left: 10px;
  }

  .wenzi p {
    display: inline-block;
  }

  .center {
    width: 70%;
    margin: 0 auto;
  }

  .el-form-item {
    width: 50%;
  }

  .el-form-item__label {
    text-align: center;
  }

  .ce {
    width: 70%;
    margin: 0 auto;
  }

  /*.crt-main .textarea /deep/ .el-form-item__label {*/
  /*  height: 82px;*/
  /*}*/
  // 控制select为只读的时候显示样式

  .hide-sel {
    .el-input__inner {
      border: 0px;
    }

    .el-icon-arrow-up {
      display: none;
    }

    .el-textarea__inner {
      background-color: #fff !important;
      border: 0;
    }

    .el-date-editor {
      i {
        display: none;
      }
    }

    .is-disabled {
      .el-input__inner:hover {
        background-color: #fff !important;
        border: 0;
      }

      color: #606266;

      .el-input__inner {
        background-color: #fff !important;
        border: 0;
        color: #606266;
      }

      .el-textarea__inner {
        background-color: #fff !important;
        border: 0;
        color: #606266;
      }
    }
  }

  /deep/.ws-info-table .el-form-item .el-form-item__label {
    width: 130px;
  }

  /deep/.el-date-editor {
    font-size: 13px;
  }

  /deep/.el-textarea {
    width: 101%;
    margin: 0px;
  }

  /deep/el-date-editor--date {
    width: 200px;
  }

  .unchanged {
    position: absolute;
    left: 37px;
    width: 102px;
    height: 14px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #afb5cb;
    line-height: 14px;
  }

  /deep/.el-input--small .el-input__inner {
    height: 32px;
    line-height: 32px;
  }

  // 控制select为只读的时候显示样式
  /deep/.ws-class-table-col {
    height: auto;
    padding: 0px 2px;

    /deep/.el-input__inner {
      padding: 0px 2px;
    }
  }

  /deep/.is-disabled {
    .el-input__prefix,
    .el-input__suffix {
      display: none;
    }

    .el-input__inner {
      background-color: #fff;
      border-color: #fff !important;
      color: #000 !important;
      font-size: 14px;
      cursor: text;
      padding: 0 !important;
    }
  }

  .winseaview-view {
    padding: 0 0 20px;
  }

  .container {
    overflow: scroll;
    height: 93vh;
  }

  .addressUrls {
    width: 100%;
    display: flex;
    margin-top: 10px;
  }

  .addressUrls-item {
    position: relative;
    display: flex;
  }

  .icon-guanbi {
    position: absolute;
    right: 8px;
  }

  .addressUrl {
    margin: 0px 10px;
    border-radius: 3px;
  }

  //上传文件成功标识
  /deep/.el-upload-list__item-status-label {
    position: absolute;
    right: 22px;
    top: -1px;
    line-height: inherit;
    display: none;
  }
   .inline-input {
      width: 100%;
    }
</style>
