<template>
  <!--COMPLETE-->
  <el-form
    ref="formRef"
    :label-width="labelWidth + 'px'"
    :model="modelValue"
    :rules="rules"
  >
    <div v-if="showTitle" class="form_title">
      <!--基本信息-->
      {{ t("admin.form.basicInfo") }}
    </div>

    <!--    v-if="isAdd"-->
    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="t('admin.form.name', { type: t('admin.common.lab') })"
          prop="name"
        >
          <el-input v-model="modelValue.name"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="isAdd">
      <el-col :span="24">
        <el-form-item :label="t('admin.form.institute')" prop="school">
          <el-input
            v-model="modelValue.school"
            :placeholder="
              t('admin.form.placeholder', { type: t('admin.form.institute') })
            "
          />
        </el-form-item>
      </el-col>
    </el-row>

    <!--FIXME:-->
    <!--<el-row v-if="isAdd && false">
          <el-col :span="24">
            <el-form-item label="组织机构代码:" prop="code">
              <el-row justify="space-between" type="flex">
                <el-col :span="20">
                  <el-input v-model="modelValue.code"></el-input>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
        </el-row>-->
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.labWebsite')">
          <el-input
            v-model="modelValue.url"
            :placeholder="
              t('admin.form.placeholder', { type: t('admin.form.labWebsite') })
            "
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <FieldOptions
      ref="fieldFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
    />

    <InputBoxAdd
      ref="keywordsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      fieldKey="keywords"
      :placeholder="t('admin.form.keywords')"
      :title="t('admin.form.keywords')"
    />

    <!--    COMPLETED-->
    <!--    <el-form-item :label="t('admin.form.country')" prop="country">
      <el-select
        v-model="modelValue.country"
        :placeholder="
          t('admin.form.pleaseSelect', { type: t('admin.form.country') })
        "
      >
        <el-option
          v-for="option in countryOptions"
          :label="option[locale]"
          :value="option.key"
        />
      </el-select>
    </el-form-item>-->

    <!--    <el-row>-->
    <!--      <el-col :span="24">-->
    <!--        <el-form-item label="详细地址:">-->
    <!--          <el-input-->
    <!--              v-model="modelValue.address"-->
    <!--              placeholder="请输入详细地址"-->
    <!--          ></el-input>-->
    <!--        </el-form-item>-->
    <!--      </el-col>-->
    <!--    </el-row>-->
    <InputBoxAdd
      ref="directionsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      fieldKey="researchs"
      :placeholder="
        t('admin.form.placeholder', { type: t('admin.form.researchDirection') })
      "
      :title="t('admin.form.researchDirection')"
    />

    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="t('webSearch.technicalApplication')"
          prop="primaryTechnology"
        >
          <el-input
            v-model="modelValue.primaryTechnology"
            :autosize="{ minRows: 4 }"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('webSearch.technicalApplication'),
              })
            "
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="t('webSearch.researchProject')"
          prop="researchProject"
        >
          <el-input
            v-model="modelValue.researchProject"
            :autosize="{ minRows: 4 }"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('webSearch.researchProject'),
              })
            "
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="t('webSearch.representativeAchievements')"
          prop="achievement"
        >
          <el-input
            v-model="modelValue.achievement"
            :autosize="{ minRows: 4 }"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('webSearch.representativeAchievements'),
              })
            "
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('webContact.phone')" prop="phone">
          <!--            :maxlength="11"-->
          <el-input
            v-model="modelValue.phone"
            oninput="value = value.replace(/\D+/, '')"
            :placeholder="
              t('admin.form.placeholder', { type: t('webContact.phone') })
            "
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <!--    ADDEMAIL-->
    <!--    邮箱-->
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.email')" prop="email">
          <el-input v-model="modelValue.email"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <!--    其他联系方式-->
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.otherContact')" prop="otherContact">
          <el-input v-model="modelValue.otherContact"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item
          :label="t('webSearch.labIntroduction')"
          prop="introduction"
        >
          <el-input
            v-model="modelValue.introduction"
            :autosize="{ minRows: 6, maxRows: 8 }"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('webSearch.labIntroduction'),
              })
            "
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.coreMembers')" prop="member">
          <el-input
            v-model="modelValue.member"
            :autosize="{ minRows: 6, maxRows: 8 }"
            :placeholder="
              t('admin.form.placeholder', { type: t('admin.form.coreMembers') })
            "
            type="textarea"
          ></el-input>
          <!-- @click.native="handleEditAble" -->
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script setup>
import FieldOptions from "@/views/components/FieldOptions";
import InputBoxAdd from "@/views/components/InputBoxAdd";
import { computed, reactive, ref, toRefs } from "vue";
import { useI18n } from "vue-i18n";
import { countryOptions } from "@/utils/parameter";

