<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="stanReviGuid">
        <el-input v-model="queryParams.stanReviGuid" placeholder="请输入评审GUID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="评分项名称" prop="name">
        <el-input v-model="queryParams.name" 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="['stan:stanscore:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['stan:stanscore:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['stan:stanscore:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['stan:stanscore:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns" storageKey="stan-stanscore-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="name" />
      <el-table-column label="标准分" prop="stanScore" />
      <el-table-column label="实际得分" prop="actScore" />
      <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:stanscore:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['stan:stanscore: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="130px">
        <el-form-item label="评审GUID" prop="stanReviGuid">
          <el-input v-model="form.stanReviGuid" placeholder="请输入评审GUID" />
        </el-form-item>
        <el-form-item label="父GUID" prop="pGuid">
          <el-input v-model="form.pGuid" placeholder="请输入父GUID" />
        </el-form-item>
        <el-form-item label="评分项名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入评分项名称" />
        </el-form-item>
        <el-form-item label="标准分" prop="stanScore">
          <el-input v-model="form.stanScore" placeholder="请输入标准分" />
        </el-form-item>
        <el-form-item label="实际得分" prop="actScore">
          <el-input v-model="form.actScore" placeholder="请输入实际得分" />
        </el-form-item>
        <el-form-item label="保留得分" prop="reseScore">
          <el-input v-model="form.reseScore" placeholder="请输入保留得分" />
        </el-form-item>
        <el-form-item label="得分率" prop="scorRate">
          <el-input v-model="form.scorRate" placeholder="请输入得分率" />
        </el-form-item>
        <el-form-item label="标准GUID" prop="stanStanGuid">
          <el-input v-model="form.stanStanGuid" placeholder="请输入标准GUID" />
        </el-form-item>
        <el-form-item label="标准化自评" prop="bisStanSelfEva">
          <el-input v-model="form.bisStanSelfEva" placeholder="请输入标准化自评" />
        </el-form-item>
        <el-form-item label="自评得分GUID" prop="stanSelfEvaScoreGuid">
          <el-input v-model="form.stanSelfEvaScoreGuid" 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="StanScore">
import { listBisStanScore, getBisStanScore, addBisStanScore, updateBisStanScore, delBisStanScore } from "@/api/slaj/stan";
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({ name: { visible: true }, stanScore: { visible: true }, actScore: { visible: true } });

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

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

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

function handleExport() {
  proxy.download('/stan/stanscore/export', { ...queryParams.value }, `标准化评分_${new Date().getTime()}.xlsx`);
}

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

function reset() {
  form.value = { guid: null, stanReviGuid: null, pGuid: null, name: null, stanScore: null, actScore: null, reseScore: null, scorRate: null, stanStanGuid: null, bisStanSelfEva: null, stanSelfEvaScoreGuid: null, note: null };
  proxy.resetForm("formRef");
}

getList();
</script>
