<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryRef"
      :inline="true"
      :model="queryParams"
      @submit.prevent
    >
      <el-form-item label="成果名称" prop="title">
        <el-input
          v-model="queryParams.title"
          clearable
          placeholder="请输入专利名称"
          size="small"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          icon="Search"
          size="small"
          type="primary"
          @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 icon="plus" size="small" type="primary">新增</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
        :show-overflow-tooltip="true"
        label="专家名称"
        prop="name"
      />
      <el-table-column label="联系电话" prop="mobile" />
      <el-table-column label="研究领域" prop="industryStr" />
      <!--      <el-table-column label="申请（专利权）人" prop="applyName"/>-->
      <el-table-column align="center" label="入驻时间" prop="workTime">
        <template #default="{ row }">
          <span>{{ dayjs(row.workTime).format("YYYY-MM-DD") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        class-name="small-padding fixed-width"
        label="操作"
      >
        <template #default="{ row }">
          <!--          <el-button-->
          <!--              icon="edit"-->
          <!--              size="small"-->
          <!--              type="text"-->
          <!--              @click="handleDeleteBind(row.id)"-->
          <!--          >编辑-->
          <!--          </el-button-->
          <!--          >-->
          <el-button
            icon="Delete"
            size="small"
            type="text"
            @click="handleDeleteBind(row.id)"
            >取消关联
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
    <!-- 添加或修改数据配置对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="800px">
      <el-form ref="expertRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="专家姓名" prop="title">
          <el-input v-model="form.title" placeholder="请输入专家姓名" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input v-model="form.gender" placeholder="请输入性别" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="单位" />
        </el-form-item>
        <el-form-item label="研究方向" prop="researchDirection">
          <InputBoxAdd
            ref="researchDirectionFormRef"
            v-model="form"
            :labelWidth="100"
            fieldKey="researchs"
            placeholder="研究方向"
            title="研究方向"
          />
        </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 dayjs from "dayjs";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
import { reactive, ref, toRefs } from "vue";
import InputBoxAdd from "@/views/components/InputBoxAdd/index.vue";
import { deleteBindExpert, expertList } from "@/api/admin/research/expert";

const router = useRouter();

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const expertRef = ref();
const researchDirectionFormRef = ref();
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inventor: undefined,
  },
  form: {},
  rules: {},
});

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

const queryRef = ref();

/** 查询数据列表 */
const getList = async () => {
  loading.value = true;
  expertList(queryParams.value)
    .then((resp) => {
      dataList.value = resp.rows;
      total.value = resp.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
function resetQuery() {
  queryRef.value.resetFields();
  handleQuery();
}

/** 删除按钮操作 */
function handleDeleteBind(id) {
  ElMessageBox.confirm(`是否确认取消关联数据编号为"${id}"的专家?`)
    .then(function () {
      return deleteBindExpert(id);
    })
    .then(() => {
      getList();
      ElMessage.success("取消关联成功");
    })
    .catch((err) => {
      console.log(err);
    });
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    // id: undefined,
    // title: undefined,
    // ext: undefined,
    // author: undefined,
    // publish_at: undefined,
    // keywords: [],
    // remark: undefined,
  };
  expertRef.value?.resetFields();
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

const handleAdd = () => {
  reset();
  title.value = "添加专家";
  open.value = true;
};
const handleUpdate = () => {
  reset();
  title.value = "编辑专家";
  open.value = true;
};

/** 提交按钮 */
function submitForm() {
  expertRef.value?.validate((valid) => {
    const keywordsFormValid = researchDirectionFormRef?.value.validateForm();
    if (valid && keywordsFormValid) {
      if (form.value.id) {
        // TODO
        paperEdit(form.value).then((response) => {
          ElMessage.success("修改成功");
          open.value = false;
          getList();
        });
      } else {
        paperAdd(form.value).then((response) => {
          ElMessage.success("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

getList();
</script>
