<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="方案明细GUID" prop="scheGuid">
        <el-input v-model="queryParams.scheGuid" placeholder="请输入方案明细GUID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="组名称" prop="groupName">
        <el-input v-model="queryParams.groupName" placeholder="请输入检查组名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @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" @click="handleAdd" v-hasPermi="['sins:sinsschegrop:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['sins:sinsschegrop:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['sins:sinsschegrop:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['sins:sinsschegrop:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="sins-sinsschegrop-index"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="检查组名称" align="center" prop="groupName" />
      <el-table-column label="组长" align="center" prop="groupLeader" />
      <el-table-column label="组长联系方式" align="center" prop="groupLeaderWiun" />
      <el-table-column label="操作" align="center" width="200">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['sins:sinsschegrop:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['sins:sinsschegrop:remove']"></el-button>
          </el-tooltip>
        </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="600px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="方案明细GUID" prop="scheGuid">
          <el-input v-model="form.scheGuid" placeholder="请输入方案明细GUID" />
        </el-form-item>
        <el-form-item label="检查组名称" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入检查组名称" />
        </el-form-item>
        <el-form-item label="组长" prop="groupLeader">
          <el-input v-model="form.groupLeader" placeholder="请输入组长" />
        </el-form-item>
        <el-form-item label="组长联系方式" prop="groupLeaderWiun">
          <el-input v-model="form.groupLeaderWiun" placeholder="请输入组长联系方式" />
        </el-form-item>
        <el-form-item label="方案模型" prop="scheModel">
          <el-input v-model="form.scheModel" placeholder="请输入方案模型" />
        </el-form-item>
        <el-form-item label="方案路径" prop="schePath">
          <el-input v-model="form.schePath" placeholder="请输入方案路径" />
        </el-form-item>
        <el-form-item label="方案路径详情" prop="schePathDetl">
          <el-input v-model="form.schePathDetl" placeholder="请输入方案路径详情" />
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入备注" />
        </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 name="SinsScheGrop">
import { listBisSinsScheGrop, getBisSinsScheGrop, addBisSinsScheGrop, updateBisSinsScheGrop, delBisSinsScheGrop } from "@/api/slaj/sins";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();

const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const columns = ref({
  groupName: { label: "检查组名称", visible: true },
  groupLeader: { label: "组长", visible: true },
  groupLeaderWiun: { label: "组长联系方式", visible: true }
});

const data = reactive({
  form: {},
  queryParams: { pageNum: 1, pageSize: 10, scheGuid: null, groupName: null },
  rules: {}
});
const { form, queryParams, rules } = toRefs(data);

function getList() {
  loading.value = true;
  listBisSinsScheGrop(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 handleSelectionChange(selection) {
  ids.value = selection.map(item => item.guid);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加检查组";
}

function handleUpdate(row) {
  reset();
  getBisSinsScheGrop(row.guid || ids.value[0]).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改检查组";
  });
}

function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.value.guid != null) {
        updateBisSinsScheGrop(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBisSinsScheGrop(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除？').then(function() {
    return delBisSinsScheGrop(row.guid || ids.value.join(","));
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  });
}

function handleExport() {
  proxy.download('/sins/sinsschegrop/export', { ...queryParams.value }, `检查组_${new Date().getTime()}.xlsx`);
}

function cancel() {
  open.value = false;
  reset();
}

function reset() {
  form.value = { scheGuid: null, groupName: null, groupLeader: null, groupLeaderWiun: null, scheModel: null, schePath: null, schePathDetl: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
