<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      @submit.prevent
    >
      <el-form-item label="论文题目" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入论文题目"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" size="mini" @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="mini"
          @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="title"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="刊物" prop="ext" :show-overflow-tooltip="true" />
      <el-table-column label="作者" prop="author" />
      <el-table-column label="关键词" align="center" prop="keywords" />
      <el-table-column label="出版日期" align="center" prop="created_at">
        <template #default="scope">
          <span>{{ parseTime(scope.row.created_at) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</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="open" width="800px" append-to-body>
      <el-form ref="paperRef" :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="ext">
          <el-input v-model="form.ext" placeholder="请输入刊物" />
        </el-form-item>
        <el-form-item label="作者:" prop="author">
          <el-input v-model="form.author" placeholder="多人员请用 , 隔开" />
        </el-form-item>
        <el-form-item label="出版日期:" prop="publish_at">
          <el-date-picker
            v-model="form.publish_at"
            type="date"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择出版日期"
          ></el-date-picker>
        </el-form-item>
        <InputBoxAdd
          :labelWidth="100"
          v-model="form"
          title="关键词"
          placeholder="应用场景关键词+技术产品关键词"
          fieldKey="keywords"
          ref="keywordsForm"
        />
        <el-form-item label="摘要:">
          <el-input
            v-model="form.remark"
            type="textarea"
            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 InputBoxAdd from "@/views/identity/components/InputBoxAdd";
import {
  expertPaper,
  paperAdd,
  paperEdit,
  paperDelete,
} from "@/api/admin/expert/technology";

const router = useRouter();
const { proxy } = getCurrentInstance();

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

const data = reactive({
  form: {},
  queryParams: {
    page_num: 1,
    page_size: 10,
    title: undefined,
  },
  rules: {
    title: [{ required: true, message: "论文题目不能为空", trigger: "blur" }],
    ext: [{ required: true, message: "刊物不能为空", trigger: "blur" }],
    author: [{ required: true, message: "作者不能为空", trigger: "blur" }],
    publish_at: [
      {
        required: true,
        message: "请选择出版日期",
        trigger: ["blur", "change"],
      },
    ],
  },
});

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

/** 查询数据列表 */
function getList() {
  loading.value = true;
  expertPaper(queryParams.value).then((response) => {
    dataList.value = response.data.data;
    total.value = response.data.count;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除数据编号为"' + row.id + '"的数据项?')
    .then(function () {
      return paperDelete({ id: row.id });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    id: undefined,
    title: undefined,
    ext: undefined,
    author: undefined,
    publish_at: undefined,
    keywords: [],
    remark: undefined,
  };
  proxy.resetForm("paperRef");
}
/** 添加数据 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加数据";
}
/** 修改数据 */
function handleUpdate(row) {
  form.value = JSON.parse(JSON.stringify(row));
  open.value = true;
  title.value = "修改数据";
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["paperRef"].validate((valid) => {
    const keywordsForm = proxy.$refs.keywordsForm.submitForm();
    if (valid && keywordsForm) {
      if (form.value.id != undefined) {
        paperEdit(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        paperAdd(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

getList();
</script>
