<template>
  <el-form
    ref="formRef"
    :label-width="labelWidth + 'px'"
    :model="modelValue"
    :rules="rules"
  >
    <div v-if="showTitle" class="form_title">基本信息</div>

    <!-- <el-row v-if="isAdd"> -->
    <el-row>
      <el-col :span="24">
        <el-form-item label="企业logo:">
          <ImageUpload v-model="modelValue.image" :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"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="联系人" prop="username">
          <el-input v-model="modelValue.username"></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"
            :maxlength="11"
            oninput="
                value = value
                  .replace(/[^\d.]/g, '')
                  .replace(/\.{2,}/g, '.')
                  .replace('.', '$#$')
                  .replace(/\./g, '')
                  .replace('$#$', '.')
                  .replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
                  .replace(/^\./g, '')
              "
          ></el-input>
          <!-- v-number -->
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="邮箱" 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="信用代码:" prop="code">
          <el-row type="flex" justify="space-between">
            <el-col :span="20">
              <el-input v-model="modelValue.code"></el-input>
            </el-col>
            <el-col :span="3">
              <el-button type="primary" @click="">查找</el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-col>
    </el-row> -->
    <el-row>
      <el-col :span="24">
        <el-form-item label="企业类型:" prop="kind">
          <el-select v-model="modelValue.kind" placeholder="请选择">
            <el-option
              v-for="item in enterpriseOptions"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="核心成果核心产品:" prop="product">
          <el-input v-model="modelValue.product"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <CityOptions
      ref="cityFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
    />

    <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>

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

    <InputBoxAdd
      ref="keywordsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      fieldKey="keywords"
      placeholder="应用场景关键词+技术产品关键词"
      title="关键词"
    />
    <InputBoxAdd
      v-if="isAdd"
      ref="directionsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      fieldKey="directions"
      placeholder="请输入生产方向"
      title="生产方向"
    />

    <el-row>
      <!-- <el-col :span="24">
        <el-form-item label="邀请码:">
          <el-input v-model="modelValue.inviter_code"></el-input>
        </el-form-item>
      </el-col> -->
      <el-col :span="24">
        <el-form-item label="企业网站:">
          <el-input v-model="modelValue.url"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="营业执照:" prop="license">
          <image-upload
            v-model="modelValue.license"
            :isShowTip="false"
            :limit="1"
          />
        </el-form-item>
      </el-col>
    </el-row>
    <!-- <el-row v-if="isAdd">
      <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="300"
            @blur="formRef.validateField(`introduce`)"
          />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script name="EnterpriseForm" setup>
import CityOptions from "../CityOptions";
import FieldOptions from "../FieldOptions";
import InputBoxAdd from "../InputBoxAdd";
// import { researchSelect, laboratorySelect } from "@/api/identity/index";
import WangEditor from "@/components/WangEditor";
import { enterpriseOptions } from "@/utils/parameter";
import { reactive, ref, toRefs } from "vue";
import ImageUpload from "@/components/ImageUpload/index.vue";

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 vNumber = {
  mounted: (el, _binding, vnode, _prevVnode) => {
    console.log(vnode);
    el.children[0].addEventListener("keypress", (e) => {
      // console.log(e);
      if (/\D/.test(e.key)) {
        e.preventDefault();
      }
    });
    // el.addEventListener("input", (e) => {
    //   console.log(e);
    //   e.srcElement.value = e.srcElement.value.replace("3", "");
    //   // value = value.replace("3", "");
    // });
  },
};
const data = reactive({
  rules: {
    product: [{ required: true, message: "请输入", trigger: "blur" }],
    name: [{ required: true, message: "请输入", trigger: "blur" }],
    username: [{ required: true, message: "请输入联系人", trigger: "blur" }],
    phone: [
      { required: true, message: "请输入联系方式", trigger: "blur" },
      {
        pattern: /^1[3456789]\d{9}$/,
        message: "手机号格式不正确",
        trigger: "blur",
      },
    ],
    email: [
      { required: true, message: "请输入邮箱", trigger: "blur" },
      {
        pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
        message: "邮箱格式不正确",
        trigger: "blur",
      },
    ],
    kind: [{ required: true, message: "请选择", trigger: "change" }],
    code: [{ required: true, message: "请输入", trigger: "blur" }],
    mobile: [
      { required: true, message: "请输入", trigger: "blur" },
      {
        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        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: ["change", "blur"],
      },
    ],
  },
});
const { rules } = toRefs(data);

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

/**
 * validate form COMPLETE
 * @return {Promise<false|*>}
 */
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(); // 关键词表单验证
  if (isAdd.value) {
    const directionsFormValid = await directionsFormRef.value.validateForm();
    return (
      formValid &&
      cityFormValid &&
      fieldFormValid &&
      keywordsFormValid &&
      directionsFormValid
    );
  } else {
    return formValid && cityFormValid && fieldFormValid && keywordsFormValid;
  }
};
defineExpose({
  validateForm,
});
</script>

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

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