<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="kind" />
      <el-table-column label="课题角色" prop="role">
        <template #default="scope">
          <div v-if="scope.row.role == 1">承担单位</div>
          <div v-else-if="scope.row.role == 2">参与单位</div>
        </template>
      </el-table-column>
      <el-table-column label="课题负责人" prop="director" />
      <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="Download"
            @click="handleShelf(scope.row)"
            >下架</el-button
          >
          <!-- <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="720px" append-to-body>
      <el-form ref="roleRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="科研项目名称:" prop="title">
          <el-input v-model="form.title" placeholder="请输入科研项目名称" />
        </el-form-item>
        <el-form-item label="课题角色:" prop="role">
          <el-radio v-model="form.role" :label="1">承担单位</el-radio>
          <el-radio v-model="form.role" :label="2">参与单位</el-radio>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="资助经费:" prop="amount">
              <el-input
                v-model="form.amount"
                oninput="value=value.replace(/[^\d.]/g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3').replace(/^\./g, '')"
                placeholder="请输入资助经费"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资助来源:" prop="source">
              <el-input v-model="form.source" placeholder="请输入资助来源" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始日期:" prop="begin_at">
              <el-date-picker
                v-model="form.begin_at"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择开始日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期:" prop="finish_at">
              <el-date-picker
                v-model="form.finish_at"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目类型:" prop="kind">
              <el-input v-model="form.kind" placeholder="请输入项目类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课题负责人:" prop="director">
              <el-input
                v-model="form.director"
                placeholder="请输入课题负责人"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </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 {
  expertProject,
  projectAdd,
  projectEdit,
  projectShelf,
  projectDelete,
} 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" },
    ],
    amount: [{ required: true, message: "资助经费不能为空", trigger: "blur" }],
    source: [{ required: true, message: "资助来源不能为空", trigger: "blur" }],
    begin_at: [
      {
        required: true,
        message: "请选择开始日期",
        trigger: ["blur", "change"],
      },
    ],
    finish_at: [
      {
        required: true,
        message: "请选择结束日期",
        trigger: ["blur", "change"],
      },
    ],
    kind: [{ required: true, message: "项目类型不能为空", trigger: "blur" }],
    director: [
      { required: true, message: "课题负责人不能为空", trigger: "blur" },
    ],
  },
});

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

/** 查询数据列表 */
function getList() {
  loading.value = true;
  expertProject(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 handleShelf(row) {
  let text = row.shelf_status == 2 ? "上架" : "下架";
  proxy.$modal
    .confirm('确认要"' + text + '""' + row.id + '"的数据吗?')
    .then(function () {
      let status = row.shelf_status == 1 ? 2 : 1;
      return projectShelf({ id: row.id, status });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess(text + "成功");
    })
    .catch(() => {});
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除数据编号为"' + row.id + '"的数据项?')
    .then(function () {
      return projectDelete({ id: row.id });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    id: undefined,
    title: undefined,
    role: 1,
    amount: undefined,
    source: undefined,
    begin_at: undefined,
    finish_at: undefined,
    kind: undefined,
    director: undefined,
  };
  proxy.resetForm("roleRef");
}
/** 添加数据 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加数据";
}
/** 修改数据 */
function handleUpdate(row) {
  reset();
  form.value = Object.assign({}, row);
  open.value = true;
  title.value = "修改数据";
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["roleRef"].validate((valid) => {
    if (valid) {
      form.value.amount = form.value.amount - 0;
      if (form.value.id != undefined) {
        projectEdit(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        projectAdd(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

getList();
</script>
