<template>
  <div>
    <van-nav-bar
      title="修改许可证"
      left-arrow
      fixed
      placeholder
      @click-left="$router.back()"
      z-index="998"
    >
    </van-nav-bar>

    <div>
      <van-form ref="form">
        <van-field v-model="licenseForm.licensePermit.year" required label="年度" placeholder="年度" input-align="right" type="digit" label-width="auto" :rules="[{ pattern: /^\d+$/ }]"/>
        <van-field v-model="licenseForm.licensePermit.licenseKey" required label="许可证号" placeholder="许可证号" input-align="right" label-width="auto" :rules="[{required: true}]"/>
        <van-field v-model="licenseForm.licensePermit.issuingAuthority" required label="发证机关" placeholder="发证机关" input-align="right" label-width="auto" :rules="[{required: true}]"/>
        <field-date-picker
          v-model="licenseForm.licensePermit.issueDate"
          label="发证日期"
          placeholder="发证日期"
          :rules="[{ required: true }]"
          formatter="yyyy-MM-dd"
          required
        />
        <van-field v-model="licenseForm.licensePermit.constructionUnit" required label="建设单位" placeholder="建设单位" input-align="right" label-width="auto" :rules="[{required: true}]"/>
        <van-field v-model="licenseForm.licensePermit.constructionItem" required label="建设项目名称" placeholder="建设项目名称" input-align="right" label-width="auto" :rules="[{required: true}]"/>
        <van-field v-model="licenseForm.licensePermit.constructionLocation" required label="建设位置" placeholder="建设位置" input-align="right" label-width="auto" :rules="[{required: true}]"/>
        <van-field v-model="licenseForm.licensePermit.constructionScale" required label="建设规模" placeholder="建设规模" input-align="left" :rules="[{required: true}]"
                   rows="2"
                   :autosize="true"
                   type="textarea"/>
      </van-form>
    </div>

    <van-goods-action style="z-index: 999;">
        <van-goods-action-button type="info" text="关闭" @click="close()"/>
        <van-goods-action-button type="info" text="保存" @click="submit()"/>
    </van-goods-action>
  </div>
</template>

<script>
import {updateRatification, updatePermit} from "@/api/onlineHome/homestead/license";
import {selectAllLicense} from "@/api/onlineHome/homestead/houseProposer";
import FieldDatePicker from "@/components/form/FieldDatePicker";
import {Notify} from "vant";

export default {
  name: "licensePermitEdit",
  components: {FieldDatePicker},
  props: ['id'],
  data() {
    return {
      proposerId: null,
      licenseForm:{
        "licensePermit": {
          "searchValue": null,
          "createBy": "",
          "createTime": "",
          "updateBy": "",
          "updateTime": null,
          "proposerId": null,
          "applyProposerId": null,
          "year": null,
          "licenseKey": null,
          "issuingAuthority": null,
          "issueDate": null,
          "constructionUnit": null,
          "constructionItem": null,
          "constructionLocation": null,
          "constructionScale": null,
          "locationPlan": null
        },
        "licenseRatification": {
          "searchValue": null,
          "createBy": "",
          "createTime": null,
          "updateBy": "",
          "updateTime": null,
          "proposerId": null,
          "applyProposerId": null,
          "year": null,
          "approvalNumber": null,
          "issuingAuthority": null,
          "issueDate": null,
          "memberName": null,
          "landArea": 0,
          "landOwner": null,
          "landUse": null,
          "location": null,
          "east": null,
          "west": null,
          "south": null,
          "north": null,
          "startTime": null,
          "endTime": null,
          "remark": null,
          'fjzdmj': 0, // 房基占地面积
        },
      },
      options: {
      },
    };
  },
  created() {
    this.proposerId = this.$route.query.id;
    this.getDetail();
  },
  methods: {
    getDetail() {
      selectAllLicense(this.proposerId).then((response) => {
        this.licenseForm = response.data;
      }).catch((e) => {
        this.$toast.fail('获取证书失败!');
      });
    },
    close() {
      this.$router.back();
    },
    submit() {
      this.$refs.form.validate().then(() => {
        updatePermit(this.licenseForm.licensePermit).then(response => {
          this.notify('保存成功', 'success');
          this.$router.back();
        }).catch((e) => {
          this.notify('保存失败！', 'danger');
        });
      });
    },
    notify(message, type) {
      Notify.clear();
      Notify({ type: type || 'primary', message: message });
    },
  },
}
</script>

<style scoped>
.van-goods-action-button--first {
  border-radius: 0.053333rem;
}
.van-goods-action-button--last {
  border-radius: 0.053333rem;
}
.van-goods-action-button {
  margin-left: 0.1rem;
}
</style>
