<template>
  <div class="app-container" :style="{height:height+'px'}">
    <van-nav-bar
      title="修改信息"
      left-arrow
      fixed
      placeholder
      @click-left="onClickLeft"
    />
    <van-form @submit="getSubmit">
      <van-field v-model="form.phone+' （ 不可修改 ）'" type="tel" readonly label="手机号" placeholder="请输入手机号" required :rules="[{ required:true }]" />
      <van-field v-model="form.companyName" label="企业名称" placeholder="请输入企业名称"/>
      <van-field v-model="form.uniformCode" label="社会信用代码" placeholder="请输入社会信用代码"/>
      <van-field v-model="form.legalName" label="法人/负责人" placeholder="请输入法人/负责人"/>
      <van-field
        v-model="deptName"
        is-link
        readonly
        name="area"
        label="所在地区"
        placeholder="请选择所在地区"
        @click="showArea = true"
      />
      <van-popup v-model:show="showArea" position="bottom">
        <van-picker
          show-toolbar
          value-key="label"
          :columns="deptOptions"
          @confirm="onConfirmAdress"
          @cancel="showArea = false"
        />
      </van-popup>
      <van-field v-model="form.companyBranch" label="企业分支机构" placeholder="请输入企业分支机构"/>
      <van-field v-model="form.address" label="详细地址" placeholder="请输入详细地址" />
      <van-field v-model="form.investor" label="企业投资人" placeholder="请输入企业投资人" />
      <van-field v-model="form.businessMode" label="经营方式" placeholder="请输入经营方式" />
      <van-field
        v-model="businessTypeText"
        is-link
        readonly
        label="经营类别"
        placeholder="请选择经营类别"
        @click="showBusiness = true"
      />
      <van-popup v-model:show="showBusiness" position="bottom">
        <van-picker
          title="经营类别"
          show-toolbar
          :columns="businessTypeOptions"
          @confirm="onConfirmBusiness"
          value-key="dictLabel"
          @cancel="showBusiness = false"
        />
      </van-popup>

      <van-field v-model="form.businessScope" label="经营范围" placeholder="请输入经营范围" />

      <div class="submit">
        <van-button round block type="info" color="#007E72" native-type="submit">保存</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import { logout ,getInfo } from "@/api/login/index";
import { userRegister } from "@/api/register/index";
import { companyList,treeselectUser , updateCompany } from "@/api/lawEnforcement/index";
import Cookies from "js-cookie";
export default {
  name: "companyRegister",
  data() {
    return {
      height:0,
      tel:'',
      value:'',
      confirmPassWord:'',
      businessTypeText:'',
      showBusiness:false,
      // 经营类别
      businessTypeOptions: [],
      showPicker: false,
      columns:[],
      deptName:'',
      showArea:false,
      // 部门列表
      deptOptions: [],
      form: {},
      fieldNames : {
        text: 'label',
        value: 'value',
        children: 'children',
      },
      codeUrl:''
    };
  },
  created() {
    this.getCode();
    this.getDicts("economic_type").then(res => {
      res.data.map(item => {
        this.columns.push(item.dictLabel);
      });
    });
    /** 查询部门下拉树结构 */
    treeselectUser().then((response) => {
      this.deptOptions = response.data;
      this.getInformation();
    });

    this.getDicts("business_type").then(response => {
      this.businessTypeOptions = response.data;
    });
    this.height = document.body.clientHeight
  },
  methods: {
    getInformation(){
      let data = {
        userId:this.$route.query.userId,
      }
      companyList(data).then((res) => {
        for (var i = 0 ; i < this.deptOptions.length ; i ++ ){
          if (this.deptOptions[i].id == res.data.deptId){
            this.deptName = this.deptOptions[i].label;
          }else{
            for (var j = 0 ; j < this.deptOptions[i].children.length ; j++){
              if (this.deptOptions[i].children[j].id == res.data.deptId){
                this.deptName = this.deptOptions[i].children[j].label;
              }else{
                for (var z = 0 ; z < this.deptOptions[i].children[j].children.length ; z++){
                  if (this.deptOptions[i].children[j].children[z].id == res.data.deptId){
                    this.deptName = this.deptOptions[i].children[j].children[z].label;
                  }
                }
              }
            }
          }
        }
        this.form = res.data;
        this.businessTypeText = this.selectDictLabel(this.businessTypeOptions, res.data.businessType);
      });
    },
    onConfirmBusiness(value){
      this.showBusiness = false;
      this.businessTypeText = value.dictLabel;
      this.form.businessType = value.dictValue;
    },
    onConfirmAdress(value){
      console.log(value)
      this.showArea = false;

      this.deptName = value[0]+'/'+value[1]+'/'+value[2]

      let array1 = eval(this.deptOptions).filter(function (e) { return e.label == value[0]; });
      let array2 = eval(array1[0].children).filter(function (e) { return e.label == value[1]; });
      let array3 = eval(array2[0].children).filter(function (e) { return e.label == value[2]; });

      this.form.deptId = array3[0].value
    },
    getCode() {
      getCodeImg().then((res) => {
        this.form.uuid = res.uuid;
        this.codeUrl = "data:image/gif;base64," + res.img;
      });
    },
    onConfirm(value, index) {
      this.value = value;
      this.showPicker = false;
      this.form.supplyDemandType = index + 1 ;
    },
    getSubmit(){
      console.log(this.form)
      updateCompany(this.form).then(response => {

        this.$dialog.alert({
          title: '提示',
          message: "修改成功！即将退出登录，重新登录后修改生效！",
        }).then(() => {
          logout().then(response => {
            Cookies.remove("User-Token");
            this.$router.push({name:'lawEnforcementLogin'})
          });
        });
      });
    },
  },
};
</script>

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