<template>
  <div class="app-container">
    <van-nav-bar
      title="资料修改"
      left-arrow
      fixed
      placeholder
      @click-left="onClickLeft"
    />
    <van-form @submit="submitForm">
      <van-field v-model="tel" readonly type="tel" label="手机号" placeholder="请输入手机号" required :rules="[{ required:true }]" >
        <template #extra >
          <p style="color: #007e72">修改手机号</p>
        </template>
      </van-field>
<!--      <van-field value="15314031621" label="密码" placeholder="请输入密码" required :rules="[{ required:true }]"/>-->
      <van-field v-model="userForm.companyName" label="公司名称" placeholder="请输入公司名称" required :rules="[{ required:true }]"/>
      <van-field v-model="userForm.companyCode" label="社会信用代码" placeholder="请输入公司的社会信用代码"/>

      <van-field
        readonly
        clickable
        label="企业性质"
        :value="companyNameValue"
        placeholder="选择企业性质"
        @click="showPickerPiker = true"
      />
      <van-popup v-model="showPickerPiker" round position="bottom">
        <van-picker
          show-toolbar
          :columns="companyNameList"
          @cancel="showPickerPiker = false"
          @confirm="onConfirmCompany"
        />
      </van-popup>
<!--      <van-field v-model="userForm.companyNature" label="企业性质" placeholder="请输入企业性质" required :rules="[{ required:true }]" />-->


      <van-field v-model="userForm.companyScope" label="经营范围" placeholder="请输入经营范围" />
      <van-field v-model="userForm.companyTimeLimit" label="经营期限" placeholder="请输入经营期限" />
      <van-field
        readonly
        clickable
        label="成立时间"
        :value="userForm.companySetupTime"
        placeholder="选择成立时间"
        @click="showPicker = true"
      />
      <van-popup v-model="showPicker" round position="bottom">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
          @cancel="showPicker = false"
          @confirm="onConfirm"
        />
      </van-popup>

      <!--<van-field v-model="userForm.companySetupTime" label="成立时间" placeholder="请输入成立时间" required :rules="[{ required:true }]" />-->
      <van-field v-model="userForm.companyCapital" label="注册资金" placeholder="请输入注册资金" />
      <van-field v-model="userForm.address" label="联系地址" placeholder="请输入联系地址" />
      <van-field v-model="userForm.realname" label="法人姓名" placeholder="请输入法人姓名" required :rules="[{ required:true }]"/>
      <van-field v-model="userForm.idCardNum" label="身份证号" placeholder="请输入法人身份证号" required :rules="[{ required:true }]"/>

      <van-field name="uploader" label="法人证件照" required>
        <template #input>
          <van-uploader :after-read="afterReadFRZJZ" :before-delete="deleteFileFRZJZ" v-model="fileListFRZJZ" multiple :max-count="1" />
        </template>
      </van-field>

      <van-field name="uploader" label="营业执照" required>
        <template #input>
          <van-uploader :after-read="afterReadYYZZ" :before-delete="deleteFileYYZZ" v-model="fileListYYZZ" multiple :max-count="1" />
        </template>
      </van-field>

      <van-field name="uploader" label="开户证明" required>
        <template #input>
          <van-uploader :after-read="afterReadKHZM" :before-delete="deleteFileKHZM" v-model="fileListKHZM" multiple :max-count="1" />
        </template>
      </van-field>
      <div class="submit">
        <van-button round block type="info" color="#007E72" native-type="submit" style="width: 90%;margin: 0 auto;">保存</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { getInfo } from "@/api/login/index";
