<script setup>
import { apiCustomerUser } from '@/api/customer'

const show = defineModel('show', { type: Boolean, default: false })
const props = defineProps({
  userId: { type: Number, default: 0 },
})
const emits = defineEmits(["success"])
const formRef = ref(null)
const formData = ref({
  CompanyId: null,
  UserId: null,
})
const formRules = {
  CompanyId: [
    { required: true, message: '请选择投资者机构', trigger: 'change' },
  ],
}

// 获取部门数据
const departArr = ref([])
async function getDepartList() {
  const res = await apiCustomerUser.companySearch()
  if (res.Ret !== 200) return
  const list = res.Data || []
  departArr.value = list
}

watch(() => show.value, (newval) => {
  if (newval) {
    formData.value.UserId = props.userId
    formRef.value ? formRef.value.clearValidate() : '';
    getDepartList()
  }
})

async function handleSubmitForm() {
  await formRef.value.validate()
  
  const { CompanyId, UserId } = formData.value

  const res=await apiCustomerUser.usersMove({
    CompanyId,
    UserId,
  })
  if(res.Ret!==200) return
  ElMessage.success('操作成功')
  show.value=false
  emits("success")
}
</script>

<template>
  <el-dialog
    v-model="show"
    :close-on-click-modal="false"
    :modal-append-to-body="false"
    width="30%"
    title="移动"
  >
    <div class="dialog-content">
      <el-form
      label-width="80px"
      :rules="formRules"
      ref="formRef"
      :model="formData"
      >
      <el-form-item label="移动到" prop="CompanyId">
          <el-select
            v-model="formData.CompanyId"
            placeholder="请选择投资者机构"
            size="large"
            style="width: 100%"
            filterable
            no-match-text="暂无该机构，请前往客户列表添加投资者机构！"
          >
            <el-option
              v-for="item in departArr"
              :key="item.CompanyId"
              :label="item.CompanyName"
              :value="item.CompanyId"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="btn-content">
        <el-button
          type="primary"
          style="width: 80px; margin-right: 24px"
          @click="handleSubmitForm"
          >保存</el-button
        >
        <el-button style="width: 80px" @click="show = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
.dialog-content {
  padding: 0 50px;
}
.btn-content {
  text-align: center;
  padding-bottom: 20px;
}
</style>
