<template>
  <div class="app-container">
    <el-card shadow="always" style="width: 55%; margin: 0 auto">
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        :label-width="labelWidth + 'px'"
      >
        <p><b>基本信息</b></p>

        <el-row>
          <el-col :span="24">
            <el-form-item label="成果需求名称:" prop="title">
              <el-input v-model="form.title"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="需求类别:">
              <el-checkbox-group v-model="form.kinds">
                <el-checkbox
                  v-for="item in checkList"
                  :key="item.id"
                  :label="item.name"
                  >{{ item.name }}</el-checkbox
                >
                <!-- <el-checkbox label="0" @change="handleCheck">其他</el-checkbox> -->
              </el-checkbox-group>
              <el-row :gutter="20">
                <el-col :span="20">
                  <el-input
                    v-model="checkInput"
                    placeholder="请输入需求类别"
                  ></el-input>
                </el-col>
                <el-col :span="4">
                  <el-button type="primary" @click="addCheck">添加</el-button>
                </el-col>
              </el-row>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="需求描述:" prop="introduce">
              <!-- <Editor v-model="form.add" :minHeight="150" /> -->
              <wangEditor
                v-model="form.introduce"
                width="100%"
                min-height="150px"
              ></wangEditor>
            </el-form-item>
          </el-col>
        </el-row>

        <!-- <CityOptions
          v-model="form"
          :labelWidth="labelWidth"
          ref="cityForm"
        /> -->
        <FieldOptions
          v-model="form"
          :labelWidth="labelWidth"
          ref="fieldFormRef"
        />

        <el-row>
          <el-col :span="12">
            <el-form-item
              label="成果需求预算:"
              :prop="form.budgetMode == 1 ? 'budget' : ''"
            >
              <el-input
                v-model="form.budget"
                oninput="value=value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '')"
                placeholder="请输入技术需求预算"
              >
                <template #append>万元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="50px">
              <el-checkbox
                false-label="1"
                true-label="2"
                v-model="form.budgetMode"
                >面议</el-checkbox
              >
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="截止时间:" prop="deadline">
              <el-date-picker
                style="width: 100%"
                v-model="form.deadline"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择截止时间"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="需求联系人:" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入需求联系人"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人手机号:" prop="mobile">
              <el-input
                v-model="form.mobile"
                placeholder="请输入联系人手机号"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="需求提交人:" prop="commitUserName">
              <el-input
                v-model="form.commitUserName"
                placeholder="请输入需求提交人"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="需求提交人手机号:" prop="commitUserPhone">
              <el-input
                v-model="form.commitUserPhone"
                placeholder="请输入需求提交人手机号"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <InputBoxAdd
              :labelWidth="labelWidth"
              v-model="form"
              title="想合作的单位及模式"
              placeholder=""
              fieldKey="wants"
              ref="directionsFormRef"
            />
          </el-col>
        </el-row>
      </el-form>
      <div :style="{ marginLeft: labelWidth + 'px' }">
        <el-button @click="router.go(-1)">取消</el-button>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </div>
    </el-card>
  </div>
</template>
<script setup>
// import { expert } from "@/api/identity/index";
import { insertTechnologyDemand } from "@/api/enterprise";
import CityOptions from "@/views/components/CityOptions";
import FieldOptions from "@/views/components/FieldOptions";
import InputBoxAdd from "@/views/components/InputBoxAdd";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const router = useRouter();
const data = reactive({
  form: {
    check: [],
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postCode: undefined,
  },
  rules: {
    title: [{ required: true, message: "需求名称不能为空", trigger: "blur" }],
    introduce: [
      { required: true, message: "需求描述不能为空", trigger: "blur" },
    ],
    name: [{ required: true, message: "需求联系人不能为空", trigger: "blur" }],
    mobile: [
      { required: true, message: "联系人手机号不能为空", trigger: "blur" },
    ],
    commitUserName: [
      { required: true, message: "需求提交人不能为空", trigger: "blur" },
    ],
    budget: [{ required: true, message: "需求预算不能为空", trigger: "blur" }],
    deadline: [
      {
        required: true,
        message: "请选择截止时间",
        trigger: ["change", "blur"],
      },
    ],
    commitUserPhone: [
      { required: true, message: "需求提交人手机号不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

const { proxy } = getCurrentInstance();
const labelWidth = 160;
// const isContainOther = ref(false);
const checkList = reactive([
  {
    id: 1,
    name: "成果推广",
  },
  {
    id: 2,
    name: "关键成果解决",
  },
  {
    id: 3,
    name: "对接专家院士",
  },
  {
    id: 4,
    name: "上市辅导",
  },
]);

const formRef = ref(null);
const fieldFormRef = ref(null);
const directionsFormRef = ref(null);
const checkInput = ref("");
const submitForm = async () => {
  // await formRef.value.validate();
  let formValid;
  try {
    formValid = await formRef.value.validate();
  } catch (error) {
    formValid = false;
  }
  const fieldFormValid = await fieldFormRef.value.validateForm();
  const directionsFormValid = await directionsFormRef.value.validateForm();
  if (formValid && fieldFormValid && directionsFormValid) {
    await insertTechnologyDemand(form.value);
    ElMessage.success("新增企业需求成功");
    router.go(-1);
  } else {
    console.log("校验未通过");
  }
  // formRef.value.validate(async (valid) => {
  //   // console.log(valid);
  //   if (valid) {
  //     form.value.amount = form.value.amount - 0;
  //   }
  // form.value.amount = form.value.amount - 0;
  // const cityForm = proxy.$refs.cityForm.submitForm(); // 城市
  // if (valid && cityForm) {
  //   console.log(form.value);
  //   if (form.value.id != undefined) {
  //     // updatePost(form.value).then((response) => {
  //     //   proxy.$modal.msgSuccess("修改成功");
  //     // proxy.$router.go(-1);
  //     // });
  //   } else {
  //     // addPost(form.value).then((response) => {
  //     // proxy.$modal.msgSuccess("新增成功");
  //     // proxy.$router.go(-1);
  //     // });
  //   }
  // }
  // });
};
function addCheck() {
  if (!checkInput.value.trim().length) return proxy.$modal.msgError("请输入");
  const flag = checkList.some((item) => {
    return item.name.trim() == checkInput.value.trim();
  });
  if (!flag) {
    checkList.push({
      id: checkList.length + 1,
      name: checkInput.value,
    });
    checkInput.value = "";
  }
}
</script>