import { getMember , base64Attach , userUpdate , showUserImg , deleteUserImg} from "@/api/user/index";
import {Dialog} from "vant";
import {base64Upload} from "@/api/register";
export default {
  name: "companyInformation",
  data() {
    return {
      height:0,
      tel:'',
      showPicker: false,
      showPickerPiker: false,
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(),
      value:'',
      companyNameValue:'',
      fileList:[],
      companyNameList:[],
      fileListKHZM:[],
      fileListFRZJZ:[],
      fileListYYZZ:[],
      form: {
        files:[],
        fileType:'',
        bizPath:'transaction',
        tableName:'t_transaction_member',
        tableId:''
      },
      userForm:{
        companyName:'',
        idCardNum:'',
        realname:'',
        companyCode:'',
        companyNature:'',
        companyScope:'',
        companyTimeLimit:'',
        companySetupTime:'',
        companyCapital:'',
        companyLicense:'',
        address:'',
        accountOpenCert:'',
      },
      userInfo:[],
      upLoadList:[]
    };
  },
  created() {
    this.height = document.body.clientHeight
    this.getInfo()
    this.getDicts("company_nature").then(res => {
      console.log(res);
      for (var i = 0 ; i < res.data.length ; i++ ){
        this.companyNameList.push(res.data[i].dictLabel)
      }
      //this.value = this.selectDictLabel(res.data,response.data.supplyDemandType);
    });
  },
  methods: {
    onConfirmCompany(value,index){
      console.log(value)
      this.companyNameValue = value;
      this.showPickerPiker = false;
      this.userForm.companyNature = index+1
      console.log(this.userForm.companyName)
    },
    onConfirm(value) {
      this.userForm.companySetupTime = this.getNowFormatDate(value).substr(0,10);
      console.log(new Date())
      this.showPicker = false;
    },
    getInfo(){
      getInfo().then(response => {
        getMember(response.user.userId).then(response => {
          console.log(response)

          // this.userForm.companyName = response.data.companyName;
          // this.userForm.idCardNum = response.data.idCardNum;
          // this.userForm.realname = response.data.realname;
          // this.userForm.companyCode = response.data.companyCode;
          // this.userForm.companyName = response.data.companyName;
          // this.userForm.companyNature = response.data.companyNature;
          // this.userForm.companyScope = response.data.companyScope;
          // this.userForm.companyTimeLimit = response.data.companyTimeLimit;
          // this.userForm.companySetupTime = response.data.companySetupTime;
          // this.userForm.companyCapital = response.data.companyCapital;
          // // this.userForm.companyLicense = response.data.companyLicense;
          // this.userForm.address = response.data.address;
          this.userForm = response.data;

          if (response.data.accountOpenCert!=''){
            this.fileListKHZM = [{
              url:'/api'+response.data.accountOpenCert,
              isImage: true
            }];
          }

          if (response.data.idCardPic!=''){
            this.fileListFRZJZ = [{
              url:'/api'+response.data.idCardPic,
              isImage: true
            }];
          }

          if (response.data.companyLicense!=''){
            this.fileListYYZZ = [{
              url:'/api'+response.data.companyLicense,
              isImage: true
            }];
          }

          this.tel = response.data.phone

          this.getDicts("company_nature").then(res => {
            console.log(res);
            this.companyNameValue = this.selectDictLabel(res.data,response.data.companyNature);
          });

          this.userForm.id = response.data.id;
          this.form.tableId = response.data.id;
          showUserImg(response.data.id).then(responseSecond => {
            console.log(responseSecond)
            this.form.files = responseSecond.data
            for (var i = 0 ; i < responseSecond.data.length ; i++){
              this.fileList.push({url:this.global.severApi+responseSecond.data[i].fileUrl,id:responseSecond.data[i].id})
            }
          });
        });
      });
    },
    afterReadFRZJZ(file) {
      // 此时可以自行将文件上传至服务器
      this.$toast.loading({
        message: "上传中...",
        forbidClick: true,
        duration: 0,
      });
      let params1 = {
        file:file.content
      }
      base64Upload(params1).then((r1) => {
        this.userForm.idCardPic = r1.fileName;
      })
    },
    deleteFileFRZJZ(elIndex){
      return (file, name) => {
        let fileIndex = name.index
        this.fileListFRZJZ[elIndex].splice(fileIndex, 1)
        this.userForm.idCardPic = '';
      }
    },
    afterReadKHZM(file) {
      // 此时可以自行将文件上传至服务器
      let params1 = {
        file:file.content
      }
      base64Upload(params1).then((r1) => {
        this.userForm.accountOpenCert = r1.fileName;
      })
    },
    deleteFileKHZM(elIndex){
      return (file, name) => {
        let fileIndex = name.index
        this.fileListKHZM[elIndex].splice(fileIndex, 1)
        this.userForm.accountOpenCert = '';
      }
    },
    afterReadYYZZ(file) {
      this.$toast.loading({
        message: "上传中...",
        forbidClick: true,
        duration: 0,
      });
      // 此时可以自行将文件上传至服务器
      let params1 = {
        file:file.content
      }
      base64Upload(params1).then((r1) => {
        this.form.companyLicense = r1.fileName;
      })
    },
    deleteFileYYZZ(elIndex){
      return (file, name) => {
        let fileIndex = name.index
        this.fileListYYZZ[elIndex].splice(fileIndex, 1)
        this.form.companyLicense = '';
      }
    },
    submitForm(){
      if (this.userForm.accountOpenCert == ''){
        this.$toast({
          icon: 'error', // 找到自己需要的图标
          message: '请上传开户证明',
          duration:"2000",
          onClose:function(){
          }
        })
        return;
      }
      if (this.userForm.companyLicense == ''){
        this.$toast({
          icon: 'error', // 找到自己需要的图标
          message: '请上传营业执照',
          duration:"2000",
          onClose:function(){
          }
        })
        return;
      }
      userUpdate(this.userForm).then(response => {
        if(response.code == 200) {
            Dialog.confirm({
              title: '系统提示',
              message: '修改成功',
              confirmButtonText: '确定',
            })
              .then(() => {
                this.onClickLeft()
              })
        }
      });
    }
  },
};
</script>

<style scoped lang="scss">
.app-container {
  background-color: #FFF;
  padding-bottom: 3rem;
}
.submit{
  position: fixed;
  bottom: 0;
  width: 100%;
  padding: 5% 0;
  background-color: #F9F9F9;
  p{
    text-align: center;
    margin-bottom: 0.2rem;
    span{
      color: #007E72;
    }
  }
}
</style>
