<template>
  <div class="position-re">
    <div>
      <el-breadcrumb separator="/" class="my-breadcrumb">
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>详情</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="back" @click="$emit('changeOperation','',{})"><span class="fa fa-reply cur-po " >返回</span></div>

    <div class="detail-main">
      <div class="block">
        <div class="my-b-title">基本信息</div>
        <div class="form"><diy-el-form ref="formRef"  v-model:model-form="forms" :forms="formConfig"/></div>
      </div>
      <div class="detail-operation-btn" v-if="operationType !== 'detail'">
        <el-button :icon="Select" @click="saveBefore()">保存</el-button>
        <el-button :icon="Close" @click="$emit('changeOperation','')">返回</el-button>
      </div>
    </div>
  </div>
</template>

<script>


import {UserManageDetailForm} from "@/data/all-form";
import DiyElForm from "@/components/common/DiyElForm.vue";
import {onBeforeMount, ref} from "vue";
import {Close, Select} from "@element-plus/icons-vue";
import {v4} from 'uuid'
import {
  proSelectUnitnew,
  proSelectXyxxDepartment, proXyxxInsertYhgl, proXyxxSelectUnitYysx, proXyxxSelectUsername,
  proXyxxSystemUnitYysx, proXyxxUpdateYhgl
} from "@/service/system-manage/user-manage/user-manage";
import {elAlert, handleMes} from "@/units/element-ui/tip";
import {Base64} from "js-base64";
import {ElMessageBox} from "element-plus";
import {InsertSyslog} from "@/units/use-method/user-log";

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

    },
    operationType:{

    }
  },
  setup(props,{emit}) {
    function saveBefore(){
      formRef.value.refEl.validate(isValid => {
            if (isValid){
              save();
            }
      })
    }
    function save(){
      if(!props.currentOpObj.USERID){
        proXyxxSelectUsername({   //新增确认不会和已有用户重名
          "A_USERNAME":forms.value.USERNAME
        }).then(res =>{
          if(res.msg[0].Rows.length > 0){
            debugger
            elAlert("用户名已存在")
            return
          }else{
            saveS()
          }
        })
      }else{   //修改的时候用户名不能修改
        saveS()
      }
    }
    function saveS() {
      let YYSX_LIST = [];
      if(forms.value.YYSX_LISE!=undefined){
        for(let i = 0;i<forms.value.YYSX_LISE.length;i++){
          let obj = {
            "V_TCODE":forms.value.YYSX_LISE[i]
          }
          YYSX_LIST.push(obj)
        }
      }
      if (!props.currentOpObj.USERID){  //新增
        proXyxxInsertYhgl({
          A_USERID:v4(),
          A_USERNAME: forms.value.USERNAME,
          A_PASSWORD: Base64.encode(forms.value.PASSWORD),
          A_NAME: forms.value.NAME,
          A_UNIT: forms.value.UNIT,
          A_DEPART: forms.value.DEPARTMENT ==undefined?"":forms.value.DEPARTMENT,
          A_SEX: "",
          A_TELPHONE: forms.value.TELPHONE ==undefined?"":Base64.encode(forms.value.TELPHONE),
          A_MAIL: forms.value.MAIL ==undefined?"":Base64.encode(forms.value.MAIL),
          A_CONTACT: forms.value.CONTACT ==undefined?"":Base64.encode(forms.value.CONTACT),
          YYSX_LISE: YYSX_LIST
        }).then(res => {
          handleMes(res,() => {
            InsertSyslog(localStorage.getItem("xyxx_usercode"),localStorage.getItem("xyxx_unitcode"), "信息新增", "用户管理新增成功", "用户管理", "")
            emit('changeOperation','')
          })
        })
      }else {  //修改
        proXyxxUpdateYhgl({
          A_USERID:props.currentOpObj.USERID,
          A_USERNAME: forms.value.USERNAME,
          A_PASSWORD: Base64.encode(forms.value.PASSWORD),
          A_NAME: forms.value.NAME,
          A_UNIT: forms.value.UNIT,
          A_DEPART: forms.value.DEPARTMENT ==undefined?"":forms.value.DEPARTMENT,
          A_SEX: "",
          A_TELPHONE: forms.value.TELPHONE ==undefined?"":Base64.encode(forms.value.TELPHONE),
          A_MAIL: forms.value.MAIL ==undefined?"":Base64.encode(forms.value.MAIL),
          A_CONTACT: forms.value.CONTACT ==undefined?"":Base64.encode(forms.value.CONTACT),
          YYSX_LISE: YYSX_LIST
        }).then(res => {
          handleMes(res,() => {
            InsertSyslog(localStorage.getItem("xyxx_usercode"),localStorage.getItem("xyxx_unitcode"), "信息修改", "用户管理修改成功", "用户管理", "")
            emit('changeOperation','')
          })
        })
      }
    }
    const forms = ref({

    })
    const formRef =ref(null)
    const formConfig = ref({...UserManageDetailForm});
    onBeforeMount(() => {

      let unitItem = formConfig.value.formItemsArr.find(i => i.modelValue === 'UNIT')
      unitItem.events = {
        'change'(val){
          getDepartmentOption();
        }
      }

      const usernameItem = formConfig.value.formItemsArr.find(i => i.modelValue === "USERNAME")
      usernameItem.elInputEleAttrs.disabled = props.operationType !== 'add';
      formConfig.value.formAttrs.disabled=props.operationType === 'detail';
      debugger
      forms.value = {
        ...props.currentOpObj
      }
      getUnitOption();
      getDepartmentOption();
      getApplicationMattersOption();
    })
    function getUnitOption(){
      proSelectUnitnew({
        pageSize:"1000",
        pageIndex:"1"
      }).then(res =>{
        debugger
        let returnData = res.msg[0].Rows;
        let returnOption = [];
        for(let i=0;i<returnData.length;i++){
          if(returnData[i].NAME ==''||returnData[i].NAME ==undefined){
            returnData.splice(i, 1);
            i--;
          }
        }
        returnOption =  returnData.map(i => ({
          ...i,
          label:i.NAME,
          value:i.CODE,
        }))
        let option = [
          {
            value:'',
            label:'请选择'
          }
        ].concat(returnOption)

        const findItem = formConfig.value.formItemsArr.find(i => i.modelValue === "UNIT")

        findItem.options = [...option]
      })
    }
    function getDepartmentOption(){
      debugger
      proSelectXyxxDepartment({
        UNITID: forms.value.UNIT,
        pageSize:"1000",
        pageIndex:"1"
      }).then(res =>{
        let returnOption =  res.msg[0].Rows.map(i => ({
          ...i,
          label:i.DEPARTMENT,
          value:i.DEPARTMENTID,
        }))
        let option = [
          {
            value:'',
            label:'请选择'
          }
        ].concat(returnOption)

        const findItem = formConfig.value.formItemsArr.find(i => i.modelValue === "DEPARTMENT")

        findItem.options = [...option]
      })
    }
    function getApplicationMattersOption(){
      proXyxxSystemUnitYysx({
        pageSize:"1000",
        pageIndex:"1"
      }).then(res =>{
        let returnOption =  res.msg[0].Rows.map(i => ({
          ...i,
          label:i.YYSX,
          value:i.ID,
        }))
        let option = [
          {
            value:'',
            label:'请选择'
          }
        ].concat(returnOption)

        const findItem = formConfig.value.formItemsArr.find(i => i.modelValue === "YYSX_LISE")

        findItem.options = [...option]

        getApplicationMatters();
      })
    }
    function getApplicationMatters(){
      proXyxxSelectUnitYysx({
        A_CODE: forms.value.USERID,
        pageSize:"1000",
        pageIndex:"1"
      }).then(res =>{
        let matters= res.msg[0].Rows;
        let matterIds = [];
        for(let i= 0;i< matters.length;i++){
          matterIds.push(matters[i].ID);
        }
        forms.value.YYSX_LISE = matterIds;
      })
    }
    return {
      formRef,
      formConfig,
      forms,
      saveS,
      saveBefore,
      save
    }
  }
}
</script>

<style scoped lang="scss">
.detail-main{
  .search-div{
    margin-top: 20px;
    margin-bottom: 20px;
    margin-left: auto;
    width: 300px;
  }
  .form{
    max-width: 100%;
  }
  padding: 0 20px;
}
</style>
