<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工程GUID" prop="engGuid">
        <el-input v-model="queryParams.engGuid" placeholder="请输入工程GUID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="工程名称" prop="engName">
        <el-input v-model="queryParams.engName" 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:attengres:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['sins:attengres:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['sins:attengres:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" />
    </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="engName" v-if="columns.engName.visible" />
      <el-table-column label="工程等级" align="center" prop="engGrad" v-if="columns.engGrad.visible" />
      <el-table-column label="总库容" align="center" prop="totCap" v-if="columns.totCap.visible" />
      <el-table-column label="坝型(材料)" align="center" prop="damTypeMat" v-if="columns.damTypeMat.visible" />
      <el-table-column label="坝高" align="center" prop="damSizeHig" v-if="columns.damSizeHig.visible" />
      <el-table-column label="死水位" align="center" prop="deadLev" v-if="columns.deadLev.visible" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['sins:attengres:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['sins:attengres:remove']">删除</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="750px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工程GUID" prop="engGuid">
              <el-input v-model="form.engGuid" placeholder="请输入工程GUID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程名称" prop="engName">
              <el-input v-model="form.engName" placeholder="请输入工程名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程等级" prop="engGrad">
              <el-input v-model="form.engGrad" placeholder="请输入工程等级" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工程规模" prop="engScal">
              <el-input v-model="form.engScal" placeholder="请输入工程规模" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总库容" prop="totCap">
              <el-input v-model="form.totCap" placeholder="请输入总库容" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="坝型(材料)" prop="damTypeMat">
              <el-input v-model="form.damTypeMat" placeholder="请输入坝型(材料)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="坝高" prop="damSizeHig">
              <el-input v-model="form.damSizeHig" placeholder="请输入坝高" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设计洪水标准" prop="desFlStand">
              <el-input v-model="form.desFlStand" placeholder="请输入设计洪水标准" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="汛限水位" prop="flLowLimLev">
              <el-input v-model="form.flLowLimLev" placeholder="请输入汛限水位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设计洪水位" prop="desFlStag">
              <el-input v-model="form.desFlStag" placeholder="请输入设计洪水位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="死水位" prop="deadLev">
              <el-input v-model="form.deadLev" placeholder="请输入死水位" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="note">
              <el-input v-model="form.note" type="textarea" 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 { listAttEngRes, getAttEngRes, addAttEngRes, updateAttEngRes, delAttEngRes } 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({
  engName: { visible: true },
  engGrad: { visible: true },
  totCap: { visible: true },
  damTypeMat: { visible: true },
  damSizeHig: { visible: true },
  deadLev: { visible: true },
});

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

function getList() {
  loading.value = true;
  listAttEngRes(queryParams.value).then(r => {
    dataList.value = r.rows;
    total.value = r.total;
    loading.value = false;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleSelectionChange(s) {
  ids.value = s.map(i => i.guid);
  single.value = s.length !== 1;
  multiple.value = !s.length;
}

function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加水库工程属性";
}

function handleUpdate(row) {
  reset();
  getAttEngRes(row.guid || ids.value[0]).then(r => {
    form.value = r.data;
    open.value = true;
    title.value = "修改水库工程属性";
  });
}

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

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

function handleExport() {
  proxy.download('/sins/attengres/export', { ...queryParams.value }, `水库工程属性_${new Date().getTime()}.xlsx`);
}

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

function reset() {
  form.value = { guid: null, engGuid: null, engName: null, engGrad: null, engScal: null, damTypeStr: null, totCap: null, damTypeMat: null, damSizeHig: null, desFlStand: null, flLowLimLev: null, desFlStag: null, deadLev: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
