<template>
  <div class="app-container">
    <el-card shadow="always" style="width: 55%; margin: 0 auto">
      <el-form
        ref="formRef"
        :label-width="labelWidth + 'px'"
        :model="formData"
        :rules="rules"
      >
        <p><b>基本信息</b></p>
        <el-row>
          <el-col :span="24">
            <el-form-item label="需求名称:" prop="title">
              <el-input v-model="formData.title"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="需求类别:" prop="kinds">
              <el-checkbox-group v-model="formData.kinds">
                <el-checkbox
                  v-for="item in checkList"
                  :key="item.id"
                  :label="item.name"
                  >{{ item.name }}
                </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="description">
              <!-- <Editor v-model="formData.description" :minHeight="150" /> -->
              <wangEditor
                ref="wangEditorRef"
                v-model="formData.description"
                min-height="150px"
                width="100%"
                @blur="formRef.validateField(`description`)"
              ></wangEditor>
            </el-form-item>
          </el-col>
        </el-row>

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

        <el-row>
          <el-col :span="12">
            <el-form-item label="需求联系人:" prop="name">
              <el-input
                v-model="formData.name"
                placeholder="请输入需求联系人"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人手机号:" prop="mobile">
              <el-input
                v-model="formData.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="formData.commitUserName"-->
        <!--                placeholder="请输入需求提交人"-->
        <!--              ></el-input>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--          <el-col :span="12">-->
        <!--            <el-form-item label="提交人手机号:" prop="commitPhone">-->
        <!--              <el-input-->
        <!--                v-model="formData.commitPhone"-->
        <!--                placeholder="请输入需求提交人手机号"-->
        <!--              ></el-input>-->
        <!--            </el-form-item>-->
        <!--          </el-col>-->
        <!--        </el-row>-->
      </el-form>
      <div :style="{ marginLeft: labelWidth + 'px' }">
        <el-button @click="back">取消</el-button>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </div>
    </el-card>
  </div>
</template>
<script setup>
// import { expert } from "@/api/identity/index";
import {
  getDemandById,
  insertDemand,
  updateDemand,
} from "@/api/admin/expert/demand";
import { demandCategoryList } from "@/utils/parameter";
import { uniqueId } from "lodash";
import tab from "@/plugins/tab";
// import CityOptions from "@/views/components/CityOptions";
import { ElMessage } from "element-plus";
import { useRoute } from "vue-router";
import { updateCount } from "@/api/admin/count.js";
import { reactive, ref, toRefs } from "vue";

const data = reactive({
  formData: {
    check: [],
    status: 1,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    // postCode: undefined,
  },
  rules: {
    kinds: [{ required: true, message: "请选择需求类别" }],
    title: [{ required: true, message: "需求名称不能为空", trigger: "blur" }],
    description: [
      {
        required: true,
        trigger: "blur",
        message: "需求描述不能为空",
      },
    ],
    bankAccount: [
      { required: true, message: "需求联系人不能为空", trigger: "blur" },
    ],
    bankPhone: [
      { required: true, message: "联系人手机号不能为空", trigger: "blur" },
    ],
    username: [
      { required: true, message: "需求提交人不能为空", trigger: "blur" },
    ],
    name: [{ required: true, message: "需求联系人不能为空", trigger: "blur" }],
    mobile: [
      { required: true, message: "需求联系人手机号不能为空", trigger: "blur" },
    ],
    // commitUserName: [
    //   { required: true, message: "需求提交人不能为空", trigger: "blur" },
    // ],
    // commitPhone: [
    //   { required: true, message: "需求提交人手机号不能为空", trigger: "blur" },
    // ],
  },
});

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

const route = useRoute();
const formRef = ref();

const back = () => {
  tab.closeOpenPage({ path: "/demand/serviceDemand" });
};
const labelWidth = 140;
// const isContainOther = ref(false);
const checkList = reactive([...demandCategoryList]);
const wangEditorRef = ref();
const checkInput = ref("");

async function submitForm() {
  console.log(wangEditorRef.value.isEmpty());
  await formRef.value.validate();
  if (formData.value.id) {
    // TODO: 修改专家需求
    await updateDemand(formData.value);
    ElMessage.success("修改成功");
  } else {
    await insertDemand(formData.value);
    ElMessage.success("新增成功");
  }
  updateCount("service");

  tab.closeOpenPage({ path: "/demand/serviceDemand" });
}

function addCheck() {
  if (!checkInput.value.trim().length) return ElMessage.error("请输入");
  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 = "";
  }
}

const getDetail = async () => {
  const resp = await getDemandById(route.query.id);
  formData.value = resp.data;
  for (const item of resp.data.kinds) {
    if (!checkList.find((el) => el.name === item)) {
      checkList.push({ id: uniqueId("unq-"), name: item });
    }
  }
};
if (route.query.id) {
  const obj = Object.assign({}, route, { title: "修改需求" });
  tab.updatePage(obj);
  getDetail();
}
</script>
