<template>
  <el-form
    ref="formRef"
    :label-width="labelWidth + 'px'"
    :model="modelValue"
    :rules="rules"
  >
    <div v-if="showTitle" class="form_title">基本信息</div>
    <el-row>
      <el-col :span="24">
        <el-form-item label="机构logo" prop="image">
          <ImageUpload v-model="modelValue.image" :fileSize="2" :limit="1" />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item label="机构名称" prop="name">
          <el-input v-model="modelValue.name" :disabled="!isAdd"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item label="组织机构代码:" prop="code">
          <el-input v-model="modelValue.code" :disabled="!isAdd"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <!--    <el-row>-->
    <!--      <el-col :span="24">-->
    <!--        <el-form-item label="所属站点:" prop="tenantId">-->
    <!--          <SiteOptions v-model="modelValue.tenantId" :limitWidth="false" :site-list="siteList"/>-->
    <!--        </el-form-item>-->
    <!--      </el-col>-->
    <!--    </el-row>-->

<!--    <CityOptions ref="cityForm" v-model="modelValue" :labelWidth="labelWidth" />-->
    <el-form-item
        label="国家"
        prop="country"
    >
      <el-select
          v-model="modelValue.country"
          placeholder="请选择国家"
      >
        <el-option
            v-for="option in countryOptions"
            :label="option.value"
            :value="option.key"
        />
      </el-select>
    </el-form-item>

    <el-row>
      <el-col :span="24">
        <el-form-item label="详细地址:">
          <el-input v-model="modelValue.address"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <field-options
      ref="fieldForm"
      v-model="modelValue"
      :isAdd="isAdd"
      :labelWidth="labelWidth"
    />

    <InputBoxAdd
      ref="directionsForm"
      v-model="modelValue"
      :labelWidth="labelWidth"
      fieldKey="researchs"
      placeholder="请输入研究方向"
      title="研究方向"
    />

    <el-row>
      <el-col :span="24">
        <el-form-item label="营业执照:" prop="license">
          <ImageUpload
            v-model="modelValue.license"
            :isShowTip="false"
            :limit="1"
          />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item label="机构简介" prop="introduce">
          <WangEditor
            v-model="modelValue.introduce"
            minHeight="350px"
          ></WangEditor>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script name="ResearchForm" setup>
import CityOptions from "@/views/components/CityOptions";
import InputBoxAdd from "@/views/components/InputBoxAdd";
import SiteOptions from "@/views/components/SiteOptions";
import FieldOptions from "@/views/components/FieldOptions";
import { onMounted, reactive, ref, toRefs } from "vue";
import { tenantSelect } from "@/api/subPlatform/tenant";
import {countryOptions} from "@/utils/parameter";

const siteList = ref([]);
const props = defineProps({
  modelValue: Object,
  isAdd: {
    type: Boolean,
    default: true,
  },
  showTitle: {
    type: Boolean,
    default: false,
  },
  labelWidth: {
    type: Number,
    default: 120,
  },
});
const { modelValue, isAdd, showTitle, labelWidth } = toRefs(props);
const data = reactive({
  rules: {
    product: [{ required: true, message: "请输入", trigger: "blur" }],
    name: [{ required: true, message: "请输入", trigger: "blur" }],
    kind: [{ required: true, message: "请选择", trigger: "change" }],
    code: [{ required: true, message: "请输入", trigger: "blur" }],
    mobile: [
      { required: true, message: "请输入", trigger: "blur" },
      {
        pattern: /^1[3-9]\d{9}$/,
        message: "请输入正确的手机号码",
        trigger: "blur",
      },
    ],
    research_id: [{ required: true, message: "请选择", trigger: "change" }],
    tenant_id: [
      {
        required: true,
        message: "请选择",
        trigger: ["blur", "change"],
      },
    ],
    school: [{ required: true, message: "请输入", trigger: "blur" }],
    education: [{ required: true, message: "请选择", trigger: "change" }],
    major: [{ required: true, message: "请输入", trigger: "blur" }],
    job: [{ required: true, message: "请输入", trigger: "blur" }],
    title: [{ required: true, message: "请输入", trigger: "blur" }],
    work_at: [
      {
        required: true,
        message: "从业时间不能为空",
        trigger: ["change", "blur"],
      },
    ],
    license: [
      {
        required: true,
        message: "请上传",
        trigger: ["blur", "change"],
      },
    ],
    introduce: [{ required: true, message: "请输入", trigger: "blur" }],
  },
});
const { rules } = toRefs(data);
const options = ref([]);

// 获取站点列表
const getSiteList = async () => {
  const resp = await tenantSelect();
  siteList.value = resp.rows;
};

const formRef = ref();

const validateForm = async () => {
  try {
    return await formRef.value.validate();
  } catch (error) {
    return false;
  }
};
defineExpose({
  validateForm,
});

onMounted(() => {
  formRef.value?.resetFields();
  getSiteList();
});
</script>

<style lang="scss" scoped>
.form_title {
  font-weight: 700;
  margin-bottom: 30px;
}

.el-select,
.el-date-editor {
  display: block;
  width: 100%;
}
</style>
