<template>
  <div class="app-container">
    <div>
      <div class="tab-header webtop">
        <div>{{ title }}</div>
        <div>
          <el-button type="primary" size="small" :disabled="submitdisabled" @click="saveOrUpdate">保存</el-button>
          <el-button type="primary" size="small" :disabled="submitdisabled" @click="saveSubmit">提交</el-button>
        </div>
      </div>
      <div class="" style="padding-bottom: 50px;">
        <el-form ref="dataForm" :model="formobj" label-position="top" label-width="190px" class="formadd">
          <div class="titwu">货物解货质押出库</div>
          <el-row style="border-top: 1px solid #e0e3eb">
            <el-col :span="4" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">项目名称</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item class="trightb_item">
                <el-select v-model="formobj.projectName"  @change="projectChange"  placeholder="请选择" clearable >
                  <el-option v-for="(item,i) in projectSidList" :key="i" :label="item.entryName" :value="item.entryName"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">申请日期</span>
              </el-form-item>
            </el-col>
            <el-col :span="8" class="trightb_item">
              <el-form-item class="trightb_item">
                <el-date-picker v-model="formobj.createTime" type="date" style="width:100%" value-format="yyyy-MM-dd" placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="4" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">仓库名称</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item class="trightb_item">
                <el-input v-model="formobj.warehouseName" placeholder="" style="width:100%" class="addinputw" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="4" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">申请人</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item class="trightb_item">
                <el-input v-model="formobj.createByName" placeholder="" style="width:100%" class="addinputw" clearable />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="4" class="trightb">
              <el-form-item class="trightb_item">
                <span slot="label">货物价值</span>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item class="trightb_item">
                <el-input v-model="formobj.goodsValue" placeholder="" style="width:100%" class="addinputw" clearable />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import req from '@/api/Jzcksq/jzcksq'

export default {
  name: 'jzckEdit',
  data() {
    return {
      title: '',
      submitdisabled: false,
      projectSidList: [],
      formobj: {
        userSid: '',
        createByName: '',
        projectName: '',
        projectSid: '',
        warehouseName: '',
        warehouseSid: '',
        goodsValue: '',
        createTime: ''
      }
    }
  },
  created() {
    this.projectSidList = JSON.parse(window.sessionStorage.getItem('projectSidList'))
    console.log('url:' + window.location.href)
    var one = window.location.href.indexOf('&data') + 6
    const data = window.location.href.substr(one) // url解码unescape()已从web中移除,尽量不使用
    const obj = JSON.parse(decodeURIComponent(data))
    this.showInfo(obj.businessSid)
  },
  mounted() {
    window.parent.postMessage({
      cmd: 'returnHeight',
      params: {
        // 告诉父级页面，子页面的弹框高度。
        code: 2,
        data: 500 + 'px'
      }
    }, '*')
  },
  methods: {
    showInfo(sid) {
      this.title = '【编辑】货物解货质押出库'
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      req.fetchDetailsBySid({ sid: sid }).then((resp) => {
        if (resp.success) {
          this.formobj = resp.data
        }
      })
    },
    projectChange(value) {
      const choose = this.projectSidList.filter((item) => item.entryName === value)
      if (choose !== null && choose.length > 0) {
        this.formobj.projectSid = choose[0].sid
      } else {
        this.formobj.projectSid = ''
      }
    },
    saveOrUpdate() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.submitdisabled = true
          req.sealrecordSave(this.formobj).then((resp) => {
            if (resp.success) {
              this.$message({ showClose: true, type: 'success', message: '保存成功' })
              this.handleReturn('true')
            } else {
              this.submitdisabled = false
            }
          }).catch(() => {
            this.submitdisabled = false
          })
        }
      })
    },
    saveSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.submitdisabled = true
          req.sealrecordSubmit(this.formobj).then((resp) => {
            if (resp.success) {
              this.$message({ showClose: true, type: 'success', message: '提交成功' })
              this.handleReturn('true')
            } else {
              this.submitdisabled = false
            }
          }).catch(() => {
            this.submitdisabled = false
          })
        }
      })
    },
    handleReturn(isreload) {
      if (isreload === 'true') this.$emit('reloadlist')
      this.formobj = {
        userSid: '',
        createByName: '',
        projectName: '',
        projectSid: '',
        warehouseName: '',
        warehouseSid: '',
        goodsValue: '',
        createTime: ''
      }
      this.submitdisabled = false
      this.$refs['dataForm'].resetFields()
      this.$emit('doback')
    },
  }
}
</script>

<style scoped>
.trightb {
  display: flex;
  align-items: center;
  justify-content: center;
}

.trightb_item {
  padding-top: 5px;
}

.span {
  margin-left: 50px;
  font-size: 15px;
  font-weight: 400;
}

.titwu {
  font-size: 28px;
  text-align: center;
  padding: 30px 0 20px 0;
}
</style>
