<template>
  <div class="detail-container">
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-position="right"
      label-width="150px"
      style="max-height: 60vh; overflow-y: auto; padding-right: 50px"
    >
      <el-form-item label="负责人:" prop="fzr">
        <el-select
          v-model="ryxx"
          filterable
          remote
          reserve-keyword
          clearable
          placeholder="输入人员名称 / 联系方式 查询"
          :remote-method="remoteMethod"
          :loading="loading"
          @change="handleSelect"
          value-key="userId"
        >
          <el-option
            v-for="item in selectOptions"
            :key="item.userId"
            :label="item.realName"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人添加经纬度:">
        <el-select v-model="sfjwd">
          <el-option label="是" value="是"></el-option>
          <el-option label="否" value="否"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item v-if="sfjwd === '否'" label="站点经度:" prop="jd">
        <el-input v-model="form.jd" />
      </el-form-item>
      <el-form-item v-if="sfjwd === '否'" label="站点纬度:" prop="wd">
        <el-input v-model="form.wd" />
      </el-form-item>
    </el-form>
  </div>
</template>
    
    <script setup>
import { ref, onMounted, watch, computed } from "vue";
import { request } from "@/utils/request";
const formRef = ref(null);
defineExpose({ formRef });

const props = defineProps({
  form: {
    type: Object,
    default: () => ({}),
  },
});

const sfjwd = ref("是");
const ryxx = ref("");
const loading = ref(false);
const selectOptions = ref([]);

// 根据用户信息 查询角色列表
const getUserList = async (key) => {
  try {
    const keyword = key;
    let url = "";
    if (keyword) {
      url = `/snow-ops-platform/yhzry/getUserByKey?key=${keyword}`;
    } else {
      url = `/snow-ops-platform/yhzry/getUserByKey?key=`;
    }
    const res = await request({
      url: url,
      method: "GET",
    });
    if (res.code === "00000") {
      return res.data;
    } else {
      throw new Error(res.message);
    }
  } catch (error) {
    ElMessage.error(error.message);
    console.log(error);
  }
};

// 选择人员筛选
const remoteMethod = async (query) => {
  if (query === "") {
    selectOptions.value = [];
    return [];
  }
  loading.value = true;
  const res = await getUserList(query);
  if (res) {
    selectOptions.value = res;
  }
  loading.value = false;
};

// 选择人员
const handleSelect = (value) => {
  props.form.xm = value.realName;
  props.form.sjhm = value.phone;
  props.form.userId = value.userId;
};

// 表单校验规则
const rules = computed(() => {
  return {
    fzr: [
      {
        required: true,
        validator: (rule, value, callback) => {
          if (props.form.userId && props.form.sjhm && props.form.xm) {
            callback();
          } else {
            callback(new Error("请选择负责人"));
          }
        },
        trigger: "blur",
      },
    ],
    jd: [
      {
        required: sfjwd.value === "否",
        message: "请输入站点经度",
        trigger: "blur",
      },
    ],
    wd: [
      {
        required: sfjwd.value === "否",
        message: "请输入站点纬度",
        trigger: "blur",
      },
    ],
  };
});
</script>
    
    <style>
</style>