<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="处罚类型" prop="punoType">
        <el-select v-model="queryParams.punoType" placeholder="请选择处罚类型" clearable>
          <el-option v-for="dict in eutr_puno_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="stat">
        <el-select v-model="queryParams.stat" placeholder="请选择状态" clearable>
          <el-option v-for="dict in eutr_puno_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </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="['stan:objpuno:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['stan:objpuno:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['stan:objpuno:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['stan:objpuno:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="stan-objpuno-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="处罚类型" prop="punoType" width="120">
        <template #default="scope">
          <dict-tag :options="eutr_puno_type" :value="scope.row.punoType" />
        </template>
      </el-table-column>
      <el-table-column label="内容" prop="cont" />
      <el-table-column label="处罚来源" prop="punoSour" />
      <el-table-column label="发布时间" prop="releTime" width="180" />
      <el-table-column label="结束时间" prop="endTime" width="180" />
      <el-table-column label="发布人" prop="relePers" />
      <el-table-column label="状态" prop="stat" width="120">
        <template #default="scope">
          <dict-tag :options="eutr_puno_stat" :value="scope.row.stat" />
        </template>
      </el-table-column>
      <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="['stan:objpuno:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['stan:objpuno: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="110px">
        <el-form-item label="标题" prop="titl">
          <el-input v-model="form.titl" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="处罚类型" prop="punoType">
          <el-select v-model="form.punoType" placeholder="请选择处罚类型">
            <el-option v-for="dict in eutr_puno_type" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="cont">
          <el-input v-model="form.cont" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="处罚来源" prop="punoSour">
          <el-input v-model="form.punoSour" placeholder="请输入处罚来源" />
        </el-form-item>
        <el-form-item label="发布时间" prop="releTime">
          <el-date-picker v-model="form.releTime" type="datetime" placeholder="选择发布时间" clearable />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker v-model="form.endTime" type="datetime" placeholder="选择结束时间" clearable />
        </el-form-item>
        <el-form-item label="有效期" prop="valiTime">
          <el-input v-model="form.valiTime" placeholder="请输入有效期" />
        </el-form-item>
        <el-form-item label="发布组织GUID" prop="releOrgGuid">
          <el-input v-model="form.releOrgGuid" placeholder="请输入发布组织GUID" />
        </el-form-item>
        <el-form-item label="发布人" prop="relePers">
          <el-input v-model="form.relePers" placeholder="请输入发布人" />
        </el-form-item>
        <el-form-item label="状态" prop="stat">
          <el-select v-model="form.stat" placeholder="请选择状态">
            <el-option v-for="dict in eutr_puno_stat" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="评审GUID" prop="stanReviGuid">
          <el-input v-model="form.stanReviGuid" placeholder="请输入评审GUID" />
        </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="ObjPuno">
import { listObjPuno, getObjPuno, addObjPuno, updateObjPuno, delObjPuno } from "@/api/slaj/stan";
import { getCurrentInstance, reactive, ref, toRefs } from "vue";

const { proxy } = getCurrentInstance();
const { eutr_puno_type, eutr_puno_stat } = proxy.useDict("eutr_puno_type", "eutr_puno_stat");

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({ punoType: { visible: true }, cont: { visible: true }, punoSour: { visible: true }, releTime: { visible: true }, endTime: { visible: true }, relePers: { visible: true }, stat: { visible: true } });

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

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

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

function handleExport() {
  proxy.download('/stan/objpuno/export', { ...queryParams.value }, `处罚对象_${new Date().getTime()}.xlsx`);
}

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

function reset() {
  form.value = { guid: null, titl: null, punoType: null, cont: null, punoSour: null, releTime: null, endTime: null, valiTime: null, releOrgGuid: null, relePers: null, stat: null, stanReviGuid: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
