<template>
<!--pages/release/companyAuth/index.wxml-->
<view class="auth-page">
  <view class="company-info-box">
    <view class="form-item acea-row row-between row-middle">
      <view class="label">姓名</view>
      <view class="inp-box">
        <input type="text" :value="form.legalPersonName" @input="inpChange" data-type="name" placeholder="请填写姓名">
      </view>
    </view>
    <view class="form-item acea-row row-between row-middle">
      <view class="label">手机号</view>
      <view class="inp-box">
        <input type="text" :value="form.legalPhone" @input="inpChange" data-type="phone" placeholder="请填写手机号">
      </view>
    </view>
    <view class="form-item acea-row row-between row-middle">
      <view class="label">身份证号</view>
      <view class="inp-box">
        <input type="text" :value="form.legalPersonIdcard" @input="inpChange" data-type="idcard" placeholder="请填写身份证号">
      </view>
    </view>
    <!-- <view class="form-item noBorder">
      <view class="label">身份证</view>
      <view class="inp-box sfz">
        <view class="acea-row row-between-wrapper">
          <image src="https://download.cyjyyjy.com/sfz1.png" @tap="upCard" data-type="b" v-if="form.idcardPicBehind == ''"></image>
          <image :src="form.idcardPicBehind" @tap="upCard" data-type="b" v-else></image>
          <image src="https://download.cyjyyjy.com/sfzb.png"></image>
        </view>
        <view class="acea-row row-between-wrapper">
          <image src="https://download.cyjyyjy.com/sfz2.png" @tap="upCard" data-type="f" v-if="form.idcardPicFront == ''"></image>
          <image :src="form.idcardPicFront" @tap="upCard" data-type="f" v-else></image>
          <image src="https://download.cyjyyjy.com/sfzz.png"></image>
        </view>
        <view class="acea-row row-between-wrapper">
          <image src="https://download.cyjyyjy.com/sfz3.png" @tap="upCard" data-type="h" v-if="form.idcardPicHold == ''"></image>
          <image :src="form.idcardPicHold" @tap="upCard" data-type="h" v-else></image>
          <image src="https://download.cyjyyjy.com/sfzs.png"></image>
        </view>
      </view>
    </view> -->
  </view>
  <view class="submit-btn" @tap="submit">保存并提交</view>
</view>
</template>

<script>
// pages/release/personAuth/index.js
const util = require("../../../utils/util.js");
const app = getApp();

export default {
  data() {
    return {
      form: {
        legalPersonName: '',
        legalPhone: '',
        legalPersonIdcard: '',
        idcardPicFront: '',
        idcardPicBehind: '',
        idcardPicHold: ''
      },
      isEdit: false
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.authInfo) {
      let authInfo = JSON.parse(decodeURIComponent(options.authInfo));
			this.isEdit = true,
      this.form.authorizationType = 2;
      this.form.id = authInfo.id;
      this.form.idcardPicHold = authInfo.idcardPicHold;
      this.form.idcardPicBehind = authInfo.idcardPicBehind;
      this.form.idcardPicFront = authInfo.idcardPicFront;
      this.form.legalPersonIdcard = authInfo.legalPersonIdcard;
      this.form.legalPhone = authInfo.legalPhone || '';
      this.form.legalPersonName = authInfo.legalPersonName;
    }
  },
  methods: {
    inpChange(e) {
      let type = e.currentTarget.dataset.type;
      let value = e.detail.value;

      if (type == 'name') {
        this.setData({//['form.legalPersonName']: value
        });
        //try fix
        this.form.legalPersonName = value;
      } else if (type == 'phone') {
        this.setData({//.form.legalPhone: value
        });
        //try fix
        this.form.legalPhone = value;
      } else if (type == 'idcard') {
        this.setData({//.form.legalPersonIdcard: value
        });
        //try fix
        this.form.legalPersonIdcard = value;
      }
    },

    upCard(e) {
      let type = e.currentTarget.dataset.type;
      util.chooseImages(img => {
        if (type == 'f') {
          this.setData({//['form.idcardPicFront']: img
          });
          //try fix
          this.form.idcardPicFront = img;
        } else if (type == 'b') {
          this.setData({//.form.idcardPicBehind: img
          });
          //try fix
          this.form.idcardPicBehind = img;
        } else {
          this.setData({//.form.idcardPicHold: img
          });
          //try fix
          this.form.idcardPicHold = img;
        }
      });
    },

    submit() {
      let form = this.form;

      if (form.legalPersonName == '') {
        uni.showToast({
          title: '请填写姓名',
          icon: 'none'
        });
        return;
      } else if (form.legalPhone == '') {
        uni.showToast({
          title: '请填写手机号',
          icon: 'none'
        });
        return;
      } else if (form.legalPersonIdcard == '') {
        uni.showToast({
          title: '请填写身份证号',
          icon: 'none'
        });
        return;
      } else {
        if (this.isEdit == false) {
          app.http('post', 'EnterpriseAuthentication/createAuthenticInfoOne', this.form).then(res => {
            if (res.data.success) {
              uni.showModal({
                title: '提示！',
                content: '提交成功！请等待审核',

                success(res) {
                  uni.navigateBack({
                    delta: 1
                  });
                }

              });
            } else {
              uni.showToast({
                title: res.data.msg,
                icon: 'none'
              });
            }
          });
        } else {
          app.http('post', 'EnterpriseAuthentication/editAuthenticInfo', this.form).then(res => {
            if (res.data.success) {
              uni.showModal({
                title: '提示！',
                content: '提交成功！请等待审核',

                success(res) {
                  uni.navigateBack({
                    delta: 1
                  });
                }

              });
            } else {
              uni.showToast({
                title: res.data.msg,
                icon: 'none'
              });
            }
          });
        }
      }
    }

  }
};
</script>
<style>
/* pages/release/personAuth/index.wxss */
@import "../companyAuth/index.css";
</style>