const { t, locale } = useI18n();
const props = defineProps({
  modelValue: Object,
  isAdd: {
    type: Boolean,
    default: true,
  },
  showTitle: {
    type: Boolean,
    default: false,
  },
  labelWidth: {
    type: Number,
    default: 120,
  },
});

const formRef = ref();
const fieldFormRef = ref();
const keywordsFormRef = ref();
const directionsFormRef = ref();

const data = reactive({
  rules: {
    name: [
      {
        required: true,
        /*"请输入实验室名称"*/
        message: computed(() =>
          t("admin.form.placeholder", {
            type: t("admin.form.name", {
              type: t("admin.common.lab"),
            }),
          })
        ),
        trigger: "blur",
      },
    ],
    // code: [{ required: true, message: "请输入组织机构代码", trigger: "blur" }],
    school: [
      {
        required: true,
        /*"请输入所属院所或单位"*/
        message: computed(() =>
          t("admin.form.placeholder", {
            type: t("admin.form.institute"),
          })
        ),
        trigger: "blur",
      },
    ],
    introduction: [
      {
        required: true,
        message: computed(() =>
          t("admin.form.placeholder", {
            type: t("admin.form.introduction", {
              type: t("admin.common.lab"),
            }),
          })
        ),
        trigger: ["change", "blur"],
      },
    ],
    /*-----*/
    kind: [
      {
        required: true,
        message: computed(() => t("admin.form.pleaseSelect")),
        trigger: "change",
      },
    ],
    product: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    mobile: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
      // {
      //   pattern: /^1[3-9]\d{9}$/,
      //   message: "请输入正确的手机号码",
      //   trigger: "blur",
      // },
    ],
    researchId: [
      {
        required: true,
        message: computed(() => t("admin.form.pleaseSelect")),
        trigger: "change",
      },
    ],
    tenantId: [
      {
        required: true,
        message: computed(() => t("admin.form.pleaseSelect")),
        trigger: ["blur", "change"],
      },
    ],
    education: [
      {
        required: true,
        message: computed(() => t("admin.form.pleaseSelect")),
        trigger: "change",
      },
    ],
    major: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    job: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    title: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    work_at: [
      {
        required: true,
        message: computed(() =>
          t("admin.validation.required", { type: t("admin.form.workingTime") })
        ),
        trigger: ["change", "blur"],
      },
    ],
    license: [
      {
        required: true,
        message: computed(() => t("admin.validation.pleaseUpload")),
        trigger: ["blur", "change"],
      },
    ],
  },
});
const { rules } = toRefs(data);

const validateForm = async () => {
  let formValid;
  try {
    formValid = await formRef.value.validate();
  } catch (error) {
    formValid = false;
  }
  const fieldFormValid = await fieldFormRef.value.validateForm(); // 领域选择表单验证
  const keywordsFormValid = await keywordsFormRef.value.validateForm(); // 关键词表单验证
  const directionsFormValid = await directionsFormRef.value.validateForm(); // 研究方向表单验证
  return (
    formValid && fieldFormValid && keywordsFormValid && directionsFormValid
  );
};

defineExpose({
  validateForm,
});
</script>

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

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