<template>
  <view>
    <custom-nav-bar position="fixed" color="black" left-text="新增关联企业" left-arrow />

    <scroll-view :scroll-y="true" :style="'height: 470px;'">
      <view class="create-enterprise">
        <view class="border">
          <view class="create-enterprise-title">
            企业名称
            <text class="red ml-10">*</text>
          </view>
          <input class="create-enterprise-input" @input="headleInput" :value="form.enterpriseName"
            data-name="enterpriseName" type="text" placeholder="请输入企业名称" />
        </view>
        <view class="border">
          <view class="create-enterprise-title">
            法人姓名
            <text class="red ml-10">*</text>
          </view>
          <input class="create-enterprise-input" @input="headleInput" :value="form.legalName" data-name="legalName"
            type="text" placeholder="请输入法人姓名" />
        </view>
        <view class="border">
          <view class="create-enterprise-title">
            企业社会统一信用代码
            <text class="red ml-10">*</text>
          </view>
          <input class="create-enterprise-input" @input="headleInput" :value="form.identificationNumber"
            data-name="identificationNumber" type="text" placeholder="请输入企业社会统一信用代码" />
        </view>
      </view>
    </scroll-view>

    <view class="enterprise-button">
      <van-button v-if="isForm" @tap.native="submit" custom-style="height: 80rpx;" color="#E0383E"
        block>立即关联</van-button>
      <van-button v-if="!isForm" custom-style="height: 80rpx;" color="#C1C1C1" block>立即关联</van-button>
    </view>
  </view>
</template>

<script>
  // const app = getApp();
  // const { enterprise } = app.globalData.api;
  import * as enterprise from '@/api/enterprise'; // 企业
  export default {
    components: {},
    data() {
      return {
        // env: app.globalData.env,
        // appName: app.globalData.env.appName,

        form: {
          enterpriseName: '',
          legalName: '',
          identificationNumber: ''
        },

        isForm: false,
        windowHeight: null,
        name: ''
      };
    },
    async onLoad() {
      const res = uni.getSystemInfoSync();
      this.windowHeight = res.windowHeight
    },
    async onShow() {},
    methods: {
      // 返回首页企业
      onClickLeft() {
        uni.navigateTo({
          url: '/packages/enterprise/pages/relation-enterprise/relation-enterprise'
        });
      },

      // 输入框重新赋值
      headleInput(event) {
        let {
          value
        } = event.detail;
        const {
          name
        } = event.currentTarget.dataset;
 
        this.form[name] = value;
        this.form = {...this.form},
        this[name]= this[name]
        const {
          enterpriseName,
          identificationNumber,
          legalName
        } = this.form;
        if (enterpriseName !== '' && identificationNumber !== '' && legalName !== '') {
          this.isForm = true
        } else {
          this.isForm = false
        }
      },

      async submit() {
        const {
          enterpriseName,
          identificationNumber,
          legalName
        } = this.form;
        if (enterpriseName === '' || identificationNumber === '' || legalName === '') {
          uni.showToast({
            title: enterpriseName === '' ?
              '企业名称不能为空' : legalName === '' ?
              '法人姓名不能为空' : identificationNumber === '' ?
              '企业社会统一信用代码不能为空' : '其中一项不能为空',
            icon: 'none'
          });
          return false;
        }
        let params = {
          enterpriseName,
          legalName,
          identificationNumber
        };
        const res = await enterprise.createAffiliatedEnterprise(params);
        if (res.code !== 0) {
          uni.showToast({
            title: '添加关联企业失败',
            icon: 'none'
          });
          return;
        }
        uni.showToast({
          title: '添加企业成功',
          icon: 'none'
        });
        uni.redirectTo({
          url: '../relation-enterprise/relation-enterprise'
        });
        //获取已经打开的页面的数组
        var pages = getCurrentPages();
        //获取上一个页面的所有的方法和data中的数据
        var lastpage = pages[pages.length - 2];
        lastpage.getEnterpriseList();
      }
    }
  };
</script>
<style lang="less">
  .create-enterprise {
    padding: 136rpx 40rpx;
  }

  .border {
    padding: 42rpx 0;
    border-bottom: 1px solid #efefef;
  }

  .create-enterprise-title {
    font-size: 28rpx;

    font-weight: 400;
    color: #333333;
  }

  .create-enterprise-input {
    margin-top: 24rpx;
  }

  .ml-10 {
    margin-left: 10px;
  }

  .enterprise-button {
    padding: 25rpx 36rpx;
  }
</style>