<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      @submit.prevent
    >
      <el-form-item label="企业名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入企业名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="small" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" size="small" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form> -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          size="small"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="数据编号" prop="id" />
      <el-table-column
        label="企业名称"
        prop="enterpriseName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="所在地"
        prop="address"
        :show-overflow-tooltip="true"
      />
      <!-- province--city--district -->
      <el-table-column label="所属领域" prop="territoryStr" />
      <el-table-column label="联系人" prop="contactUser" />
      <el-table-column label="联系电话" prop="contactPhone" />
      <!-- <el-table-column label="创建日期" align="center" prop="created_at">
        <template #default="{ row }">
          <span>{{ dayjs(row.created_at).format("YYYY-MM-DD HH:mm:ss") }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-button
            size="small"
            type="text"
            icon="Edit"
            @click="handleUpdate(row)"
            >修改</el-button
          >
          <el-button
            size="small"
            type="text"
            icon="Delete"
            @click="handleDelete(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据配置对话框 -->
    <el-dialog
      :title="title"
      v-model="showEditDialog"
      width="750px"
      append-to-body
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="企业名称:" prop="enterpriseName">
          <el-input
            v-model="form.enterpriseName"
            placeholder="请输入企业名称"
          />
        </el-form-item>
        <el-form-item label="联系人:">
          <el-input v-model="form.contactUser" placeholder="请输入专家姓名" />
        </el-form-item>
        <el-form-item label="联系电话:">
          <el-input v-model="form.contactPhone" placeholder="默认专家电话" />
        </el-form-item>
        <el-form-item label="所在地">
          <el-input v-model="form.address" placeholder="请输入所在地" />
        </el-form-item>
        <!-- <div class="hide_required">
          <CityOptions v-model="form" :labelWidth="100" :is_required="false" />
        </div> -->

        <div class="hide_required">
          <FieldSingle v-model="form" :labelWidth="100" />
        </div>

        <el-form-item label="合作内容:" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6 }"
            placeholder="请输入想要合作的需求"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import CityOptions from "@/views/components/CityOptions";
import FieldSingle from "@/views/components/FieldSingle";

import {
  expertWantEnterpriseList,
  updateExpertWantEnterprise,
  insertExpertWantEnterprise,
  deleteExpertWantEnterpriseByIds,
} from "@/api/admin/expert/demand";
import dayjs from "dayjs";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { cloneDeep } from "lodash-unified";
const router = useRouter();
const { proxy } = getCurrentInstance();

const dataList = ref([]);
const showEditDialog = ref(false);
const loading = ref(true);
// const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const formRef = ref();

// const formData = reactive({
//   industrys: [],
// });

const data = reactive({
  form: {
    industrys: [],
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    // mode: 2,
  },
  rules: {
    enterpriseName: [
      { required: true, message: "企业名称不能为空", trigger: "blur" },
    ],
    content: [{ required: true, message: "合作内容不能为空", trigger: "blur" }],
  },
});

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

/** 查询数据列表 */
const getList = async () => {
  loading.value = true;
  const resp = await expertWantEnterpriseList(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  loading.value = false;
};
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
// function resetQuery() {
//   proxy.resetForm("queryRef");
//   handleQuery();
// }
/** 删除按钮操作 */
function handleDelete(id) {
  ElMessageBox.confirm(`是否确认删除数据编号为"${id}"的数据项?`).then(
    async () => {
      await deleteExpertWantEnterpriseByIds({ ids: [id] });
      getList();
      ElMessage.success("删除成功");
    }
  );
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    // id: undefined,
    // name: undefined,
    // contact: undefined,
    // contact_mobile: undefined,
    industrys: [],
    // mode: 2,
    // content: undefined,
  };
  if (formRef.value) {
    formRef.value.resetFields();
  }
}
/** 添加数据 */
function handleAdd() {
  reset();
  showEditDialog.value = true;
  title.value = "添加数据";
}
/** 修改数据 */
function handleUpdate(row) {
  reset();
  form.value = cloneDeep(row);
  form.value.industrys =
    form.value.territory
      ?.split("-")
      .filter((el) => el != "")
      .map((el) => parseInt(el)) ?? [];
  showEditDialog.value = true;
  title.value = "修改数据";
}

/** 提交按钮 */
function submitForm() {
  formRef.value.validate(async (valid) => {
    form.value.territory = form.value.industrys.join("-");
    if (valid) {
      if (form.value.id) {
        await updateExpertWantEnterprise(form.value);
        ElMessage.success("修改成功");
        showEditDialog.value = false;
        getList();
      } else {
        await insertExpertWantEnterprise(form.value);
        ElMessage.success("修改成功");
        showEditDialog.value = false;
        getList();
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  showEditDialog.value = false;
  reset();
}

getList();
</script>
<style lang="scss" scoped>
.hide_required {
  ::v-deep .el-form-item__label {
    &::before {
      display: none;
    }
  }
}
</style>
