<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
      @submit.prevent
    >
      <el-form-item label="企业名称" prop="researchName">
        <el-input
          v-model="queryParams.researchName"
          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="researchId" />
      <el-table-column
        label="合作企业名称"
        prop="researchName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="关联论文（2）"
        prop="roleKey"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="关联论文（1）" prop="roleSort" />
      <el-table-column label="创建日期" align="center" prop="createTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</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="500px" append-to-body>
      <el-form ref="roleRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="企业名称:" prop="researchName">
          <el-input v-model="form.researchName" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="关联论文:" prop="projectType">
          <el-select
            style="width: 100%"
            v-model="form.projectType"
            multiple
            placeholder="请选择关联论文"
          >
            <!-- <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option> -->
          </el-select>
        </el-form-item>
        <el-form-item label="关联专利:" prop="projectLeader">
          <el-select
            style="width: 100%"
            v-model="form.projectLeader"
            multiple
            placeholder="请选择关联专利"
          >
            <!-- <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option> -->
          </el-select>
        </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 {
  addRole,
  changeRoleStatus,
  dataScope,
  delRole,
  getRole,
  listRole,
  updateRole,
} from "@/api/system/role";
import {
  roleMenuTreeselect,
  treeselect as menuTreeselect,
} from "@/api/system/menu";
import {
  treeselect as deptTreeselect,
  roleDeptTreeselect,
} from "@/api/system/dept";

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: {
    pageNum: 1,
    pageSize: 10,
    researchName: undefined,
  },
  rules: {
    researchName: [
      { required: true, message: "企业名称不能为空", trigger: "blur" },
    ],
    projectType: [
      {
        type: "array",
        required: true,
        message: "关联论文不能为空",
        trigger: ["blur", "change"],
      },
    ],
    projectLeader: [
      {
        type: "array",
        required: true,
        message: "关联专利不能为空",
        trigger: ["blur", "change"],
      },
    ],
  },
});

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

/** 查询数据列表 */
function getList() {
  loading.value = true;
  dataList.value = [1];
  total.value = 15;
  loading.value = false;
  // listRole(queryParams.value).then(
  //   (response) => {
  //     dataList.value = response.rows;
  //     total.value = response.total;
  //     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 delRole(researchIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    researchId: undefined,
    researchName: undefined,
    projectType: [],
    projectLeader: [],
  };
  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) {
      console.log(form.value);
      // if (form.value.researchId != undefined) {
      //   updateRole(form.value).then((response) => {
      //     proxy.$modal.msgSuccess("修改成功");
      //     open.value = false;
      //     getList();
      //   });
      // } else {
      //   addRole(form.value).then((response) => {
      //     proxy.$modal.msgSuccess("新增成功");
      //     open.value = false;
      //     getList();
      //   });
      // }
    }
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

getList();
</script>
