<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>
    <template v-if="isEditDialogShow">
      <unit-manage-department-setting-dialog  :dialog-operation-type="dialogOperationType" :current-dialog-obj="currentDialogOpObj" v-model:dialog-visible="isEditDialogShow"/>
    </template>
  </div>
</template>

<script>
import {Close, Select} from "@element-plus/icons-vue";
import DiyElForm from "@/components/common/DiyElForm.vue";
import {onBeforeMount, ref} from "vue";
import {unitManageDetailForm} from "@/data/all-form";
import {useDetailOrEdit} from "@/units/use-method/usePagination";
import {v4} from 'uuid'
import UnitManageDepartmentSettingDialog
  from "@/views/main/c-views/system-manage/unit-manage/UnitManageDepartmentSettingDialog.vue";
import {elAlert, handleMes} from "@/units/element-ui/tip";
import {
  proXyxxInsertUnit,
  proXyxxSelectUnitCode,
  proXyxxUpdateUnit
} from "@/service/system-manage/unit-manage/unit-manage";
import {proXyxxInsertYhgl, proXyxxUpdateYhgl} from "@/service/system-manage/user-manage/user-manage";
import {Base64} from "js-base64";
import {InsertSyslog} from "@/units/use-method/user-log";
export default {
  name: "UnitManageEdit",
  computed: {
    Close() {
      return Close
    },
    Select() {
      return Select
    },
  },
  components: {UnitManageDepartmentSettingDialog, DiyElForm},
  props:{
    currentOpObj:{

    },
    operationType:{

    }
  },
  setup(props,{emit}) {
    const UNIT_CODE = ref(null);
    const forms = ref({});
    const formRef =ref(null);
    const formConfig = ref({...unitManageDetailForm});
    const isEditDialogShow = ref(false);
    const {changeOperation,currentOpObj:currentDialogOpObj,operationType:dialogOperationType} = useDetailOrEdit()
    function saveBefore(){
      formRef.value.refEl.validate(isValid => {
        if (isValid){
          save();
        }
      })
    }
    function save(){
      if(!props.currentOpObj.CODE){
        proXyxxSelectUnitCode({   //新增确认不会和已有单位重名
          "A_UNITNAME":forms.value.NAME
        }).then(res =>{
          if(res.msg[0].Rows.length > 0){
            debugger
            elAlert("单位名已存在")
            return
          }else{
            saveS()
          }
        })
      }else{   //修改验证单位名称是否修改
        if(props.currentOpObj.NAME != forms.value.NAME){
          proXyxxSelectUnitCode({   //新增确认不会和已有单位重名
            "A_UNITNAME":forms.value.NAME
          }).then(res =>{
            if(res.msg[0].Rows.length > 0){
              debugger
              elAlert("单位名已存在")
              return
            }else{
              saveS()
            }
          })
        }else{
          saveS()
        }
      }
    }
    function saveS(){
      if (!props.currentOpObj.CODE){  //新增
        proXyxxInsertUnit({
          A_CODE:  UNIT_CODE.value,
          A_NAME: forms.value.NAME,
          A_ADDRESS: forms.value.ADDRESS,
          A_MANAGER: forms.value.MANAGER,
          A_ZIPCODE: forms.value.ZIPCODE ==undefined?"":forms.value.ZIPCODE,
          A_TELEPHONE: forms.value.TELEPHONE ==undefined?"":forms.value.TELEPHONE,
          A_FAXNUM:forms.value.FAXNUM ==undefined?"":forms.value.FAXNUM,
          A_MAIL: forms.value.MAIL ==undefined?"":forms.value.MAIL,
          A_IS_SUPERIOR: 1,
          A_SUPERIOR: "",
        }).then(res => {
          handleMes(res,() => {
            InsertSyslog(localStorage.getItem("xyxx_usercode"),localStorage.getItem("xyxx_unitcode"), "信息新增", "单位管理新增成功", "单位管理", "")
            emit('changeOperation','')
          })
        })
      }else {  //修改
        proXyxxUpdateUnit({
          A_CODE:  UNIT_CODE.value,
          A_NAME: forms.value.NAME,
          A_ADDRESS: forms.value.ADDRESS,
          A_MANAGER: forms.value.MANAGER,
          A_ZIPCODE: forms.value.ZIPCODE ==undefined?"":forms.value.ZIPCODE,
          A_TELEPHONE: forms.value.TELEPHONE ==undefined?"":forms.value.TELEPHONE,
          A_FAXNUM:forms.value.FAXNUM ==undefined?"":forms.value.FAXNUM,
          A_MAIL: forms.value.MAIL ==undefined?"":forms.value.MAIL,
          A_IS_SUPERIOR: 1,
          A_SUPERIOR: "",
        }).then(res => {
          handleMes(res,() => {
            InsertSyslog(localStorage.getItem("xyxx_usercode"),localStorage.getItem("xyxx_unitcode"), "信息修改", "单位管理修改成功", "单位管理", "")
            emit('changeOperation','')
          })
        })
      }
    }
    function openDialog(type,UNIT_CODE){
      isEditDialogShow.value = true;
      let obj = {
        UNIT_CODE:UNIT_CODE
      }
      changeOperation(type,obj)
    }
    onBeforeMount(() => {
      formConfig.value.formAttrs.disabled=props.operationType === 'detail';
      if(props.operationType == "add"){  //单位新增时，前端默认增加单位编码
        UNIT_CODE.value = v4();
      }else{   //其他情况从props中获取
        UNIT_CODE.value = props.currentOpObj.CODE;
      }
      let departmentItem = formConfig.value.formItemsArr.find(i => i.modelValue === 'DEPARTMENT')
      departmentItem.events = {
        'click'(){
          openDialog('detail',UNIT_CODE.value);
        }
      }
      forms.value = {
        ...props.currentOpObj
      }
    })
    return {
      formRef,
      formConfig,
      forms,
      saveBefore,
      isEditDialogShow,
      dialogOperationType,
      currentDialogOpObj
    }
  }
}
</script>

<style scoped>

</style>