// 船次管理
<template>
  <div class="container">
    <el-row>
      <el-col :span="20">
        <h2 class="bg-left title">船次管理</h2>
      </el-col>
      <el-col :span="4" class="bg-right">
        <el-button class="bg-bottom" type="primary" size="small" @click="revert()">
          <img
            width="6"
            height="10"
            style="vertical-align: bottom; margin-right: 3px"
            src="../../../public/img/lujing.png"
            alt
          />返回
        </el-button>
      </el-col>
    </el-row>
    <div class="center">
      <ws-form ref="deptBudgetList" :model="deptBudgetList" :rules="rules" class="position">
        <div class="basicInformation">
          <div class="small-title">任务详情</div>
          <ws-info-table>
             <ws-form-item label="任务编号" span="1" prop="processNo">{{deptBudgetList.processNo}}</ws-form-item>
            <ws-form-item label="货名" span="1" prop="goodsName">{{deptBudgetList.goodsName}}</ws-form-item>
            <ws-form-item label="重量（吨）" span="1" prop="weight">{{deptBudgetList.weight}}</ws-form-item>
            <ws-form-item
              label="发货地址"
              span="1"
              prop="sendDetailedAddress"
            >{{deptBudgetList.sendDetailedAddress}}</ws-form-item>
           <ws-form-item label="发货人" span="1" prop="sender">{{deptBudgetList.sender}}</ws-form-item>
            <ws-form-item label="发货人电话" span="1" prop="senderPhone">{{deptBudgetList.senderPhone}}</ws-form-item>
            <ws-form-item
              label="收货地址"
              span="1"
              prop="receiveDetailedAddress"
            >{{deptBudgetList.receiveDetailedAddress}}</ws-form-item>
            <ws-form-item label="收货人" span="1" prop="receiver">{{deptBudgetList.receiver}}</ws-form-item>
            <ws-form-item
              label="收货人电话"
              span="1"
              prop="receiverPhone"
            >{{deptBudgetList.receiverPhone}}</ws-form-item>
            <ws-form-item
              label="发货日期"
              span="1"
              prop="deliveryDateStart"
            >{{deptBudgetList.deliveryDateStart}}</ws-form-item>
            <ws-form-item
              label="最晚到货日期"
              span="1"
              prop="deliveryDateEnd"
            >{{deptBudgetList.deliveryDateEnd}}</ws-form-item>
            <ws-form-item label="合同编号" span="1" prop="contractNo">{{deptBudgetList.contractNo}}</ws-form-item> 
          </ws-info-table>
          <div class="small-title">联络员及航次</div>
          <div
            class="position siji"
            v-for="(item, index) in deptBudgetList.tranCarInfoList"
            :key="index"
          >
            <div class="zhong">
                <ws-form-item label="姓名" span="1" prop="driver" class="ys">
                  <el-select
                    v-model="item.driver"
                    placeholder="请选择姓名"
                    filterable
                    :filter-method="dataFilter"
                    @change="selectstaff"
                    class="ys"
                  >
                    <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.staffName"
                      :value="item.staffName"
                    />
                  </el-select>
                </ws-form-item>
                <ws-form-item label="电话" span="1" prop="driverPhone" class="zz">
                  <ws-input
                    :readonly="true"
                    v-model="item.driverPhone"
                    placeholder="请输入电话"
                    maxlength="20"
                    size="small"
                  />
                </ws-form-item>
                <ws-form-item label="发船日期" span="1" prop="sendDateStart" class="">
                  <ws-date-picker
                    v-model="item.sendDateStart"
                    type="date"
                    placeholder="请选择发货日期"
                    value-format="yyyy-MM-dd"
                  />
                </ws-form-item>
                <ws-form-item label="预计到港日期" span="1" prop="receiveDateEnd" class="">
                  <ws-date-picker
                    v-model="item.receiveDateEnd"
                    type="date"
                    placeholder="请选择发货日期"
                    value-format="yyyy-MM-dd"
                  />
                </ws-form-item>
                <ws-form-item label="船名" span="1" prop="shipName" class="">
                  <ws-input
                    v-model="item.shipName"
                    placeholder="请输入船名"
                    maxlength="100"
                    size="small"
                  ></ws-input>
                </ws-form-item>
                <ws-form-item label="航次" span="1" prop="shipNo" class="">
                  <ws-input v-model="item.shipNo" placeholder="请选择航次" maxlength="100" size="small"></ws-input>
                </ws-form-item>
                <ws-form-item label="类型" span="1" prop="shipType" class="">
                  <ws-select v-model="item.shipType" placeholder="请输入" class="typeselect">
                    <ws-option
                      v-for="items in shipType"
                      :key="items.constKey"
                      :label="items.constValue"
                      :value="items.constValue"
                    />
                  </ws-select>
                </ws-form-item>
                <ws-form-item
                  label="数量"
                  span="1"
                  prop="boxNumber"
                  class="siji"
                  v-show="item.shipType=='集装箱'"
                >
                  <ws-input v-model="item.boxNumber" placeholder="请输入数量" size="small"></ws-input>
                </ws-form-item>
              </ws-info-table>
            </div>
            <div
              style="width: 530px"
              class="xia"
              v-for="(items, index) in freightspace"
              v-show="item.shipType=='散船'"
              :key="index"
            >
              <ws-info-table>
                <ws-form-item :label="'仓位号-'+ (index +1 )" span="1" prop="impurity" class="bot">
                  <el-input v-model="items.impurity"></el-input>
                </ws-form-item>
                <ws-form-item label="重量(吨)" span="1" prop="positionWeight" class="bot">
                  <el-input v-model="items.positionWeight"></el-input>
                </ws-form-item>
                <span
                  width="22"
                  height="22"
                  class="del"
                  @click="del(index)"
                  src="../../../public/img/del.png"
                  alt
                >X</span>
              </ws-info-table>
            </div>
            <div>
              <el-button
                class="add bg-bottom"
                type="primary"
                size="small"
                @click="add"
                v-show="item.shipType=='散船'"
              >
                <img width="22" height="22" @click="add" src="../../../public/img/add.png" alt />
                <div class="spans">添加仓位</div>
              </el-button>
            </div>
          </div>
        </div>
      </ws-form> 
      <div style="text-align: right; padding: 10px">
        <el-button class="bg-bottom-up" type="primary" size="small" @click="submit()">提交</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import { regionData, CodeToText, TextToCode } from 'element-china-area-data'
