<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="suenGuid">
        <el-input v-model="queryParams.suenGuid" placeholder="请输入被检查单位GUID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="行为编码" prop="behavCode">
        <el-input v-model="queryParams.behavCode" 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:wiunberebad:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['sins:wiunberebad:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['sins:wiunberebad:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['sins:wiunberebad:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="sins-wiunberebad-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="behavCode" />
      <el-table-column label="行为类型" align="center" prop="boodBehType" />
      <el-table-column label="状态" align="center" prop="stat" />
      <el-table-column label="处理决定" align="center" prop="dispDeci" />
      <el-table-column label="信用等级" align="center" prop="credGrad" />
      <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:wiunberebad:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['sins:wiunberebad: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="700px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="被检查单位GUID" prop="suenGuid">
          <el-input v-model="form.suenGuid" placeholder="请输入被检查单位GUID" />
        </el-form-item>
        <el-form-item label="水库ID" prop="lakeId">
          <el-input v-model="form.lakeId" placeholder="请输入水库ID" />
        </el-form-item>
        <el-form-item label="行为编码" prop="behavCode">
          <el-input v-model="form.behavCode" placeholder="请输入行为编码" />
        </el-form-item>
        <el-form-item label="行为类型" prop="boodBehType">
          <el-input v-model="form.boodBehType" placeholder="请输入行为类型" />
        </el-form-item>
        <el-form-item label="不良行为描述" prop="badBehDesc">
          <el-input v-model="form.badBehDesc" type="textarea" placeholder="请输入不良行为描述" />
        </el-form-item>
        <el-form-item label="处置时间" prop="dispTime">
          <el-date-picker v-model="form.dispTime" type="datetime" placeholder="选择处置时间" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker v-model="form.startTime" type="datetime" placeholder="选择开始时间" />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker v-model="form.endTime" type="datetime" placeholder="选择结束时间" />
        </el-form-item>
        <el-form-item label="处置决定" prop="dispDeci">
          <el-input v-model="form.dispDeci" placeholder="请输入处置决定" />
        </el-form-item>
        <el-form-item label="处置说明" prop="dispDisc">
          <el-input v-model="form.dispDisc" type="textarea" placeholder="请输入处置说明" />
        </el-form-item>
        <el-form-item label="信用分" prop="dateScou">
          <el-input v-model="form.dateScou" placeholder="请输入信用分" />
        </el-form-item>
        <el-form-item label="信用等级" prop="credGrad">
          <el-input v-model="form.credGrad" placeholder="请输入信用等级" />
        </el-form-item>
        <el-form-item label="状态" prop="stat">
          <el-input v-model="form.stat" 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="SinsBad">
import { listBisWiunBereBad, getBisWiunBereBad, addBisWiunBereBad, updateBisWiunBereBad, delBisWiunBereBad } 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({
  behavCode: { label: "行为编码", visible: true },
  boodBehType: { label: "行为类型", visible: true },
  stat: { label: "状态", visible: true },
  dispDeci: { label: "处理决定", visible: true },
  credGrad: { label: "信用等级", visible: true }
});

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

function getList() {
  loading.value = true;
  listBisWiunBereBad(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();
  getBisWiunBereBad(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) {
        updateBisWiunBereBad(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBisWiunBereBad(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

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

function handleExport() {
  proxy.download('/sins/wiunberebad/export', { ...queryParams.value }, `不良行为记录_${new Date().getTime()}.xlsx`);
}

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

function reset() {
  form.value = { suenGuid: null, lakeId: null, behavCode: null, boodBehType: null, badBehDesc: null, dispTime: null, startTime: null, endTime: null, dispDeci: null, dispDisc: null, dateScou: null, credGrad: null, stat: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
