<!--采购合同-->
<!--2019年5月30日 20:25:16 by jlx-->
<template>
  <div class="container">
    <el-row>
      <el-col :span="12">
        <h2 class="bg-left">创建采购合同</h2>
      </el-col>
      <el-col :span="12" class="bg-right">
        <el-button
          class="bg-bottom"
          type="primary"
          size="small"
          @click="returnsales()"
          >返回</el-button
        >
      </el-col>
    </el-row>
   
    <ws-form
      ref="deptBudgetList"
      :rules="mainReportAdd"
      :model="deptBudgetList"
    >
      <div class="remark">
        <h3>基本信息</h3>
        <p style="color: #8890b1">
          注：基本信息和货物信息均为必填项，“*” 标记的条目提交后不可修改。
        </p>
      </div>
      <ws-info-table>
        <ws-form-item label="合同编号" span="1" prop="contractNo">
          <ws-input
            v-model="deptBudgetList.contractNo"
            maxlength="100"
            size="small"
          />
          <span class="unchangeable">*</span>
        </ws-form-item>
        <ws-form-item label="运输方式" span="1" prop="shippingType">
          <ws-input
            v-model="deptBudgetList.shippingType"
            maxlength="100"
            size="small"
          />
        </ws-form-item>
        <ws-form-item label="买方" span="1" prop="buyer">
          <ws-input
            v-model="deptBudgetList.buyer"
            maxlength="100"
            size="small"
          />
        </ws-form-item>
        <ws-form-item label="结算方式" span="1" prop="settlementMethod">
          <ws-input
            v-model="deptBudgetList.settlementMethod"
            maxlength="100"
            size="small"
          />
        </ws-form-item>
        <ws-form-item label="卖方" span="1" prop="seller">
          <ws-input
            v-model="deptBudgetList.seller"
            maxlength="100"
            size="small"
          />
        </ws-form-item>
        <ws-form-item label="货源地" span="1" prop="sourceGoods">
          <ws-input
            v-model="deptBudgetList.sourceGoods"
            maxlength="100"
            size="small"
          />
        </ws-form-item>
        <ws-form-item label="买方电话" span="1" prop="buyerPhone">
          <ws-input
            v-model="deptBudgetList.buyerPhone"
            maxlength="100"
            size="small"
          />
        </ws-form-item>
        <ws-form-item label="交货地" span="1" prop="placeDelivery">
          <ws-input
            v-model="deptBudgetList.placeDelivery"
            maxlength="100"
            size="small"
          />
        </ws-form-item>
        <ws-form-item label="卖方电话" span="1" prop="sellerPhone">
          <ws-input
            v-model="deptBudgetList.sellerPhone"
            maxlength="100"
            size="small"
          />
        </ws-form-item>
        <ws-form-item label="包装方式" span="1" prop="packingMethod">
          <ws-select
            v-model="deptBudgetList.packingMethod"
            placeholder=""
            class="typeselect"
            @change="selectpackingMethod"
          >
            <ws-option
              v-for="item in packtypeList"
              :key="item.constKey"
              :label="item.constValue"
              :value="item.constValue"

            />
          </ws-select>
            <span class="unchangeable">*</span>
          </ws-form-item>

          <!--运输方式-->
          <ws-form-item label="运输方式" span="1" prop="shippingType">
            <ws-input
              v-model="deptBudgetList.shippingType"
              placeholder="请输入运输方式"
              maxlength="20"
              size="small"
            />
          </ws-form-item>

          <!--买方-->
          <ws-form-item label="买方" span="1" prop="buyer">
            <ws-input
              v-model="deptBudgetList.buyer"
              placeholder="请输入买方名称"
              maxlength="100"
              size="small"
            />
            <span class="unchangeable">*</span>
          </ws-form-item>

          <!--结算方式-->
          <ws-form-item label="结算方式" span="1" prop="settlementMethod">
            <ws-input
              v-model="deptBudgetList.settlementMethod"
              placeholder="请输入结算方式"
              maxlength="100"
              size="small"
            />
          </ws-form-item>

          <!--卖方-->
          <ws-form-item label="卖方" span="1" prop="seller">
            <ws-input
              v-model="deptBudgetList.seller"
              placeholder="请输入卖方名称"
              maxlength="100"
              size="small"
            />
            <span class="unchangeable">*</span>
          </ws-form-item>

          <!--货源地-->
          <ws-form-item label="货源地" span="1" prop="sourceGoods">
            <ws-input
              v-model="deptBudgetList.sourceGoods"
              placeholder="请输入货源地"
              maxlength="100"
              size="small"
            />
          </ws-form-item>

          <!--买方电话-->
          <ws-form-item label="买方电话" span="1" prop="buyerPhone">
            <ws-input
              v-model="deptBudgetList.buyerPhone"
              placeholder="请输入买方电话"
              maxlength="100"
              size="small"
            />
          </ws-form-item>

          <!--交货地-->
          <ws-form-item label="交货地" span="1" prop="placeDelivery">
            <ws-input
              v-model="deptBudgetList.placeDelivery"
              placeholder="请输入交货地"
              maxlength="100"
              size="small"
            />
          </ws-form-item>

          <!--卖方电话-->
          <ws-form-item label="卖方电话" span="1" prop="sellerPhone">
            <ws-input
              v-model="deptBudgetList.sellerPhone"
              placeholder="请输入卖方电话"
              maxlength="100"
              size="small"
            />
          </ws-form-item>

          <!--包装方式-->
          <ws-form-item label="包装方式" span="1" prop="packingMethod">
            <ws-select
              v-model="deptBudgetList.packingMethod"
              placeholder=""
              class="typeselect"
              @change="selectpackingMethod"
            >
              <ws-option
                v-for="item in packtypeList"
                :key="item.constKey"
                :label="item.constValue"
                :value="item.constValue"
              />
            </ws-select>
          </ws-form-item>

          <!--重量（吨）-->
          <ws-form-item label="重量（吨）" span="1" prop="weight">
            <ws-input
              v-model="deptBudgetList.weight"
              placeholder="请输入重量（吨）"
              maxlength="100"
              size="small"
            />
          </ws-form-item>

          <!--验收方式-->
          <ws-form-item label="验收方式" span="1" prop="acceptanceMethod">
            <el-select
              v-model="deptBudgetList.acceptanceMethod"
              placeholder="请选择验收方式"
              style="width: 100%"
              filterable
              @change="selectunitList"
            >
              <el-option
                v-for="(item, index) in unitList"
                :key="item.constValue"
                :label="item.constValue"
                :value="item.constValue"
              >
                <span class="unit-left" style="float: left">
                  <span v-if="item.flag == 'delete'">
                    {{ item.constValue }}</span
                  >
                  <!-- 新增文本框 -->
                  <div
                    style="width: 160px"
                    v-if="item.flag !== 'delete'"
                    @click.stop
                  >
                    <ws-input
                      v-model="item.constValue"
                      clearable
                      maxlength="10"
                      style="width: 100%"
                    ></ws-input>
                  </div>
                </span>
                <span style="float: right; color: #8492a6; font-size: 13px">
                  <!-- 对号 -->
                  <i
                    class="el-icon-check"
                    style="line-height: 29px; margin-left: 10px"
                    v-if="item.flag !== 'delete'"
                    @click.stop="saveClick(item, index)"
                  ></i>
                  <!-- 编辑 -->
                  <i
                    class="el-icon-edit"
                    style="line-height: 29px; margin-left: 10px"
                    v-if="item.flag == 'delete'"
                    @click.stop="editClick(item, index)"
                  ></i>
                  <!-- 删除 -->
                  <i
                    class="el-icon-delete"
                    style="line-height: 29px"
                    @click.stop="deleteClick(item, index)"
                  ></i>
                </span>
              </el-option>
              <!-- 新增按钮 -->
              <el-option value="" label="">
                <div style="text-align: center">
                  <ws-button type="primary" @click.stop="addClick">{{
                    $t('button.add')
                  }}</ws-button>
                </div>

              </el-option>
            </el-select>
          </ws-form-item>

          <!--溢短装（%）-->
          <ws-form-item label="溢短装（%）" span="1" prop="overShort">
            <ws-input
              v-model="deptBudgetList.overShort"
              placeholder="请输入溢短装比例（%）"
              maxlength="100"
              size="small"
            />
          </ws-form-item>
          <!--交货日期（起）-->
          <ws-form-item
            label="交货日期（起）"
            span="1"
            prop="deliveryDateStart"
          >
            <ws-date-picker
              :picker-options="pickerBeginDateBefore"
              v-model="deptBudgetList.deliveryDateStart"
              type="date"
              default-value="1980-01-01"
              placeholder="起始日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            />
          </ws-form-item>

          <!--合同单价（元/吨）-->
          <ws-form-item label="合同单价（元/吨）" span="1" prop="unitContractPrice">
            <ws-input
              v-model="deptBudgetList.unitContractPrice"
              placeholder="请输入合同单价"
              maxlength="100"
              size="small"
            />
            <span class="unchangeable">*</span>
          </ws-form-item>

          <!--交货日期（止）-->
          <ws-form-item label="交货日期（止）" span="1" prop="deliveryDateEnd">
            <ws-date-picker
              :picker-options="pickerBeginDateBefore"
              v-model="deptBudgetList.deliveryDateEnd"
              type="date"
              default-value="1980-01-01"
              placeholder="起始日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            />
          </ws-form-item>

          <!--合同总价-->
          <ws-form-item label="合同总价" span="1" prop="totalContractPrice">
            <ws-input
              v-model="deptBudgetList.totalContractPrice"
              placeholder="请输入合同总价"
              maxlength="100"
              size="small"
            />
          </ws-form-item>

          <!--签订日期-->
          <ws-form-item label="签订日期" span="1" prop="signingDate">
            <ws-date-picker
              :picker-options="pickerBeginDateBefore"
              v-model="deptBudgetList.signingDate"
              type="date"
              default-value="1980-01-01"
              placeholder="起始日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
            />
          </ws-form-item>
        </ws-info-table>
      </ws-form>
    </div>
    
