<template>
  <el-form
    ref="form"
    :model="value"
    :rules="rules"
    :label-width="labelWidth + 'px'"
  >
    <div class="form_title" v-if="showTitle">基本信息</div>
    <el-row>
      <el-col :span="24">
        <el-form-item :label="imageName" prop="image">
          <ImageUpload v-model="value.image" :fileSize="2" :limit="1" />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 3 || formType == 2">
      <el-col :span="24">
        <el-form-item :label="nameName" prop="name">
          <el-input
            v-model="value.name"
            :disabled="isAdd ? false : true"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 3 || formType == 2">
      <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="value.code" :disabled="!isAdd"></el-input>
            </el-col>
            <el-col :span="3">
              <el-button type="primary" @click="" disabled v-if="isAdd"
                >查找</el-button
              >
            </el-col>
          </el-row>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 2">
      <el-col :span="24">
        <el-form-item label="企业类型:" prop="kind">
          <el-select v-model="value.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="value.product"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 1">
      <el-col :span="12">
        <el-form-item label="姓名:" prop="name">
          <el-input
            v-model="value.name"
            :disabled="isAdd ? false : true"
          ></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="手机号:" prop="mobile">
          <el-input
            v-model="value.mobile"
            :disabled="isAdd ? false : true"
          ></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item label="所属站点:" prop="tenant_id">
          <SiteOptions :size="''" :limitWidth="false" v-model="value" />
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 1">
      <el-col :span="12">
        <el-form-item label="所属单位:" prop="research_id">
          <el-select
            v-model="value.research_id"
            filterable
            placeholder="请选择"
            @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="value.laboratory_id"
            filterable
            placeholder="请选择"
          >
            <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>

    <CityOptions v-model="value" :labelWidth="labelWidth" ref="cityForm" />

    <el-row>
      <el-col :span="24">
        <el-form-item label="详细地址:">
          <el-input v-model="value.address"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 1">
      <el-col :span="24">
        <el-form-item label="毕业院校:" prop="school">
          <el-input v-model="value.school"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 1">
      <el-col :span="12">
        <el-form-item label="最高学历:" prop="education">
          <el-select v-model="value.education" placeholder="请选择">
            <el-option
              v-for="item in educationOptions"
              :key="item.text"
              :label="item.text"
              :value="item.text"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="主修专业:" prop="major">
          <el-input v-model="value.major"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 1">
      <el-col :span="12">
        <el-form-item label="职务:" prop="job">
          <el-input v-model="value.job"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="职称:" prop="title">
          <el-input v-model="value.title"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 1">
      <el-col :span="12">
        <el-form-item label="从业时间:" prop="work_at">
          <el-date-picker
            v-model="value.work_at"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="性别:" prop="gender">
          <el-radio v-model="value.gender" :label="1">男</el-radio>
          <el-radio v-model="value.gender" :label="2">女</el-radio>
        </el-form-item>
      </el-col>
    </el-row>

    <FieldOptions v-model="value" :labelWidth="labelWidth" ref="fieldForm" />

    <InputBoxAdd
      v-if="formType == 1 || formType == 2"
      :labelWidth="labelWidth"
      v-model="value"
      title="关键词"
      placeholder="应用场景关键词+技术产品关键词"
      fieldKey="keywords"
      ref="keywordsForm"
    />

    <InputBoxAdd
      v-if="formType != 2"
      :labelWidth="labelWidth"
      v-model="value"
      title="研究方向"
      placeholder="请输入研究方向"
      fieldKey="researchs"
      ref="researchsForm"
    />

    <InputBoxAdd
      v-if="formType == 2"
      :labelWidth="labelWidth"
      v-model="value"
      title="生产方向"
      placeholder="请输入生产方向"
      fieldKey="directions"
      ref="directionsForm"
    />

    <el-row v-if="formType == 2">
      <el-col :span="24">
        <el-form-item label="邀请码:">
          <el-input v-model="value.inviter_code" disabled></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="企业网站:">
          <el-input v-model="value.url"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="formType == 3 || formType == 2">
      <el-col :span="24">
        <el-form-item label="营业执照:" prop="license">
          <ImageUpload v-model="value.license" :isShowTip="false" :limit="1" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item :label="introduceName" prop="introduce">
          <el-input
            v-if="formType == 1"
            v-model="value.introduce"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 6 }"
          />
          <Editor
            v-else
            v-model="value.introduce"
            :minHeight="150"
            ref="introduceRef"
            @click.native="handleEditAble"
          />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import CityOptions from "@/views/components/CityOptions";
