<template>
  <baseTitle title="公司详情"></baseTitle>
  <fv-Form :schema="schema" @getInstance="(e)=>form = e"></fv-Form>
  <UserPicker :multiple="false" ref="presidentNamePicker"  @ok="editPresident"></UserPicker>
  <UserPicker :multiple="false" ref="generalManagerNamePicker" @ok="editGeneralManager"></UserPicker>
</template>

<script setup lang="jsx">
import UserPicker from '@/views/workflow/process/common/UserPicker.vue';
import Ttsup from '@/views/system/department/components/ToolToShowUserPicker.vue'
import {getCompanyDetail,setCompanyLeader} from "@/api/subsidiary";
const props=defineProps({
  value:{
    type:String,
    default:''
  }
})
const form = ref()
const presidentName = ref()
const presidentIds = ref()
const generalManagerName = ref()
const presidentNamePicker = ref()
const generalManagerNamePicker = ref()
const generalManagerIds = ref()
const schema = reactive([
  {
    label: '公司名称：',
    prop: 'companyName',
  },
  {
    label: '公司编码：',
    prop: 'companyCode'
  },
  {
    label: '董事长：',
    prop: 'presidentIds',
    component: ()=><Ttsup modelValue={presidentName.value} onClick={()=>{presidentNamePicker.value.showUserPicker()} } />

  },
  {
    label: '总经理：',
    prop: 'generalManagerIds',
    component: ()=><Ttsup modelValue={generalManagerName.value} onClick={()=>{generalManagerNamePicker.value.showUserPicker()} } />
  },
  {
    label: '创建时间：',
    prop: 'createTime'
  }
])
const emit = defineEmits(['update:value'])
const _value = computed({
  get() {
    return props.value;
  },
  set(value) {
    emit('update:value', value)
  }
})
const editPresident = (list) => {
  const arr = list.map(item=>item.name)
  presidentIds.value = list.map(item=>item.id)
  presidentName.value = arr.join(',')
  setDeptmentInfo({
    companyId: _value.value,
    presidentIds: presidentIds.value,
    generalManagerIds: generalManagerIds.value
  })
}

const editGeneralManager = (list) => {
  const arr = list.map(item=>item.name)
  generalManagerIds.value = list.map(item=>item.id)
  generalManagerName.value = arr.join(',')
  setDeptmentInfo({
    companyId: _value.value,
    presidentIds: presidentIds.value,
    generalManagerIds: generalManagerIds.value
  })
}

const setDeptmentInfo = async ({companyId = [], presidentIds = [], generalManagerIds = [] }) => {
  const params = {
    presidentIds,
    generalManagerIds,
    companyId,
  }
  const res = await setCompanyLeader(params)
  // console.log(res.data);
}
const getInfo = async () => {
  const { data } = await getCompanyDetail(_value.value)
  const params = {
    companyName: data.companyName,
    companyCode: data.companyCode,
    createTime: data.createTime
  }
  presidentIds.value = data.presidentIds
  generalManagerIds.value = data.generalManagerIds
  params.presidentIds = formatIdsToNames(data.presidentIds, data.presidentInfo, 'userId', 'nickName')
  presidentName.value = params.presidentIds
  params.generalManagerIds = formatIdsToNames(data.generalManagerIds, data.generalManagerInfo, 'userId', 'nickName')
  generalManagerName.value = params.generalManagerIds
  form.value.setValues(params)
}

const formatIdsToNames = (ids, infoList, key, viewKey) => {
  const resArr = []
  if(!ids?.length) return
  ids.forEach(item=>{
    infoList.forEach(v=>{
      item == v[key] && resArr.push(v[viewKey])
    })
  })
  return resArr.join(',')
}

getInfo()
</script>

<style scoped>

</style>
