<template>
  <div class="app-container">
    <div class="info">
      <!--<div class="avatar" v-show="onlyFlag">
        <img src="~@/assets/system/systemAvatar.svg" alt="">
        <span>{{ form.orgName}}</span>
      </div>-->
      <div class="title">企业信息</div>
      <el-form ref="form" :model="form" label-width="120px" label-position="left">
        <el-row :gutter="20">
          <el-col :span="11">
            <el-form-item label="粮库名称">
              <el-input v-model="form.orgName" :readonly="onlyFlag"></el-input>
            </el-form-item>
            <el-form-item label="行业">
              <el-input v-model="form.orgTrade" :readonly="onlyFlag"></el-input>
            </el-form-item>
            <el-form-item label="联系人">
              <el-input v-model="form.orgContacts" :readonly="onlyFlag"></el-input>
            </el-form-item>
            <el-form-item label="所在地区">
              <el-input v-model="form.orgArea" :readonly="onlyFlag"></el-input>
            </el-form-item>
            <el-form-item label="企业简介">
              <el-input v-model="form.orgIntroduction" :readonly="onlyFlag"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11" :offset="2">
            <el-form-item label="识别码">
              <el-input v-model="form.orgCode" :readonly="onlyFlag"></el-input>
            </el-form-item>
            <el-form-item label="企业规模">
              <el-input v-model="form.orgScale" :readonly="onlyFlag"></el-input>
            </el-form-item>
            <el-form-item label="邮政编码">
              <el-input v-model="form.zipCode" :readonly="onlyFlag"></el-input>
            </el-form-item>
            <el-form-item label="详细地址">
              <el-input v-model="form.detailedAddress" :readonly="onlyFlag"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <footer class="centerStyle">
        <el-button type="primary" @click="save" :loading='loading' v-show="!onlyFlag">保 存</el-button>
      </footer>
    </div>
  </div>
</template>

<script>
  // import {getUserInfo} from '@/api/system/index.js'
  import {getPageList, saveOrgInfo, updateById} from '@/api/unitInfo/index.js'

  export default {
    data() {
      return {
        form: {
          id: '',
          orgName: '',
          orgTrade: '',
          orgContacts: '',
          orgArea: '',
          orgIntroduction: '',
          orgCode: '',
          orgScale: '',
          zipCode: '',
          detailedAddress: ''
        },
        onlyFlag: null,
        loading: false
      }
    },
    created() {
      // console.log(this.$route.query, 'query')
      // this.userInfo = JSON.parse(localStorage.getItem("userInfo"))
    },
    mounted() {
      // this.getInfo()
      // console.log(this.userInfo,'userInfo')
      this.tableDataAdd();
    },
    methods: {
      // 表单加载
      tableDataAdd() {
        // 只读标记
        this.onlyFlag = this.$route.params.onlyFlag;
        // 修改,详情赋值
        this.form = this.$route.params.orgInfo;
      },
      // 新增接口方法
      save() {
        this.loading = true;
        let id = this.form.id;
        if (id.length === 0) {
          this.saveData(this.form);
        } else {
          this.updateData(this.form);
        }
      },
      saveData(data) {
        saveOrgInfo(data).then(res => {
          if (res.code === 200) {
            this.$message.success("新增成功");
            this.$router.push({
              name: "UnitInfo"
            })
          } else {
            this.loading = false
          }
        }).catch(err => {
          this.loading = false;
          this.$message.error("新增失败");
          console.log(err);
        });
      },
      updateData(data) {
        updateById(data).then(res => {
          if (res.code === 200) {
            this.$message.success("修改成功");
            this.$router.push({
              name: "UnitInfo"
            })
          } else {
            this.loading = false
          }
        }).catch(err => {
          this.loading = false;
          this.$message.error("修改失败");
          console.log(err);
        });
      }
      /*getInfo() {
        getUserInfo(this.userInfo.userName).then(res => {
          console.log(res,'userinfo')
          if(res.code == 200) {
            console.log(res,'用户信息')
            this.form = res.data
          }
        }).catch(err =>{
          console.log(err)
        })
      }*/
    }
  }
</script>

<style lang='scss' scoped>
  .centerStyle {
    text-align: center;
  }

  .info {
    background-color: #fff;
    border-radius: 15px;
    padding: 10px 15px;
    height: calc(100vh - 110px);
    .avatar {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      align-items: center;
      margin-bottom: 20px;
    }
    .title {
      border-left: 4px solid #4a7de6;
      padding-left: 10px;
    }
    .el-form {
      margin-top: 15px;
      padding: 15px;
    }
  }

  .table {
    border-radius: 0.185185rem;
  }

  .th {
    height: 1.074074rem;
    line-height: 1.074074rem;
    background-color: #1d9eef;
    text-align: center;
    // border: 1px solid #000;
  }

  .list-tr {
    height: 1.074074rem;
    line-height: 1.074074rem;
    border: 1px solid #000;
  }

  td {
    text-align: center;
  }

  ::v-deep .el-input__inner {
    text-align: center;
  }
</style>