</template>
<script>
//  import { getVesselOne } from '@/model/procurement/basic'
//  import { dayjs } from 'base-core-lib'
import {
  packList,
  addList,
  xiala,
  addxiala,
  editxiala,
  delxiala,
} from '@/model/contarct/index'
import WsUpload from '@/components/WsUpload'

export default {
  name: 'viewSpareMoney',
  components: {
    WsUpload,
  },
  watch: {
    vesselId(val) {
      this.getVesselData()
    },
    isShow(val) {
      this.showType = val
    },
  },
  data() {
    return {
      //弹出框
      dialogViewSpareMoney: false,
      dialogApproveFormVisible: false,
      // 船舶类型
      monetaryKey: null,
      // 表格显示数据
      tableDate: [],
      // 是否显示
      showType: true,
      // 年
      year: '',
      // 提交类型
      submitType: true,
      tableData: [{ date: 1111, name: 'qqqq', address: 'errrtt' }],

      packtypeList: [],
      compId: sessionStorage.getItem('ws-pf_compId'),
      mainReportAdd: {},
      appendixIdsAdd: '',
      size: 10,
      // uploadSuccess: {},
      // onChange:{},
      unitList: [],
      goodnameList: [],

      gradeList: [],
      ChapterTwoList: [],

      deptBudgetList: {
        contractGoodsInfo: {
          goodsName: '',
        },
        contractProcessInfo: {},
      },
      pickerBeginDateBefore: {
        disabledDate: (time) => {
          return time.getTime() > Date.now()
        },
      },
      // selectpackingMethod: {},
      // selectgrade: {},
      // selectunitList: {},
      // selectDuty: {},
      // isEdited: false,
      // registered: {},
      // selectCrtDuty: {},
      // dutyList: {},
      // sexList: {},
      ruleDeptBudget: {
          contractNo: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
          ],
          
      },
      selectIntendedShip: {},
      interviewTypeList: {},
    }
  },
  activated() {
    //cg.viewBudget
    //cg.viewSpareMoney
    // this.getVesselData();
    this.loaddata()
    this.showType = this.isShow
  },
  methods: {
    // 关闭 dialog时 处理文件url 初始化upload组件
    handleClose() {
      this.dialogViewSpareMoney = false
    },
    returnsales() {
      this.$router.push({ path: 'purchaseContract' })
    },
    loaddata() {
      // 包装方式
      packList({ constId: 'CON1' })
        .toPromise()
        .then((response) => {
          this.packtypeList = response
        })
      // 验收方式
      this.getUnitList()
      // 货名
      packList({ constId: 'CON2' })
        .toPromise()
        .then((response) => {
          this.goodnameList = response
        })
      // 品级
      packList({ constId: 'CON3' })
        .toPromise()
        .then((response) => {
          this.gradeList = response
        })
      // 双章
      packList({ constId: 'CON4' })
        .toPromise()
        .then((response) => {
          this.ChapterTwoList = response
        })
    },
    // 上传附件
    uploadSuccess(data, files, url) {
      console.log(data, files, url)

      // this.deptBudgetList.
      // this.formData.append('files', files)
      // this.feedbackObj.uploadNameAttachment = data.appendixName
      // this.feedbackObj.pathUploadAttachment = data.appendixPath
      // // this.newAppendixs = files
      // this.onChangeFlag = true
    },
    onChange(files) {
      this.fileNum = files
      this.$refs.upload.handleSaveBill().then((res) => {
        console.log(files)
      })
    },
    getUnitList() {
      xiala({
        compId: sessionStorage.getItem('ws-pf_compId'),
        constCode: 'TYPEYAN',
      })
        .toPromise()
        .then((response) => {
          this.unitList = response
          let currItem
          this.unitList.forEach((item, index, arr) => {
            item.flag = 'delete'
            if (this.vModel == item.constKey) {
              currItem = item
            }
          })
          //
          if (currItem) {
            this.selectContract(currItem.constValue)
          }
        })
    },
   
    submit() {
      if(!this.deptBudgetList.contractNo){
        this.$message({
            message: '请输入合同编号',
            type: 'warning'
          });
        return
      }
     
      if(!this.deptBudgetList.shippingType){
        this.$message({
          message:'请输入运输方式！',
          type:'warning'
        });
        return
      }
      
      if(!this. deptBudgetList.buyer){
        this.$message({
          message:'请输入买方名称！',
          type:'warning'
        });
        return
      }
      if(!this.deptBudgetList.seller){
        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.sellerPhone){
        this.$message({
          message:'请输入卖方电话！',
          type:'warning'
        });
        return
      }
       if(isNaN(this.deptBudgetList.sellerPhone)){
        this.$message({
          message:'输入卖方电话有误！',
          type:'warning'
        });
        return
      }
      if(!this. deptBudgetList.settlementMethod){
        this.$message({
          message:'请输入结算方式！',
          type:'warning'
        });
        return
      }
     
      if(!this.deptBudgetList.sourceGoods){
        this.$message({
          message:'请输入货源地！',
          type:'warning'
        });
        return
      }
      if(!this.deptBudgetList.placeDelivery){
        this.$message({
          message:'请输入交货地！',
          type:'warning'
        });
        return
      }
      if(!this.deptBudgetList.weight){
        this.$message({
          message:'请输入重量！',
          type:'warning'
        });
        return
      }
       if(isNaN(this.deptBudgetList.weight)){
        this.$message({
          message:'输入重量有误！',
          type:'warning'
        });
        return
      }
       if(!this.deptBudgetList.overShort && isNaN(this.deptBudgetList.overShort)){
        this.$message({
          message:'溢短装输入有误！',
          type:'warning'
        });
        return
      }
    if(!this.deptBudgetList.unitContractPrice && isNaN(this.deptBudgetList.unitContractPrice)){
        this.$message({
          message:'合同单价输入有误！',
          type:'warning'
        });
        return
      }
       if(!this.deptBudgetList.totalContractPrice && isNaN(this.deptBudgetList.totalContractPrice)){
        this.$message({
          message:'合同总价输入有误！',
          type:'warning'
        });
        return
      }
      //时间
        if(this.deptBudgetList.deliveryDateStart>this.deptBudgetList.deliveryDateEnd){
           this.$message({
          message:'时间输入有误！',
          type:'warning'
        });
        return
        }
      this.$refs.deptBudgetList.validate((valid) => {
        if (valid) {
          this.deptBudgetList.compId = this.compId
          this.deptBudgetList.contractType = 1
          addList(this.deptBudgetList)
            .toPromise()
            .then((response) => {
              this.$message.success('添加成功')
              this.$router.push({ path: 'salesContract' })
            })
        } else {
          EventBus.$emit('error', this.$t('showMessage.asteriskRequired'))
          return false
        }
      })
      
    },
     resetForm(deptBudgetList) {
        this.$refs[deptBudgetList].resetFields();
      },
    saveClick(item, index) {
      console.log(item)

      if (Object.is(item.id, 1)) {
        return
      }
      if (Object.is(this.unitList[index].flag, 'delete')) {
        this.$set(this.unitList, index, { flag: 'check' })
      } else {
        this.$set(this.unitList, index, { flag: 'delete' })
      }
      if (!item.constValue) {
        this.unitList.splice(index, 1)
        return
      }
      if (item.flag == 'add') {
        item.constKey = Math.random() * 20
        this.trainingMethods.compId = sessionStorage.getItem('ws-pf_compId')
        this.trainingMethods.constKey = item.constKey
        this.trainingMethods.constCode = 'TYPEYAN'
        this.trainingMethods.constValue = item.constValue
        this.trainingMethods.id = item.id
        addxiala(this.trainingMethods)
          .toPromise()
          .then((response) => {
            this.getUnitList()
          })
      } else if (item.flag == 'check') {
        this.trainingMethods.compId = sessionStorage.getItem('ws-pf_compId')
        this.trainingMethods.constKey = item.constKey
        this.trainingMethods.constCode = 'TYPEYAN'
        this.trainingMethods.constValue = item.constValue
        this.trainingMethods.id = item.id
        editxiala(this.trainingMethods)
          .toPromise()
          .then((response) => {
            this.getUnitList()
          })
      }
    },
    // selectContract(val) {
    //   let key = this.unitList.find((item) => item.constValue === val).constKey
    //   this.pleaseChoose = val
    // },

    handleExamine() {},
    approve() {},
    // returnsales() {
    //   this.$router.push({ path: 'purchaseContract' })
    // },
    selectChapterTwo(e) {
      for (var i = 0; i < this.ChapterTwoList.length; i++) {
        if (this.ChapterTwoList[i].constValue == e) {
          this.deptBudgetList.gradeKey = this.ChapterTwoList[i].constKey
        }
      }
    },
    selectunitList(e) {
      for (var i = 0; i < this.unitList.length; i++) {
        if (this.unitList[i].constValue == e) {
          this.deptBudgetList.packingMethodKey = this.unitList[i].constKey
        }
      }
    },
    selectgrade(e) {
      for (var i = 0; i < this.gradeList.length; i++) {
        if (this.gradeList[i].constValue == e) {
          this.deptBudgetList.gradeKey = this.gradeList[i].constKey
        }
      }
    },
    selectgoodsName(e) {
      for (var i = 0; i < this.goodnameList.length; i++) {
        if (this.goodnameList[i].constValue == e) {
          this.deptBudgetList.goodsNameKey = this.goodnameList[i].constKey
        }
      }
    },
    selectpackingMethod(e) {
      for (var i = 0; i < this.packtypeList.length; i++) {
        if (this.packtypeList[i].constValue == e) {
          this.deptBudgetList.acceptanceMethodKey = this.packtypeList[
            i
          ].constKey
        }
      }
    },
    // 编辑
    editClick(item, index) {
      const map = JSON.parse(JSON.stringify(item))
      if (Object.is(item.id, 1)) {
        return
      }
      if (Object.is(this.unitList[index].flag, 'delete')) {
        map.flag = 'check'
        this.$set(this.unitList, index, map)
      } else {
        map.flag = 'delete'
        this.$set(this.unitList, index, map)
      }
    },
    // 删除
    deleteClick(item, index) {
      if (Object.is(item.constKey, 1)) {
        return
      }
      if (!item.constValue) {
        this.unitList.splice(index, 1)
        return
      }
      delxiala({ id: this.unitList[index].id })
        .toPromise()
        .then((response) => {
          this.getUnitList()
          this.pleaseChoose = ''
        })
    },
  },
}
</script>

<style lang="scss" scoped>
.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;
}
.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: 20px 0px;
}
.wenzi {
  width: 900px;
  margin: 0 auto;
}
.wenzi h3 {
  display: inline-block;
  left: 10px;
}
.wenzi p {
  display: inline-block;
}
.center {
  width: 900px;
  margin: 0 auto;
}
.el-form-item {
  width: 50%;
}
.el-form-item__label {
  text-align: center;
}
.ce {
  width: 900px;
  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;
    }
  }
}

// 控制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: 10px 0 20px;
}
.container {
  overflow: scroll;
  height: 93vh;
}
.unchangeable {
  position: absolute;
  width: 9px;
  height: 22px;
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #ff2727;
  line-height: 22px;
  display: inline-block;
  right: 14px;
}
/deep/.el-input,
/deep/.el-date-editor {
  font-size: 13px;
  width: 232px;
  left: 21px;
}
/deep/.el-textarea {
  width: 900px;
  margin: 0px;
}
/deep/el-date-editor--date {
  width: 200px;
}
/deep/.ws-info-table[data-v-850a44a6] .el-form-item .el-form-item__labe{
  width: 130px;
}
}
</style>