import { shippingLook, packList, dispatchCat } from '@/model/transport/index'
import { getstaff } from '@/model/warehouse/index'
import Pagination from '@/components/Pagination'
import WsUpload from '@/components/WsUpload'
// import { dayjs, fmoney, EventBus } from 'base-core-lib'
import { dayjs, EventBus } from 'base-core-lib'
export default {
  name: 'viewSpareMoney',
  components: {
    WsUpload,
    Pagination
  },
  watch: {
    vesselId(val) {
      this.getList()
    },
    isShow(val) {
      this.showType = val
    }
  },
  data() {
    return {
      //弹出框
      dialogViewSpareMoney: false,
      dialogApproveFormVisible: false,
      // 船舶类型
      monetaryKey: null,
      // 表格显示数据
      tableDate: [],
      // 是否显示
      showType: true,
      // 年
      year: '',
      item: [],
      shipType: [],
      tranType: 3,
      deptBudgetTotal: 0,
      currentPage: 1,
      pageSize: 10,
      searchType: 1,
      searchKeyWord: '',
      contractType: 2,
      startDate: null,
      endDate: null,
      goodnameList: {},
      // 提交类型
      submitType: true,
      storageType: [],
      readonly: true,
      appendixIdsAdd: '',
      uploadSuccess: {},
      onChange: {},
      selectedOptions: [],
      options: [],
      tranCarInfoList: {},
      staffList: [],
      gradeList: [],
      rules: {
        netWeight: [
          {
            required: true,
            type: 'number',
            message: '请输入活动名称',
            trigger: 'blur'
          }
        ]
      },
      arr: [],
      freightspace: [
        // {
        //   impurity:'',
        // },
      ],
      size: 10,
      compId: sessionStorage.getItem('ws-pf_compId'),
      deptCircularPage: {},
      packtypeList: {},
      date: {
        year: dayjs().format('YYYY'),
        month: dayjs().format('MM')
      },
      contractList: [],
      deptBudgetList: {
        warehouseInOutDetail: {}
      },
      historyList: [],

      pickerBeginDateBefore: {
        disabledDate: time => {
          return time.getTime() > Date.now()
        }
      },
      accessoryTFs: false
    }
  },
  mounted() {
    this.getList()
  },
  activated() {
    this.loaddata()
  },
  computed: {
    totalStorage: function() {
      var maxStorage = 0
      for (var i = 0; i < this.freightspace.length; i++) {
        maxStorage += Number(this.freightspace[i].maxStorage)
      }
      return maxStorage
    }
  },
  methods: {
    getList() {
      getstaff({ compId: sessionStorage.getItem('ws-pf_compId') })
        .toPromise()
        .then(response => {
          this.options = response
          this.staffList = response
        })
      //类型
      packList({ constId: 'TRAN4' })
        .toPromise()
        .then(response => {
          this.shipType = response
        })
    },
    dataFilter(val) {
      this.deptBudgetList.personCharge = val
      if (val) {
        console.log(val)
        this.options = this.staffList.filter(item => {
          if (
            !!~item.staffName.indexOf(val) ||
            !!~item.staffName.toUpperCase().indexOf(val.toUpperCase())
          ) {
            return true
          }
        })
      } else {
        this.options = this.staffList
      }
    },
    selectstaff(e) {
      for (var i = 0; i < this.staffList.length; i++) {
        if (this.staffList[i].staffName == e) {
          this.deptBudgetList.driverPhone = this.staffList[i].staffMobilePhone
          this.deptBudgetList.tranCarInfoList[0].driverPhone = this.staffList[
            i
          ].staffMobilePhone
          this.deptBudgetList.personChargeKey = this.staffList[i].staffId
        }
      }
    },
    loaddata() {
      shippingLook({ id: this.$route.query.id })
        .toPromise()
        .then(response => {
          this.deptBudgetList = response
          // for (var i = 0; i < this.deptBudgetList.tranCarInfoList.length; i++) {
          //   var num = this.deptBudgetList.tranCarInfoList[
          //     i
          //   ].positionWeight.split(',')
          //   for (var j = 0; j < num.length; j++) {
          //     var num2 = num[j].split('/')
          //     this.freightspace.push({
          //       impurity: num2[i],
          //       positionWeight: num2[num2.length - 1]
          //     })
          //   }
          // }
        })
    },
    //返回按钮
    revert() {
      this.freightspace = []
      this.$router.go(-1)
    },
    del(index) {
      if (this.freightspace.length > 1) {
        this.freightspace.splice(index, 1)
      }
    },
    add() {
      this.freightspace.push({
        impurity: ''
      })
    },
    selectdriver() {},
    handleChange(value) {
      this.selectedOptions = value
    },
    returnsales() {
      this.deptBudgetList = {}
      this.freightspace = {}
      this.selectedOptions = ''
      this.$router.push({ path: 'tranManagementShipping' })
    },
    //提交按钮
    submit() {
      for (var i = 0; i < this.deptBudgetList.tranCarInfoList.length; i++) {
        if (!this.deptBudgetList.tranCarInfoList[i].driver) {
          this.$message({
            message: '名字不为空',
            type: 'warning'
          })
          return
        }
        if (!this.deptBudgetList.tranCarInfoList[i].driverPhone) {
          this.$message({
            message: '电话号不为空',
            type: 'warning'
          })
          return
        }
        if (!this.deptBudgetList.tranCarInfoList[i].sendDateStart) {
          this.$message({
            message: '发船时间不能为空',
            type: 'warning'
          })
          return
        }
        if (!this.deptBudgetList.tranCarInfoList[i].receiveDateEnd) {
          this.$message({
            message: '预计到港时间不能为空',
            type: 'warning'
          })
          return
        }
        if (!this.deptBudgetList.tranCarInfoList[i].shipName) {
          this.$message({
            message: '船名不能为空',
            type: 'warning'
          })
          return
        }
        if (!this.deptBudgetList.tranCarInfoList[i].shipNo) {
          this.$message({
            message: '航次不能为空',
            type: 'warning'
          })
          return
        }
        if (!this.deptBudgetList.tranCarInfoList[i].boxNumber) {
          this.$message({
            message: '数量不能为空',
            type: 'warning'
          })
          return
        }
        if (!this.deptBudgetList.tranCarInfoList[i].shipType) {
          this.$message({
            message: '类型不能为空',
            type: 'warning'
          })
          return
        }
        if (!this.deptBudgetList.tranCarInfoList[i].impurity) {
          this.$message({
            message: '仓位号不能为空',
            type: 'warning'
          })
        }
        if (!this.deptBudgetList.tranCarInfoList[i].positionWeight) {
          this.$message({
            message: '重量不能为空',
            type: 'warning'
          })
        }
        if (
          new Date(
            this.deptBudgetList.tranCarInfoList[i].sendDateStart
          ).getTime() >
          new Date(
            this.deptBudgetList.tranCarInfoList[i].receiveDateEnd
          ).getTime()
        ) {
          this.$message({
            message: '发船时间不能大于最晚到到港时间！',
            type: 'warning'
          })
          return
        }
      }
      this.$confirm(`提交成功后，任务将下发给相关人员，是否确定提交?`, {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning'
      })
        .then(() => {
          this.$refs.deptBudgetList.validate(valid => {
            if (valid) {
              for (var i = 0; i < this.freightspace.length; i++) {
                var num =
                  this.freightspace[i].impurity +
                  '/' +
                  this.freightspace[i].positionWeight
                this.arr.push(num)
              }

              this.deptBudgetList.totalStorage = this.totalStorage
              this.tranCarInfoList.driver = this.deptBudgetList.driver
              this.tranCarInfoList.driverPhone = this.deptBudgetList.driverPhone
              this.tranCarInfoList.sendDateStart = this.deptBudgetList.sendDateStart
              this.tranCarInfoList.receiveDateEnd = this.deptBudgetList.receiveDateEnd
              this.tranCarInfoList.shipName = this.deptBudgetList.shipName
              this.tranCarInfoList.shipNo = this.deptBudgetList.shipNo
              this.tranCarInfoList.id = this.deptBudgetList.tranCarInfoList[0].id
              this.tranCarInfoList.tranType = this.deptBudgetList.tranType
              this.tranCarInfoList.boxNumber = this.deptBudgetList.boxNumber
              this.tranCarInfoList.shipType = this.deptBudgetList.shipType
              this.tranCarInfoList.positionWeight = this.arr.toString()
              var tranCarInfo = {}
              var tranList = [this.tranCarInfoList]
              tranCarInfo.id = this.deptBudgetList.id
              tranCarInfo.infoId = this.deptBudgetList.infoId
              tranCarInfo.tranCarInfoList = tranList
              dispatchCat(tranCarInfo)
                .toPromise()
                .then(response => {
                  this.$message.success('添加成功')
                  this.deptBudgetList = {}
                  this.freightspace = {}
                  this.selectedOptions = ''
                  this.$router.go(-1)
                })
            } else {
              // EventBus.$emit('error', this.$t('showMessage.asteriskRequired'))
              return false
            }
          })
        })
        .catch(() => {
          return false
        })
    },
    handleClose() {
      this.accessoryTFs = false
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      console.log(`当前页: ${val}`)
      this.getList()
    },
    selecttaskType(e) {
      for (var i = 0; i < this.taskTypeList.length; i++) {
        if (this.taskTypeList[i].value == e) {
          this.searchType = this.taskTypeList[i].type
        }
      }
    }
    // approve() {},
    // listQuery() {}
  }
}
</script>
<style lang="scss" scoped>
/deep/.basicInformation {
  .ws-info-table {
    border: none;
  }
  .el-form-item {
    border: 1px solid #d8dce6;
    width: 33.3333%;
    border: none;
    .el-form-item__label {
      background: transparent;
      border: none;
    }
    .el-form-item__content {
      border: none;
    }
  }
}
.title {
  position: relative;
  // padding-left: 10px;
}
.title::before {
  content: '';
  display: inline-block;
  width: 5px;
  height: 30px;
  background: #5473e8;
  position: absolute;
  left: 0;
}
.el-form {
  padding: 0 10%;
}
.el-button--primary {
  background-color: #5878e8;
  border-color: #5878e8;
}
.el-col {
  background: #f6f7fc;
}
.bg-right {
  text-align: right;
  padding: 16px 20px;
}
.center {
  width: 80%;
  margin: 0 auto;
}

