<template>
  <div class="page">
    <div class="action">
      <span v-if="form.auditStatus !== ''"
            class="status">{{ auditStatus.find(e => e.id === form.auditStatus).name }}</span>
      <el-button v-if="!editing"
                 @click="edit">编辑</el-button>
    </div>
    <el-form label-width="170px"
             label-suffix=":"
             class="input-form model"
             size="small"
             :disabled="!editing">
      <el-form-item label="认证状态">
        <el-select v-model="form.authenticationStatus"
                   clearable>
          <el-option v-for="(item, i) in authenticationStatus"
                     :key="i"
                     :label="item.name"
                     :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="幼儿园名称">
        <div class="d-inline-block">
          <el-input v-model="form.companyName" />
        </div>
      </el-form-item>
      <el-form-item label="统一社会信用代码">
        <div class="d-inline-block">
          <el-input v-model="form.creditCode" />
        </div>
      </el-form-item>
      <el-form-item label="法人">
        <div class="d-inline-block">
          <el-input v-model="form.legalPerson" />
        </div>
      </el-form-item>
      <el-form-item label="营业执照">
        <img v-if="form.businessLicensePicture"
             class="pic"
             :src="form.businessLicensePicture"
             alt="">
      </el-form-item>
      <el-form-item label="办学许可证件">
        <img v-if="form.licenseForRunningSchool"
             class="pic"
             :src="form.licenseForRunningSchool"
             alt="">
      </el-form-item>
      <div class="aline"></div>

      <div class="info">
        <div class="field">
          <label>申请人</label>
          <p class="val">{{ form.account }}</p>
        </div>
        <div class="field">
          <label>联系方式</label>
          <p class="val">{{ form.contactInformation }}</p>
        </div>
        <div class="field">
          <label>提交时间</label>
          <p class="val">{{ form.submitTime }}</p>
        </div>
      </div>
    </el-form>

    <div class="btns">
      <el-button v-if="editing"
                 type="primary"
                 @click="submit">保存</el-button>
      <el-button @click="$router.back()">返回</el-button>
    </div>
  </div>
</template>

<script>
import Util from "@/libs/util";
import Setting from "@/setting";
import Const from '@/const/user'
export default {
  data () {
    return {
      id: this.$route.query.id,
      auditStatus: Const.auditStatus,
      authenticationStatus: [
        {
          id: 2,
          name: '已认证'
        },
        {
          id: 0,
          name: '未认证'
        },
      ],
      form: {
        companyName: '',
        creditCode: '',
        legalPerson: '',
        auditStatus: '',
        authenticationStatus: ''
      },
      editing: false,
      submiting: false,
    };
  },
  mounted () {
    this.$store.commit('user/setCrumbs', [
      {
        name: '幼儿园管理',
        route: '/preschool'
      },
      {
        name: '认证信息'
      },
    ])
    this.getData()
  },
  methods: {
    getData () {
      this.$post(`${this.api.enterpriseCertificationDetails}?id=${this.id}`).then(({ data }) => {
        if (data.authenticationStatus === 1) data.authenticationStatus = ''
        this.form = data
      }).catch(err => { })
    },
    // 编辑
    edit () {
      this.editing = true
    },
    // 提交
    async submit () {
      const { form } = this
      if (form.authenticationStatus === '') return Util.warningMsg('请选择认证状态')
      if (!form.companyName) return Util.warningMsg('请输入幼儿园名称')
      if (!form.creditCode) return Util.warningMsg('请输入统一社会信用代码')
      if (!form.legalPerson) return Util.warningMsg('请输入法人')
      if (this.submiting) return false
      this.submiting = true
      try {
        await this.$post(this.api.updateCertification, form).then(res => {
          Util.successMsg('保存成功！');
          this.$router.back()
        })
      } catch (e) {
        this.submiting = true
      }
    },
  }
};
</script>

<style scoped lang="scss">
.page {
    position: relative;
    padding-bottom: 80px;
    .action {
        z-index: 1;
        position: absolute;
        right: 50px;
        .status {
            margin-right: 10px;
            color: #2962ff;
        }
    }
}
.pic {
    max-width: 500px;
}
.input-form.model {
    height: calc(100vh - 310px);
}
.info {
    .field {
        display: flex;
        justify-content: space-between;
        width: 470px;
        margin: 10px 0;
    }
    label {
        font-size: 14px;
    }
    .val {
        font-size: 15px;
        font-weight: 600;
        color: #333;
    }
}
</style>