<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="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="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="keyword" />
      <el-table-column label="出版日期" align="center">
        <template #default="{ row }">
          <span>{{ dayjs(row.publishTime).format("YYYY-MM-DD") }}</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="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="publishTime">
          <el-date-picker
            v-model="form.publishTime"
            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="keywordsFormRef"
        />
        <el-form-item label="摘要:" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
        <!-- //TODO:上传论文添加字段 -->
        <el-form-item label="上传论文:" prop="fileUrl">
          <FileUpload
            v-model="form.fileUrl"
            :limit="1"
            :fileType="['docx', 'doc', 'pdf']"
          ></FileUpload>
        </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/components/InputBoxAdd";
import {
  paperList,
  insertCasPaper,
  deleteCasPaper,
  updateCasPaper,
} from "@/api/admin/expert/technology";
import dayjs from "dayjs";
import { cloneDeep } from "lodash";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
const router = useRouter();

const dataList = ref([]);
const showEditDialog = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const paperRef = ref();
const queryRef = ref();
const keywordsFormRef = ref();
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    // title: undefined,
  },
  rules: {
    title: [{ required: true, message: "论文题目不能为空", trigger: "blur" }],
    ext: [{ required: true, message: "刊物不能为空", trigger: "blur" }],
    author: [{ required: true, message: "作者不能为空", trigger: "blur" }],
    publishTime: [
      {
        required: true,
        message: "请选择出版日期",
        trigger: ["blur", "change"],
      },
    ],
    remark: [
      {
        required: true,
        message: "请输入摘要",
        trigger: "blur",
      },
    ],
  },
});

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

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

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    // id: undefined,
    // title: undefined,
    // ext: undefined,
    // author: undefined,
    // publish_at: undefined,
    // keywords: [],
    // remark: undefined,
  };
  if (paperRef.value) {
    paperRef.value.resetFields();
  }
}
/** 添加数据 */
function handleAdd() {
  reset();
  showEditDialog.value = true;
  title.value = "添加论文";
}
/** 修改数据 */
function handleUpdate(row) {
  // form.value = JSON.parse(JSON.stringify(row));
  form.value = cloneDeep(row);
  form.value.keywords = [...form.value.keyword?.split(",")];
  showEditDialog.value = true;
  title.value = "修改论文";
}
/** 提交按钮 */
function submitForm() {
  paperRef.value.validate(async (valid) => {
    const keywordsValid = keywordsFormRef.value.validateForm();
    if (valid && keywordsValid) {
      form.value.keyword = form.value.keywords.join(",");
      if (form.value.id) {
        await updateCasPaper(form.value);
        ElMessage.success("修改成功");
        showEditDialog.value = false;
        getList();
      } else {
        await insertCasPaper(form.value);
        ElMessage.success("新增成功");
        showEditDialog.value = false;
        getList();
      }
    } else {
      console.log("校验未通过");
    }
  });
}
/** 取消按钮 */
function cancel() {
  showEditDialog.value = false;
  reset();
}

getList();
</script>
