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

    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.expertAvatar')">
          <ImageUpload v-model="modelValue.image" :limit="1" />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item :label="t('admin.form.fullName')" prop="name">
          <el-input v-model="modelValue.name"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item :label="t('admin.form.mobile')" prop="mobile">
          <el-input v-model="modelValue.mobile"></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="12">
        <el-form-item :label="t('webSearch.unit')" prop="researchId">
          <el-select
            v-model="modelValue.researchId"
            filterable
            :placeholder="t('admin.form.pleaseSelect')"
            @change="setLaboratory"
          >
            <el-option
              v-for="item in researchOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="所属实验室:">
          <el-select
            v-model="modelValue.laboratoryId"
            filterable
            :placeholder="t('admin.form.pleaseSelect')"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row> -->

    <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="t('admin.form.address')">
          <el-input v-model="modelValue.address"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <!--    <el-row>-->
    <!--      <el-col :span="12">-->
    <!--        <el-form-item :label="'所属实验室'" prop="school">-->
    <!--          <el-input v-model="modelValue.school"></el-input>-->
    <!--        </el-form-item>-->
    <!--      </el-col>-->
    <!--      <el-col :span="12">-->
    <!--        <el-form-item-->
    <!--            :label="t('webSearch.researchInstitute')"-->
    <!--            prop="organization"-->
    <!--        >-->
    <!--          <el-input v-model="modelValue.organization"></el-input>-->
    <!--        </el-form-item>-->
    <!--      </el-col>-->
    <!--    </el-row>-->
    <el-form-item :label="t('admin.table.graduateSchool')" prop="school">
      <el-input v-model="modelValue.school"></el-input>
    </el-form-item>
    <el-row>
      <el-col :span="12">
        <el-form-item
          :label="t('admin.table.highestEducation')"
          prop="education"
        >
          <el-select
            v-model="modelValue.education"
            :placeholder="t('admin.form.pleaseSelect')"
          >
            <el-option
              v-for="item in educationOptions"
              :key="item.key"
              :label="item[locale]"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item :label="t('admin.table.major')" prop="major">
          <el-input v-model="modelValue.major"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item :label="t('admin.form.position')" prop="job">
          <el-input v-model="modelValue.job"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item :label="t('webSearch.title')" prop="title">
          <el-input v-model="modelValue.title"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item :label="t('admin.form.workingTime')" prop="workTime">
          <el-date-picker
            v-model="modelValue.workTime"
            :placeholder="t('admin.form.pleaseSelect')"
            type="date"
            value-format="YYYY-MM-DD"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item :label="t('input.gender')" prop="gender">
          <el-radio v-model="modelValue.gender" label="1">
            {{ t("gender.male") }}
          </el-radio>
          <el-radio v-model="modelValue.gender" label="2"
            >{{ t("gender.female") }}
          </el-radio>
        </el-form-item>
      </el-col>
    </el-row>

    <FieldOptions
      ref="fieldFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
    />

    <InputBoxAdd
      ref="keywordsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      :placeholder="t('admin.form.inputKeywords')"
      :title="t('admin.form.keywords')"
      fieldKey="keywords"
    />
    <InputBoxAdd
      ref="researchsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      :placeholder="
        t('admin.form.placeholder', {
          name: t('admin.form.researchDirection'),
        })
      "
      :title="t('admin.form.researchDirection')"
      fieldKey="researchs"
    />

    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.personalProfile')" prop="introduce">
          <el-input
            v-model="modelValue.introduce"
            :placeholder="
              t(
                'admin.validation.pleaseEnterResearchDirectionCoreTechnologyAndProductRepresentativePatentsAndPapersUndertakeTheNameAndAbstractOfScientificResearchProjects'
              )
            "
            type="textarea"
            :autosize="{ minRows: 6, maxRows: 8 }"
            show-word-limit
          />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script name="ExpertForm" setup>
import FieldOptions from "@/views/components/FieldOptions";
import InputBoxAdd from "@/views/components/InputBoxAdd";
import { countryOptions, educationOptions } from "@/utils/parameter";
import { useI18n } from "vue-i18n";
import { computed, toRefs } from "vue";

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 { modelValue, isAdd, showTitle, labelWidth } = toRefs(props);
const data = reactive({
  rules: {
    product: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    name: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    kind: [
      {
        required: true,
        message: computed(() => t("admin.form.pleaseSelect")),
        trigger: "change",
      },
    ],
    code: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    mobile: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
      // {
      //   pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
      //   message: computed(() =>
      //     t("admin.validation.pleaseEnterTheCorrectFormatOfThePhoneNumber")
      //   ),
      //   trigger: "blur",
      // },
    ],
    country: [
      {
        required: true,
        message: computed(() => t("admin.validation.required")),
        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"],
      },
    ],
    school: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    organization: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: "blur",
      },
    ],
    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",
      },
    ],
    workTime: [
      {
        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"],
      },
    ],
    introduce: [
      {
        required: true,
        message: computed(() => t("admin.form.placeholder")),
        trigger: ["change", "blur"],
      },
    ],
  },
});

const { rules } = toRefs(data);
const researchOptions = ref([]);
const options = ref([]);
const formRef = ref();
const cityFormRef = ref();
const fieldFormRef = ref();
const keywordsFormRef = ref();
const researchsFormRef = ref();
const setLaboratory = () => {};
const validateForm = async () => {
  let formValid;
  try {
    formValid = await formRef.value.validate();
  } catch (error) {
    formValid = false;
  }
  // const cityFormValid = await cityFormRef.value.validateForm(); // 城市选择表单验证
  const fieldFormValid = await fieldFormRef.value.validateForm(); // 领域选择表单验证
  const keywordsFormValid = await keywordsFormRef.value.validateForm(); // 关键词表单验证
  const researchsFormValid = await researchsFormRef.value.validateForm(); // 研究方向表单验证
  return (
    formValid &&
    // cityFormValid &&
    fieldFormValid &&
    keywordsFormValid &&
    researchsFormValid
  );
  // if (isAdd.value) {
  //   // const directionsFormValid = await directionsFormRef.value.validateForm();
  //   return formValid && cityFormValid && fieldFormValid && researchsFormValid;
  // } else {
  //   return formValid && cityFormValid && fieldFormValid && keywordsFormValid;
  // }
};
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>
