<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="案件名称" prop="caseName">
        <el-input v-model="queryParams.caseName" placeholder="请输入案件名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="案件编号" prop="caseNum">
        <el-input v-model="queryParams.caseNum" 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:objcase:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['sins:objcase:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['sins:objcase:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['sins:objcase:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="sins-objcase-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="caseNum" />
      <el-table-column label="案件名称" align="center" prop="caseName" />
      <el-table-column label="当事人" align="center" prop="caseLitiName" />
      <el-table-column label="立案时间" align="center" prop="filiTime" width="180" />
      <el-table-column label="结案时间" align="center" prop="closTime" width="180" />
      <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:objcase:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['sins:objcase: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="750px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="案件编号" prop="caseNum">
          <el-input v-model="form.caseNum" placeholder="请输入案件编号" />
        </el-form-item>
        <el-form-item label="案件名称" prop="caseName">
          <el-input v-model="form.caseName" placeholder="请输入案件名称" />
        </el-form-item>
        <el-form-item label="案件情况" prop="caseSitu">
          <el-input v-model="form.caseSitu" type="textarea" placeholder="请输入案件情况" />
        </el-form-item>
        <el-form-item label="案由" prop="caseCous">
          <el-input v-model="form.caseCous" type="textarea" placeholder="请输入案由" />
        </el-form-item>
        <el-form-item label="立案时间" prop="filiTime">
          <el-date-picker v-model="form.filiTime" type="datetime" placeholder="选择立案时间" />
        </el-form-item>
        <el-form-item label="结案时间" prop="closTime">
          <el-date-picker v-model="form.closTime" type="datetime" placeholder="选择结案时间" />
        </el-form-item>
        <el-form-item label="案件进展" prop="caseProg">
          <el-input v-model="form.caseProg" placeholder="请输入案件进展" />
        </el-form-item>
        <el-form-item label="所属单位" prop="orgGuid">
          <el-input v-model="form.orgGuid" placeholder="请输入所属单位" />
        </el-form-item>
        <el-form-item label="当事人" prop="caseLitiName">
          <el-input v-model="form.caseLitiName" placeholder="请输入当事人" />
        </el-form-item>
        <el-form-item label="处罚描述" prop="punDesc">
          <el-input v-model="form.punDesc" type="textarea" placeholder="请输入处罚描述" />
        </el-form-item>
        <el-form-item label="处罚形式" prop="punForm">
          <el-input v-model="form.punForm" 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="SinsCase">
import { listObjCase, getObjCase, addObjCase, updateObjCase, delObjCase } 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({
  caseNum: { label: "案件编号", visible: true },
  caseName: { label: "案件名称", visible: true },
  caseLitiName: { label: "当事人", visible: true },
  filiTime: { label: "立案时间", visible: true },
  closTime: { label: "结案时间", visible: true }
});

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

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

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

function handleExport() {
  proxy.download('/sins/objcase/export', { ...queryParams.value }, `执法案件_${new Date().getTime()}.xlsx`);
}

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

function reset() {
  form.value = { caseNum: null, caseName: null, caseSitu: null, caseCous: null, filiTime: null, closTime: null, caseProg: null, orgGuid: null, caseLitiName: null, punDesc: null, punForm: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
