<template>
  <div>
    <div class="tab-header webtop">
      <div>挂靠公司信息</div>
      <div>
        <el-button :disabled="submitdisabled" size="small" type="primary" @click="saveOrUpdate">保存</el-button>
        <el-button size="small" type="info" @click="handleReturn()">关闭</el-button>
      </div>
    </div>
    <div class="listconadd">
      <el-form ref="form_obj" :model="formobj" :rules="rules" class="formaddcopy02">
        <el-row class="first_row">
          <el-col :span="24">
            <div class="span-sty"><span class="must">*</span> 公司名称</div>
            <el-form-item prop="corporateName">
              <el-input v-model="formobj.corporateName" class="addinputw addinputInfo" clearable placeholder="挂靠公司名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <div class="span-sty">联系人</div>
            <el-form-item>
              <el-input v-model="formobj.contacts" class="addinputw addinputInfo" clearable placeholder="联系人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <div class="span-sty">联系电话</div>
            <el-form-item>
              <el-input v-model="formobj.contactNumber" class="addinputw addinputInfo" clearable placeholder="联系电话"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import req from '@/api/affiliatedCompany/affiliatedCompany.js'
import { getOrgSidByPath } from '@/api/Common/dictcommons.js'

export default {
  components: {},
  data() {
    return {
      submitdisabled: false,
      formobj: {
        sid: '',
        corporateName: '',
        contacts: '',
        contactNumber: '',
        useOrgSid: window.sessionStorage.getItem('departmentSid'),
        orgPath: window.sessionStorage.getItem('defaultOrgPath'),
        createOrgSid: window.sessionStorage.getItem('orgSid'),
        userSid: window.sessionStorage.getItem('userSid'),
        createBySid: window.sessionStorage.getItem('userSid'),
        createByName: window.sessionStorage.getItem('name'),
        deptSid: window.sessionStorage.getItem('defaultOrgPath').substring(window.sessionStorage.getItem('defaultOrgPath').lastIndexOf('/') + 1),
        deptName: window.sessionStorage.getItem('defaultOrgPathName').substring(window.sessionStorage.getItem('defaultOrgPathName').lastIndexOf('/') + 1),

      },
      rules: {
        corporateName: [{ required: true, message: '挂靠公司名称不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    getDictValue() {
      getOrgSidByPath({ orgPath: window.sessionStorage.getItem('defaultOrgPath') }).then((res) => {
        if (res.success) {
          this.formobj.createOrgSid = res.data
        }
      })
    },
    saveOrUpdate() {
      console.log('>>>>>>>>>saveOrUpdate', this.formobj)
      this.$refs['form_obj'].validate((valid) => {
        if (valid) {
          req.saveAffiliatedcompany(this.formobj).then(resp => {
            if (resp.success) {
              this.$message({
                showClose: true,
                type: 'success',
                message: '保存成功'
              })
              this.handleReturn('true')
            } else {
              this.submitdisabled = false
            }
          }).catch(() => {
          })
        }
      })
    },
    handleReturn(isreload) {
      if (isreload === 'true') this.$emit('reloadlist')
      this.formobj = {
        sid: '',
        corporateName: '',
        contacts: '',
        contactNumber: '',
        useOrgSid: window.sessionStorage.getItem('departmentSid'),
        orgPath: window.sessionStorage.getItem('defaultOrgPath'),
        createOrgSid: window.sessionStorage.getItem('orgSid'),
        userSid: window.sessionStorage.getItem('userSid'),
        createBySid: window.sessionStorage.getItem('userSid'),
        createByName: window.sessionStorage.getItem('name'),
        deptSid: window.sessionStorage.getItem('defaultOrgPath').substring(window.sessionStorage.getItem('defaultOrgPath').lastIndexOf('/') + 1),
        deptName: window.sessionStorage.getItem('defaultOrgPathName').substring(window.sessionStorage.getItem('defaultOrgPathName').lastIndexOf('/') + 1),

      }
      this.$emit('doback')
    },
    showAdd() {
      this.getDictValue()
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
    },
    showEdit(row) {
      this.getDictValue()
      this.$nextTick(() => {
        this.$refs['form_obj'].clearValidate()
      })
      req.initAffiliatedcompany(row.sid).then(resp => {
        if (resp.success) {
          this.formobj = resp.data
        }
      }).catch(e => {
        this.formobj = row
      })
    }
  }
}
</script>
<style scoped>
/deep/ .el-form-item__error {
  margin-left: 120px !important;
}

.must {
  color: #f00;
}

.span-sty {
  width: 130px !important;
  align-items: center;
}

.addinputInfo {
  margin-left: 120px !important;
}

.formaddcopy02 .el-row .el-col /deep/ .el-form-item .addinputw {
  margin-left: 120px !important;
  width: calc(100% - 115px);
}

.first_row {
  border-top: 1px solid #E0E3EB;
}

.titleOne {
  padding: 7px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}

.formaddcopy02 .el-row .el-col /deep/ .el-form-item .el-radio-group {
  display: inline;
  line-height: 1px;
  vertical-align: middle;
}
</style>
