<template>
  <div class="my-detail-main">
    <my-breadcrumb class="my-breadcrumb" :breadcrumb-arr="myBread"/>
    <div class="block">
      <div class="my-b-title">用户信息</div>
      <diy-el-form  ref="formRef" v-model:model-form="forms" :forms="formConfig"/>
    </div>
    <div class="detail-operation-btn" v-if="operationType !== 'detail'">
      <el-button :icon="Select" @click="saveUserInfo">保存</el-button>
      <el-button :icon="Close" @click="$emit('changeOperation','')">关闭</el-button>
    </div>
  </div>
</template>

<script>
import MyBreadcrumb from "@/components/common/MyBreadcrumb.vue";
import {userManageDetailFormKj} from "@/data/all-forms";
import DiyElForm from "@/components/common/DiyElForm.vue";
import {onBeforeMount, ref} from "vue";
import {useRootStore} from "@/stores/useStore";
import {
  addSpaceUserInfo,
  getDetailUnitListSpace,
  getSpaceUserDetail, updateSpaceUserInfo,
  validateUseName
} from "@/service/system-manage/unit-manage/unit-maneg";
import {UserInfo} from "@/unit/UserInfo";
import {decode, encode} from "js-base64";
import {Close, Select} from "@element-plus/icons-vue";
import {handleMes, showMessage} from "@/unit/element-ui/tip";

export default {
  name: "UserManageDetailKj",
  computed: {
    Close() {
      return Close
    },
    Select() {
      return Select
    }
  },
  components: {DiyElForm, MyBreadcrumb},
  props:{
    currentOpObj:{

    },
    operationType:{

    }
  },
  setup(props,{emit}) {
    const formRef = ref(null)
    const myBread = [{name:'用户管理 '},{name:'用户管理新增'}]
    const store = useRootStore()
    const formConfig = ref({...userManageDetailFormKj})
    function saveUserInfo(){
      formRef.value.refEl.validate(isValid => {
        if (isValid){
          validateUseName(forms.value.USERNAME).then(res => {
            let isHas = res.msg[0].Rows.find(i => i.USERID !== forms.value.USERID)
            if (props.operationType === 'add' && res.msg[0].Rows.length > 0){
              isHas = true
            }
            if (isHas){
              showMessage({
                type:'error',
                message:'用户名已存在！'
              })
            }else {
              let url = props.operationType === 'add'?addSpaceUserInfo:updateSpaceUserInfo
              url({
                a_userid:forms.value.USERID,
                a_username:forms.value.USERNAME,
                a_password:encode(forms.value.PASSWORD),
                a_contact:encode(forms.value.CONTACT),
                a_telphone:encode(forms.value.TELPHONE),
                a_name:forms.value.NAME,
                a_unit:forms.value.UNIT,
                a_sex:forms.value.SEX,
                a_mail:forms.value.MAIL?encode(forms.value.MAIL):'',
              }).then(res => {
                handleMes(res,() => {
                  emit('changeOperation','')
                })
              })


            }

          })
        }
      })

    }
    onBeforeMount(() =>{
      let newPasswordReg =/^(?=.{8,16})(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)(?=.*?[*?!&￥$%^#,./@";:><\[\]}{\-=+_\\|》《。，、？’‘“”~ `]).*$/;
      formConfig.value.formItemsArr.find(i => i.label === '确认密码').itemOtherAttrs.rules = [
        {
          validator(rules,value,callback) {
            if (!value) {
            callback(new Error('请确认新密码'))
          }else if (!newPasswordReg.test(value)){
              callback(new Error('密码必须是8到16位大小写字母和数字和特殊字符的组合！'))
            }else if(value !== forms.value['PASSWORD']){
            callback(new Error('两次密码输入不一致。'))
          }else {
            callback()
          }
          },trigger:'blur'
        }
      ]
      if (props.operationType!=='add'){
        getSpaceUserDetail({
          a_userid:props.currentOpObj.USERID,
          appkey:new UserInfo().GetUserKeyCode()
        }).then(res=>{
          try {
            forms.value = {
              ...res.msg[0].Rows[0],
              UNIT:res.msg[0].Rows[0].UNIT?res.msg[0].Rows[0].UNIT+'':'',
              CONFIRMPASSWORD:decode(res.msg[0].Rows[0].PASSWORD),
              PASSWORD:decode(res.msg[0].Rows[0].PASSWORD),
              CONTACT:res.msg[0].Rows[0].CONTACT?decode(res.msg[0].Rows[0].CONTACT):'',
              TELPHONE:res.msg[0].Rows[0].TELPHONE?decode(res.msg[0].Rows[0].TELPHONE):'',
              MAIL:res.msg[0].Rows[0].MAIL?decode(res.msg[0].Rows[0].MAIL):'',
            }
            console.log('能解码')
          }catch {

            forms.value = {
              ...res.msg[0].Rows[0],
              UNIT:res.msg[0].Rows[0].UNIT?res.msg[0].Rows[0].UNIT+'':'',
              CONFIRMPASSWORD:decode(res.msg[0].Rows[0].PASSWORD),
              PASSWORD:decode(res.msg[0].Rows[0].PASSWORD),
              CONTACT:res.msg[0].Rows[0].CONTACT,
              TELPHONE:res.msg[0].Rows[0].TELPHONE,
              MAIL:res.msg[0].Rows[0].MAIL,
            }

          }

        })
      }
      getDetailUnitListSpace({
        appkey:new UserInfo().GetUserKeyCode()
      }).then(res => {
        let findUnit = formConfig.value.formItemsArr.find(i=>i.modelValue === 'UNIT')
        findUnit.options = [{value:'',label:'请选择'}].concat(res.msg[0].Rows.map(i => ({
          label:i.NAME,
          value:i.CODE,
        })))
      })

    })
    const forms = ref({})
    return {
      myBread,
      formConfig,
      forms,
      saveUserInfo,
      formRef
    }
  }
}
</script>

<style scoped lang="scss">

</style>
