<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">
      <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="title"
      />
      <el-table-column :show-overflow-tooltip="true" label="刊物" prop="ext" />
      <el-table-column label="所属专家" prop="author" />
      <el-table-column align="center" label="关键词" prop="keyword" />
      <!--      <el-table-column align="center" label="浏览量" prop="readCount"/>-->
      <el-table-column align="center" label="出版日期" prop="publishTime">
        <template #default="{ row }">
          <span>{{ dayjs(row.publishTime).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="Delete"
            size="small"
            type="text"
            @click="handleDelete(row.id)"
            >删除
          </el-button>
          <el-button
            icon="view"
            size="small"
            type="text"
            @click="handleBrowseUnitInfo(row)"
            >浏览单位信息
          </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="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.publishAt"
            placeholder="请选择出版日期"
            type="date"
            value-format="YYYY-MM-DD HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <InputBoxAdd
          ref="keywordsFormRef"
          v-model="form"
          :labelWidth="100"
          fieldKey="keywords"
          placeholder="应用场景关键词+技术产品关键词"
          title="关键词"
        />
        <el-form-item label="摘要:">
          <el-input
            v-model="form.remark"
            placeholder="请输入内容"
            type="textarea"
          ></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 dayjs from "dayjs";
import { ElMessage, ElMessageBox } from "element-plus";
import InputBoxAdd from "@/views/components/InputBoxAdd";
import { useRouter } from "vue-router";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";
import { cloneDeep } from "lodash";
import { deletePaper, paperList } from "@/api/admin/laboratory/paper";

const router = useRouter();

const { proxy } = getCurrentInstance();
const paperRef = ref();
const keywordsFormRef = ref();

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: {
    pageNum: 1,
    pageSize: 10,
    name: 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);

const queryRef = ref();

/** 查询数据列表 */
const getList = async () => {
  loading.value = true;
  paperList(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 handleDelete(id) {
  ElMessageBox.confirm('是否确认删除数据编号为"' + id + '"的论文项?')
    .then(function () {
      return deletePaper(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,
  };
  paperRef.value?.resetFields();
}

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

const handleEdit = (row) => {
  form.value = cloneDeep(row);
  open.value = true;
  title.value = "修改数据";
};

/** 提交按钮 */
function submitForm() {
  paperRef.value?.validate((valid) => {
    const keywordsFormValid = keywordsFormRef.value.validateForm();
    if (valid && keywordsFormValid) {
      if (form.value.id) {
        // TODO
        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();
        });
      }
      */
    }
  });
}

const handleBrowseUnitInfo = (row) => {
  router.push({
    path: "/admin/research/paper/unit-info",
    query: {
      id: row.id,
    },
  });
};

getList();
</script>