import FieldOptions from "@/views/components/FieldOptions";
import InputBoxAdd from "@/views/components/InputBoxAdd";
import { researchSelect } from "@/api/dataList/research";
import { laboratorySelect } from "@/api/dataList/laboratory";
export default {
  components: {
    CityOptions,
    FieldOptions,
    InputBoxAdd,
  },
  props: {
    value: Object,
    isAdd: {
      type: Boolean,
      default: true,
    },
    showTitle: {
      type: Boolean,
      default: false,
    },
    formType: {
      type: Number,
      default: 1,
    },
    labelWidth: {
      type: Number,
      default: 120,
    },
  },
  computed: {
    introduceName() {
      let str = "";
      switch (this.formType) {
        case 1:
          str = "个人简介:";
          break;
        case 2:
          str = "单位简介:";
          break;
        case 3:
          str = "机构简介:";
          break;
        case 4:
          str = ":";
          break;
        case 5:
          str = ":";
          break;
        default:
          break;
      }
      return str;
    },
    imageName() {
      let str = "";
      switch (this.formType) {
        case 1:
          str = "专家头像:";
          break;
        case 2:
          str = "企业logo:";
          break;
        case 3:
          str = "机构logo:";
          break;
        case 4:
          str = ":";
          break;
        case 5:
          str = ":";
          break;
        default:
          break;
      }
      return str;
    },
    nameName() {
      let str = "";
      switch (this.formType) {
        case 2:
          str = "单位名称:";
          break;
        case 3:
          str = "机构名称:";
          break;
          break;
        default:
          break;
      }
      return str;
    },
  },
  data() {
    return {
      researchOptions: [],
      options: [
        // { value: "", label: "" }
      ],
      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" }],
        work_at: [
          {
            required: true,
            message: "从业时间不能为空",
            trigger: ["change", "blur"],
          },
        ],
        license: [
          {
            required: true,
            message: "请上传",
            trigger: ["blur", "change"],
          },
        ],
        introduce: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },
  watch: {
    "value.tenant_id"(newOld) {
      console.log(newOld);
      if (newOld) {
        if (this.formType == 1) {
          // this.formType == 1 专家表单
          // 获取所属单位-研究机构options
          researchSelect({ tenant_id: newOld }).then(({ data }) => {
            this.researchOptions = data;
          });
        }
      } else {
        this.researchOptions = [];
        if (this.value.research_id) this.value.research_id = undefined;
      }
    },
  },
  mounted() {
    console.log(this.formType);
    if (this.formType != 1) {
      // 取消富文本自动聚焦
      this.$nextTick(() => {
        this.$refs.introduceRef.Quill.enable(false);
      });
    }
  },
  methods: {
    // 富文本聚焦
    handleEditAble() {
      this.$refs.introduceRef.Quill.enable(true);
      this.$refs.introduceRef.Quill.focus();
    },
    setLaboratory(e) {
      laboratorySelect({ research_id: e }).then((res) => {
        console.log(res);
      });
    },
    submitForm() {
      let flag = false;
      if (this.formType == 1) {
        this.$refs["form"].validate((valid) => {
          const cityForm = this.$refs.cityForm.submitForm(); // 城市
          const fieldForm = this.$refs.fieldForm.submitForm(); // 所属领域
          const keywordsForm = this.$refs.keywordsForm.submitForm(); // 关键词
          const researchsForm = this.$refs.researchsForm.submitForm(); // 研究方向
          if (valid && cityForm && fieldForm && keywordsForm && researchsForm) {
            flag = !flag;
          }
        });
      } else if (this.formType == 2) {
        this.$refs["form"].validate((valid) => {
          const cityForm = this.$refs.cityForm.submitForm();
          const fieldForm = this.$refs.fieldForm.submitForm();
          const keywordsForm = this.$refs.keywordsForm.submitForm();
          const directionsForm = this.$refs.directionsForm.submitForm();
          if (
            valid &&
            cityForm &&
            fieldForm &&
            keywordsForm &&
            directionsForm
          ) {
            flag = !flag;
          }
        });
      } else if (this.formType == 3) {
        this.$refs["form"].validate((valid) => {
          const cityForm = this.$refs.cityForm.submitForm();
          const fieldForm = this.$refs.fieldForm.submitForm();
          const researchsForm = this.$refs.researchsForm.submitForm();
          if (valid && cityForm && fieldForm && researchsForm) {
            flag = !flag;
          }
        });
      }
      return flag;
    },
  },
};
</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>