<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-col :span="24">
        <el-form-item label="机构logo:">
          <ImageUpload v-model="modelValue.image" :limit="1" />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="isAdd">
      <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 v-if="isAdd">
      <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>

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

    <el-row>
      <el-col :span="12">
        <el-form-item label="详细地址:">
          <el-input v-model="modelValue.address"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <field-options
      ref="fieldFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
    />

    <InputBoxAdd
      ref="directionsFormRef"
      v-model="modelValue"
      :labelWidth="labelWidth"
      fieldKey="researchs"
      placeholder="请输入研究方向"
      title="研究方向"
    />

    <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">
          <editor
            ref="introduceRef"
            v-model="modelValue.introduce"
            :minHeight="150"
            @blur="handleBlur"
          />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script setup>
import { ref } from "vue";
import Editor from "@/components/WangEditor";
import CityOptions from "../CityOptions";
import FieldOptions from "../FieldOptions";
import InputBoxAdd from "../InputBoxAdd";

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

const handleBlur = (value) => {
  console.log(value);
};
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|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" }],
  workAt: [
    {
      required: true,
      message: "从业时间不能为空",
      trigger: ["change", "blur"],
    },
  ],
  license: [
    {
      required: true,
      message: "请上传",
      trigger: ["blur", "change"],
    },
  ],
  introduce: [
    {
      required: true,
      message: "请输入",
      trigger: ["change", "blur"],
    },
  ],
};

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 directionsFormValid = await directionsFormRef.value.validateForm();
  // const introduceValid = await introduceRef.value.validateForm();
  return (
    formValid && cityFormValid && fieldFormValid && directionsFormValid
    // introduceValid
  );
};
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>
