<template>
  <div class="app-container" :style="{height:height+'px'}">
    <van-nav-bar
      title="修改信息"
      left-arrow
      fixed
      placeholder
      @click-left="onClickLeft"
    />
    <van-form @submit="getSubmit" style="padding-bottom: 22%;">
      <van-field v-model="form.phone+' （ 不可修改 ）'" type="tel" readonly label="手机号" placeholder="请输入手机号" required :rules="[{ required:true }]" />
      <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.name" label="姓名" placeholder="请输入姓名"/>
      <van-field v-model="form.cardNum" label="身份证号" placeholder="请输入身份证号"/>
      <van-field name="radio" label="性别">
        <template #input>
          <van-radio-group v-model="form.sex" direction="horizontal">
            <van-radio name="1">男</van-radio>
            <van-radio name="2">女</van-radio>
          </van-radio-group>
        </template>
      </van-field>
      <van-field
        v-model="nationText"
        is-link
        readonly
        label="民族"
        placeholder="请选择民族"
        @click="showNation = true"
      />
      <van-popup v-model:show="showNation" position="bottom">
        <van-picker
          title="民族"
          show-toolbar
          :columns="nationOptions"
          @confirm="onConfirmNation"
          value-key="dictLabel"
        />
      </van-popup>
<!--      <van-field-->
<!--        v-model="form.code"-->
<!--        center-->
<!--        clearable-->
<!--        label="验证码"-->
<!--        placeholder="图形验证码"-->
<!--        required-->
<!--        :rules="[{ required:true }]"-->
<!--      >-->
<!--        <template #button>-->
<!--          <img class="code-img" :src="codeUrl" @click="getCode" />-->
<!--        </template>-->
<!--      </van-field>-->
<!--      <van-field v-model="form.password" type="password" label="密码" placeholder="请输入密码" required :rules="[{ required:true }]"/>-->
<!--      <van-field v-model="confirmPassWord" type="password" label="确认密码" placeholder="请再次输入密码" required :rules="[{ required:true }]"/>-->
      <van-field
        v-model="form.birthday"
        is-link
        readonly
        name="datetimePicker"
        label="出生日期"
        placeholder="请选择出生日期"
        @click="showDate = true"
      />
      <van-popup v-model:show="showDate" position="bottom">
        <van-datetime-picker
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
          @confirm="onConfirmBirthday"
          @cancel="showDate = false"
        />
      </van-popup>
      <van-field v-model="form.companyPosition" label="工作单位及职务" placeholder="请输入工作单位及职务"/>
      <van-field v-model="form.uniformCode" label="统一社会信用代码" placeholder="请输入统一社会信用代码"/>

      <van-field v-model="form.zihao" label="字号名称" placeholder="请输入字号名称"/>
      <van-field v-model="form.address" 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"
        />
      </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 { personalList,treeselectUser , updatePersonal } from "@/api/lawEnforcement/index";
import $ from "jquery";
import Cookies from "js-cookie";
// import { areaList } from '@vant/area-data';
export default {
  name: "userRegister",
  data() {
    return {
      height:0,
      tel:'',
      deptName:'',
      confirmPassWord:'',
      businessTypeText:'',
      areaList:[],
      showArea:false,
      showDate:false,
      showNation:false,
      showBusiness:false,
      minDate: new Date(1900, 1, 1),
      maxDate: new Date(),
      form: {
        sex:1
      },
      sex:'',
      codeUrl:'',
      nationText:'',
      // 民族字典
      nationOptions: [],
      // 部门列表
      deptOptions: [],
      // 经营类别
      businessTypeOptions: [],
    };
  },
  created() {
    this.getCode()
    this.height = document.body.clientHeight
    this.getDicts("nationality").then(response => {
      this.nationOptions = response.data;
    });
    /** 查询部门下拉树结构 */
    treeselectUser().then((response) => {
      this.deptOptions = response.data;
      this.getInformation();
    });

    this.getDicts("business_type").then(response => {
      this.businessTypeOptions = response.data;
    });

    this.getDicts("sex").then(response => {
      this.sexOptions = response.data;
    });
  },
  methods: {
    getInformation(){
      let data = {
        userId:this.$route.query.userId,
      }
      personalList(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.nationText = this.selectDictLabel(this.nationOptions, res.data.nation);
          this.businessTypeText = this.selectDictLabel(this.businessTypeOptions, res.data.businessType);
          this.sex = this.selectDictLabel(this.sexOptions, res.data.sex);
      });
    },
    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

      this.phoneChange();
    },
    onConfirmBirthday(value){
      console.log(this.timeFormat(value))
      this.showDate = false;
      this.form.birthday = this.timeFormat(value);
    },
    onConfirmNation(value,index){
      this.showNation = false;
      this.nationText = value.dictLabel;
      this.form.nation = value.dictValue;
    },
    onConfirmBusiness(value){
      this.showBusiness = false;
      this.businessTypeText = value.dictLabel;
      this.form.businessType = value.dictValue;
      console.log(value)
    },
    timeFormat(time) { // 时间格式化 2019-09-08

      let year = time.getFullYear();

      let month = time.getMonth() + 1;

      let day = time.getDate();

      return year + '-' + month + '-' + day

    },
    getCode() {
      getCodeImg().then((res) => {
        this.form.uuid = res.uuid;
        this.codeUrl = "data:image/gif;base64," + res.img;
      });
    },
    getSubmit(){
      updatePersonal(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>
