<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="统一社会信用代码" prop="unifiedSocialCreditCode">
        <el-input
            v-model="queryParams.unifiedSocialCreditCode"
            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-table v-loading="loading" :data="enterprisesList" height="650" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="企业名称" align="center" prop="enterpriseName" width="260" />
      <el-table-column label="统一社会信用代码" align="center" prop="unifiedSocialCreditCode" width="200" />
      <el-table-column label="财务总得分(满分10分)" align="center" prop="financialScore" width="170" />
      <el-table-column label="参考信用评价得分(满分10分)" align="center" prop="referenceCreditRatingScore" width="220" />
      <el-table-column label="行政处罚得分(满分20分)" align="center" prop="administrativePenaltyScore" width="220" />
      <el-table-column label="行政监管、践诺履约得分(满分20分)" align="center" prop="administrativeSupervisionScore" width="240" />
      <el-table-column label="企业竞争力得分(满分30分)" align="center" prop="enterpriseCompetitivenessScore" width="220" />
      <el-table-column label="司法裁判得分(满分10分)" align="center" prop="judicialScore" width="220" />
      <el-table-column label="总分" align="center" prop="total" />
      <el-table-column label="评级" align="center" prop="grade" />
    </el-table>
    <div style="position: fixed;bottom: 20px;right: 10px;">
      <el-pagination
          :page-sizes="[10, 20, 30, 50]"
          v-model:page-size = "queryParams.pageSize"
          v-model:current-page = "queryParams.pageNum"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="getList"
          :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script setup name="Enterprises">
import { listEnterprises, getEnterprises, delEnterprises, addEnterprises, updateEnterprises } from "@/api/basicData/enterprises2";
import {likeQueryMethod} from "../../../utils/likeQueryMethod";

const { proxy } = getCurrentInstance();

const enterprisesList = 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 data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 20,
    enterpriseName: null,
    unifiedSocialCreditCode: '',
    enterpriseLeader: null,
    contactPhone: null,
  },
  rules: {
    enterpriseName: [
      { required: true, message: "企业名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询高新区企业（119家）列表 */
function getList() {
  loading.value = true;
  let toServerObj = {
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize,
    unifiedSocialCreditCode:queryParams.value.unifiedSocialCreditCode
  }
  listEnterprises(toServerObj).then(response => {
    enterprisesList.value = response.data.result;
    total.value = response.data.totalSize;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    enterpriseName: null,
    unifiedSocialCreditCode: '',
    enterpriseLeader: null,
    contactPhone: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("enterprisesRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加高新区企业（119家）";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value
  getEnterprises(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改高新区企业（119家）";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["enterprisesRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateEnterprises(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addEnterprises(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除高新区企业（119家）编号为"' + ids + '"的数据项？').then(function() {
    return delEnterprises(ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('basicData/enterprises/export', {
    ...queryParams.value
  }, `enterprises_${new Date().getTime()}.xlsx`)
}

getList();
</script>