//表格文字
/deep/.ws-info-table .el-form-item .el-form-item__label {
  text-align: left;
  font-size: 14px;
  font-weight: 400;
  color: #8890b1;
  line-height: 16px;
}
//蓝标
.small-title {
  position: relative;
  padding: 10px;
  font-weight: 600;
}
.small-title::before {
  position: absolute;
  content: '';
  display: block;
  background: #5473e8;
  width: 4px;
  height: 14px;
  left: 0px;
  top: 13px;
  padding: 4px 2px;
  font-size: 16px;
  font-family: PingFangSC-Semibold, PingFang SC;
  font-weight: 600;
  color: #323233;
  line-height: 22px;
}
/deep/.position .ws-info-table .el-form-item .el-form-item__label {
  width: 40%;
  text-align: center;
}
.position .zaizhong {
  width: 16%;
}
//减号
.del {
  right: -70px;
  width: 13px;
  height: 13px;
}
.bot {
  width: 30%;
}
//去边框
/deep/.el-form-item {
  border-right: 0px;
  border-bottom: 0px;
}
/deep/.ws-info-table {
  border-left: 0px;
  border-top: 0px;
}
.ws-info-table .el-form-item .el-form-item__content {
  border-right: 0px;
  border-bottom: 0px;
  border-left: 0px;
  border-top: 0px;
}
/deep/.ws-info-table .el-form-item {
  border-right: 0px;
  border-bottom: 0px;
  border-left: 0px;
  border-top: 0px;
}
/deep/.ws-info-table .el-form-item .el-form-item__content {
  background: #f5f7fa;
  border-radius: 4px;

  font-family: PingFangSC-Regular, PingFang SC;
  margin-bottom: 5px;
  // background-color: #fff;
  font-size: 14px;
  font-weight: 400;
  color: #8890b1;
  line-height: 16px;
}
/deep/.ws-info-table .el-form-item .el-form-item__label {
  background-color: #fff;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #8890b1;
  line-height: 16px;
}
.position {
  width: 90%;
}
//下表格样式
.position.siji {
  background: white;
  border-radius: 4px;
  /* border: 1px solid #d8dce6; */
  width: 1010px;
  
}
/deep/.zi {
  width: 64px;
  height: 22px;
  font-size: 16px;
  font-family: PingFangSC-Semibold, PingFang SC;
  font-weight: 600;
  color: #323233;
  line-height: 22px;
}
//表格文字
/deep/.ws-info-table .el-form-item .el-form-item__label {
  text-align: left;
  font-size: 14px;
  font-weight: 400;
  color: #8890b1;
  line-height: 16px;
}
//小标题文字
.small-title::before {
  position: absolute;
  content: '';
  display: block;
  background: #5473e8;
  width: 4px;
  height: 14px;
  left: 0px;
  top: 13px;
  padding: 4px 2px;
}
/deep/.basicInformation {
  .el-form-item {
    width: 25%;

    .el-form-item__label {
      background: transparent;
      border: none;
    }
    .el-form-item__content {
      // border: none;
    }
  }
}
//下方表格
// .biao{
//   background: #F6F7FC;
//   border-radius: 4px;
//   border: 1px solid #D8DCE6;
//   margin-top: 10px;

// }
//添加仓位样式
.add {
  width: 100px;
  height: 34px;
  background: #f6f7fb;
  border-radius: 17px;
  color: #5473e8;
  font-size: 14px;
  border: none;
  margin-top: 8px;
}
.add img {
  display: inline-block;
  margin-top: 3px;
  margin-left: -12px;
}
.add .spans {
  display: table-caption;
  width: 56px;
  height: 20px;
  line-height: 18px;
}
/deep/.xia {
  width: 500px;
  height: 54px;
  border-radius: 4px;
  border: 1px solid #d8dce6;
  margin-top: 10px;
  background: #f6f7fc;
  border-radius: 4px;
}
.basicInformation .ws-info-table {
  border: none;
  width: 1000px;
}
.el-input--small .el-input__inner {
  border-block-color: #f6f7fc;
  background: #f6f7fc;
}
.zhong {
  background: #f6f7fc;
  border: 1px solid #d8dce6;
}
.bg-left {
  padding-left: 30px;
}
.ys{
  // margin-left: 300px;
  width: 230px;
  background: #5878e8;
}
</style>
