<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">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--            icon="Plus"-->
      <!--            plain-->
      <!--            size="small"-->
      <!--            type="primary"-->
      <!--            @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 align="center" label="数据编号" prop="id"/>-->
      <el-table-column align="center" label="实验室名称" prop="name" />
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="研究领域"
        prop="industryStr"
      />
      <el-table-column align="center" label="联系电话" prop="phone" />
      <el-table-column align="center" label="实验室网址" prop="url" />
      <el-table-column align="center" label="添加日期" prop="createTime">
        <template #default="{ row }">
          <span>{{ dayjs(row.createTime).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="Edit"
            size="small"
            type="text"
            @click="handleUpdate(row)"
            >修改
          </el-button>
          <el-button size="small" type="text" @click="handleCancelBind(row)"
            >解绑
            <template #icon>
              <svg-icon icon-class="broken_link" />
            </template>
          </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="600px">
      <el-form
        ref="equipmentRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="设备名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="型号:" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="购买时间:" prop="buyDate">
          <el-date-picker
            v-model="form.buyDate"
            placeholder="请选择购买时间"
            type="date"
            value-format="YYYY-MM-DD HH:mm:ss"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="成果参数:" prop="param">
          <el-input v-model="form.param" placeholder="请输入成果参数" />
        </el-form-item>
        <el-form-item label="设备描述:" prop="description">
          <el-input
            v-model="form.description"
            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 { reactive, ref, toRefs } from "vue";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  deleteEquipment,
  insertEquipment,
} from "@/api/admin/laboratory/equipment";
import {
  delBindLaboratory,
  laboratoryEdit,
  laboratoryList,
} from "@/api/admin/research/laboratory";
import dayjs from "dayjs";
import SvgIcon from "@/components/SvgIcon/index.vue";

const router = useRouter();

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const equipmentRef = ref();
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
  },
  rules: {
    name: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
    model: [{ required: true, message: "型号不能为空", trigger: "blur" }],
    param: [{ required: true, message: "成果参数不能为空", trigger: "blur" }],
    buyDate: [
      {
        required: true,
        message: "请选择购买时间",
        trigger: ["blur", "change"],
      },
    ],
  },
});

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

/** 查询数据列表 */
function getList() {
  loading.value = true;
  laboratoryList(queryParams.value).then((response) => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  equipmentRef.value?.resetFields();
  handleQuery();
}

/** 删除按钮操作 */
function handleDelete(row) {
  ElMessageBox.confirm('是否确认删除数据编号为"' + row.deviceId + '"的数据项?')
    .then(function () {
      return deleteEquipment([row.deviceId]);
    })
    .then(() => {
      getList();
      ElMessage.success("删除成功");
    })
    .catch(() => {});
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    name: null,
    model: null,
    buyDate: null,
    param: null,
    description: null,
  };
  equipmentRef.value?.resetFields();
}

/** 添加数据 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加数据";
}

/** 修改数据 */
async function handleUpdate(row) {
  router.push({
    path: "/admin/research/laboratory/edit",
    query: {
      id: row.id,
    },
  });
}

/** 提交按钮 */
function submitForm() {
  equipmentRef.value?.validate((valid) => {
    if (valid) {
      if (form.value.id) {
        laboratoryEdit(form.value).then((response) => {
          ElMessage.success("修改成功");
          open.value = false;
          getList();
        });
      } else {
        insertEquipment(form.value).then((response) => {
          ElMessage.success("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

const handleCancelBind = (row) => {
  ElMessageBox.confirm(`是否确认解绑实验室 : ${row.name}`, "解绑实验室")
    .then(() => {
      return delBindLaboratory(row.id);
    })
    .then(() => {
      getList();
      ElMessage.success("解绑实验室成功");
    });
};

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

getList();
</script>
