<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.fullName')" prop="name">
          <el-input v-model="modelValue.name"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.mobile')" prop="phone">
          <el-input v-model="modelValue.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 :placeholder="t('input.inputEmail')" 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 v-if="isAdd">
      <el-col :span="24">
        <el-form-item
          :label="/*身份证号:*/ t('admin.form.idCardNumber')"
          prop="idCard"
        >
          <el-input v-model="modelValue.idCard"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.workLocation')" prop="wordAddress">
          <el-input v-model="modelValue.wordAddress" />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item :label="t('admin.form.workExperience')" prop="experience">
          <el-input
            v-model="modelValue.experience"
            :autosize="{ minRows: 2, maxRows: 10 }"
            type="textarea"
          />
        </el-form-item>
      </el-col>
    </el-row>

    <field-options ref="researchsFormRef" v-model="modelValue" />

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

    <el-row v-if="isAdd">
      <el-col :span="24">
        <el-form-item :label="t('admin.form.idCard')">
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item prop="idImage.0">
                <ImageUpload
                  v-model="modelValue.idImage[0]"
                  :isShowTip="false"
                  :limit="1"
                />
              </el-form-item>
              <div style="text-align: center">
                <!--身份证人像面-->
                {{ t("admin.form.idCardPortrait") }}
              </div>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="idImage.1">
                <ImageUpload
                  v-model="modelValue.idImage[1]"
                  :isShowTip="false"
                  :limit="1"
                />
              </el-form-item>
              <div style="text-align: center">
                <!--身份证国徽面-->
                {{ t("admin.form.idCardEmblem") }}
              </div>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="idImage.2">
                <ImageUpload
                  v-model="modelValue.idImage[2]"
                  :isShowTip="false"
                  :limit="1"
                />
              </el-form-item>
              <div style="text-align: center">
                <!--手持身份证人像面-->
                {{ t("admin.validation.portraitOfTheIdCard") }}
              </div>
            </el-col>
          </el-row>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="isAdd">
      <el-col :span="24">
        <el-form-item
          :label="t('admin.form.achievementBroker')"
          prop="certificatePics"
        >
          <el-row :gutter="20">
            <el-col :span="8">
              <image-upload
                v-model="modelValue.certificatePics"
                :isShowTip="false"
              />
            </el-col>
          </el-row>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script setup>
import InputBoxAdd from "../InputBoxAdd";
import FieldOptions from "../FieldOptions";
import { computed, reactive, ref } from "vue";
import { useI18n } from "vue-i18n";

const { t } = useI18n();
const formRef = ref();
const researchsFormRef = ref();
const keywordsFormRef = ref();
const props = defineProps({
  modelValue: Object,
  isAdd: {
    type: Boolean,
    default: true,
  },
  showTitle: {
    type: Boolean,
    default: false,
  },
  labelWidth: {
    type: Number,
    default: 120,
  },
});
const rules = reactive({
  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", { type: t("admin.form.mobile") })
      ),
      trigger: "blur",
    },
    // {
    //   pattern: /^1[3-9]\d{9}$/,
    //   message: "请输入正确的手机号码",
    //   trigger: "blur",
    // },
  ],
  idCard: [
    // {
    //   required: true,
    //   message: computed(() => t("admin.form.placeholder")),
    //   trigger: "blur",
    // },
  ],
  wordAddress: [
    {
      required: true,
      message: computed(() => t("admin.form.placeholder")),
      trigger: "blur",
    },
  ],
  experience: [
    {
      required: true,
      message: computed(() => t("admin.form.placeholder")),
      trigger: "blur",
    },
  ],
  "idImage.0": [
    // {
    //   required: true,
    //   message: computed(() => t("admin.validation.pleaseUpload")),
    //   trigger: ["blur", "change"],
    // },
  ],
  "idImage.1": [
    // {
    //   required: true,
    //   message: computed(() => t("admin.validation.pleaseUpload")),
    //   trigger: ["blur", "change"],
    // },
  ],
  "idImage.2": [
    // {
    //   required: true,
    //   message: computed(() => t("admin.validation.pleaseUpload")),
    //   trigger: ["blur", "change"],
    // },
  ],
  certificatePics: [
    {
      required: true,
      message: computed(() => t("admin.validation.pleaseUpload")),
      trigger: ["blur", "change"],
    },
  ],
});

const validateForm = async () => {
  let formValid;
  try {
    formValid = await formRef.value.validate();
  } catch (error) {
    formValid = false;
  }
  const researchsFormValid = await researchsFormRef.value.validateForm();
  const keywordsFormValid = await keywordsFormRef.value.validateForm();
  return formValid && researchsFormValid && keywordsFormValid;
};

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

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

:deep(.hide) {
  height: 148px;
}

// 上传图片框限制
// ::v-deep .el-upload--picture-card {
//   width: 120px;
//   height: 120px;
//   line-height: 120px;
// }
.el-select,
.el-date-editor {
  display: block;
  width: 100%;
}
</style>
