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

const show = defineModel('show', { type: Boolean, default: false })
const props = defineProps({
  data: Object,
})
const emits = defineEmits(["success"])
const formRef = ref()
const formData = ref({
  RealName: '',
  SellerId: null,
  CompanyId: null,
  UserId: null,
  UserType: null
})
const formRules = {
  RealName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  SellerId: [{ required: true, message: '请选择营业部', trigger: 'change' }],
  UserType: [{ required: true, message: '请选择客户类型', trigger: 'blur' }],
  CompanyId: [{ required: true, message: '请选择机构', trigger: 'blur' }],
}

const options = ref([
  { label: '个人投资者', value: 1 },
  { label: '机构投资者', value: 2 },
])


// 获取部门数据
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.RealName = props.data.RealName
    formData.value.Mobile = props.data.Mobile
    formData.value.UserId = props.data.UserId
    formRef.value ? formRef.value.clearValidate() : '';
    getDepartList()
  }
})

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

  const res=await apiCustomerUser.usersTransFormal({
    RealName,
    SellerId,
    CompanyId,
    UserId,
    UserType
  })
  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="RealName">
          <el-input
            v-model="formData.RealName"
            style="width: 100%"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <span>{{ formData.Mobile }}</span>
        </el-form-item>
        <el-form-item label="客户类型" prop="UserType">
          <el-select
            v-model="formData.UserType"
            placeholder="请选择客户类型"
            size="large"
            style="width: 100%"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属机构" prop="CompanyId" v-if="formData.UserType === 2">
          <el-select
            v-model="formData.CompanyId"
            placeholder="请选择客户类型"
            size="large"
            filterable
            no-match-text="暂无该机构，请前往客户列表添加投资者机构！"
            style="width: 100%"
          >
            <el-option
              v-for="item in departArr"
              :key="item.CompanyId"
              :label="item.CompanyName"
              :value="item.CompanyId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="营业部" prop="SellerId" v-if="formData.UserType === 1">
          <all-user-for-depart
            style="width: 100%"
            :props="{
             emitPath: false,
            }"
            :filterable="true"
            onlySelectUser
            :size="'large'"
            v-model="formData.SellerId"
            placeholder="请选择营业部"
          />
        </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>
