<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="姓名:" 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="手机号:" prop="phone">
          <el-input v-model="modelValue.phone"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="isAdd">
      <el-col :span="24">
        <el-form-item label="身份证号:" 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="工作所在地:" 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="工作经历:" 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="应用场景关键词+技术产品关键词"
      title="关键词"
    />

    <el-row v-if="isAdd">
      <el-col :span="24">
        <el-form-item label="身份证:" required>
          <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">身份证人像面</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">身份证国徽面</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">手持身份证人像面</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="成果经纪资格证书:" 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 { ref } from "vue";

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 = {
  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",
    },
  ],
  idCard: [{ required: true, message: "请输入", trigger: "blur" }],
  wordAddress: [{ required: true, message: "请输入", trigger: "blur" }],
  experience: [{ required: true, message: "请输入", trigger: "blur" }],
  "idImage.0": [
    {
      required: true,
      message: "请上传",
      trigger: ["blur", "change"],
    },
  ],
  "idImage.1": [
    {
      required: true,
      message: "请上传",
      trigger: ["blur", "change"],
    },
  ],
  "idImage.2": [
    {
      required: true,
      message: "请上传",
      trigger: ["blur", "change"],
    },
  ],
  certificatePics: [
    {
      required: true,
      message: "请上传",
      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>
