<template>
  <div>
    <div class="titleBox">
      <div class="title">重点区域分值列表</div>
      <div class="btnBox">
        <el-button type="primary" @click="addRole()">
          <el-icon><CirclePlus /></el-icon>
          <span>新增</span>
        </el-button>
      </div>
    </div>
    <div class="searchBox" ref="searchBox">
      <el-form :model="listQuery" :inline="true">
        <el-form-item label="配置类型">
          <el-select
            clearable
            v-model="dialogForm.pzlx"
            placeholder="请选择配置类型"
          >
            <el-option
              v-for="(dict, index) in D_BZ_PZLX"
              :key="index"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleFilter"> 查询 </el-button>
          <el-button @click="reset()"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabBox">
      <el-table
        :data="pzList"
        border
        :height="tableHeight"
        style="width: 100%"
        :key="keyCount"
      >
        <el-table-column
          prop="orderNo"
          label="编号"
          show-overflow-tooltip
          type="index"
          width="120px"
        >
        </el-table-column>
        <el-table-column prop="roleName" label="配置类型" show-overflow-tooltip>
          <template #default="{ row }">
            <dict-tag :options="D_BZ_PZLX" :value="row.pzlx" :tag="false" />
          </template>
        </el-table-column>
        <el-table-column prop="pzfz" label="配置分值" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="pzsjfw" label="时间范围/分钟"> </el-table-column>
        <el-table-column label="操作" fixed="right" width="220">
          <template #default="{ row }">
            <el-button @click="updateDict(row)" size="small">修改</el-button>
            <el-popconfirm
              confirm-button-text="是"
              cancel-button-text="否"
              icon-color="red"
              title="确定要删除?"
              @confirm="delRole(row)"
            >
              <template #reference>
                <el-button type="danger" size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye" :style="{ top: tableHeight + 'px' }">
        <el-pagination
          class="pagination"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="listQuery.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <div v-if="dialogFormVisible" class="dialog">
      <div class="head_box">
        <span class="title">{{ isEdit ? "修改" : "新增" }}</span>
        <div>
          <!-- 修改 -->
          <el-button
            v-if="isEdit"
            type="primary"
            size="small"
            @click="onSave"
            :loading="buttonLoading"
            >保存</el-button
          >
          <!-- 新增 -->
          <el-button
            v-else
            type="primary"
            size="small"
            @click="onAdd"
            :loading="buttonLoading"
            >保存</el-button
          >
          <el-button size="small" @click="closeDialog">关闭</el-button>
        </div>
      </div>
      <el-form
        :rules="rules"
        ref="elform"
        inline="true"
        label-position="top"
        :model="dialogForm"
        class="mosty-from-wrap"
      >
        <el-form-item prop="pzlx" class="one" label="配置类型">
          <el-select clearable v-model="dialogForm.pzlx" placeholder="请选择">
            <el-option
              v-for="(dict, index) in D_BZ_PZLX"
              :key="index"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          class="one"
          prop="pzsjfw"
          label="时间范围(分钟)"
          label-width="140px"
        >
          <el-input
            v-model="dialogForm.pzsjfw"
            show-word-limit
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          class="one"
          label="配置分值"
          prop="pzfz"
          label-width="140px"
        >
          <el-input
            v-model="dialogForm.pzfz"
            show-word-limit
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { ref, onMounted, getCurrentInstance, onUnmounted } from "vue";
import { getSelectPzfz, addPzfz, updatePzfz, deletePzfz } from "@/api/base.js";
const { proxy } = getCurrentInstance();
const { D_BZ_PZLX } = proxy.$dict("D_BZ_PZLX");
import { useRouter } from "vue-router";
const searchBox = ref(null); // 搜索盒子
const keyCount = ref(0); //tabel组件刷新值
const tableHeight = ref(); // 表格高度
//查询参数
const total = ref(0);
const page = ref(1);
const size = ref(20);
const currentRow = ref({});
const listQuery = ref({
  pageCurrent: 1,
  pageSize: 20,
  pzlx: ""
});
const elform = ref(null);
//验证
const rules = ref({
  pzlx: [{ required: true, message: "请选择配置类型", trigger: "change" }],
  pzfz: [{ required: true, message: "请输入配置分值", trigger: "change" }],
  pzsjfw: [{ required: true, message: "请输入时间范围", trigger: "change" }]
});
const isEdit = ref(true);
const dialogForm = ref({
  pzlx: "",
  pzfz: "",
  pzsjfw: "",
  pzlx: "" //筛选条件
});
const pzList = ref([]); //配置分值数据
const buttonLoading = ref(false);
// 数据相关
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
// 获取分值列表数据的方法
function _getSelectPzfz() {
  let data = {
    pageSize: listQuery.value.pageSize,
    pageCurrent: listQuery.value.pageCurrent,
    pzlx: dialogForm.value.pzlx
  };
  getSelectPzfz(data).then((res) => {
    pzList.value = res.records;
  });
}
//条件查询
const handleFilter = () => {
  listQuery.value.pageCurrent = 1;
  _getSelectPzfz();
};
//重置
const reset = () => {
  listQuery.value = {
    pageCurrent: 1,
    pageSize: 20
  };
  dialogForm.value.pzlx = "";
};
// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  listQuery.value.pageSize = currentSize;
  _getSelectPzfz();
};

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  listQuery.value.pageSize = currentPage;
  listQuery.value.pageCurrent = 1;
  _getSelectPzfz();
};

/**修改字典 */
const updateDict = (row) => {
  isEdit.value = true;
  row.pzlx = "" + row.pzlx;
  dialogForm.value = { ...row };
  dialogFormVisible.value = true;
};

/**点击新增 */

const addRole = (row) => {
  isEdit.value = false;
  dialogForm.value = {};
  dialogFormVisible.value = true;
};
/**修改 */
const onSave = () => {
  elform.value.validate((valid) => {
    if (!valid) {
      ElMessage.error("请完成必填项！");
      return false;
    }
    updatePzfz(dialogForm.value).then((res) => {
      ElMessage.success("修改成功");
      buttonLoading.value = false;
      dialogFormVisible.value = false;
      listQuery.value = {
        pageCurrent: 1,
        pageSize: 20
      };
      dialogForm.value.pzlx = "";
      _getSelectPzfz();
    });
  });
};
//添加
const onAdd = () => {
  elform.value.validate((valid) => {
    if (!valid) {
      ElMessage.error("请完成必填项！");
      return false;
    }
    buttonLoading.value = true;
    addPzfz(dialogForm.value)
      .then((res) => {
        ElMessage.success("添加成功");
        buttonLoading.value = false;
        dialogFormVisible.value = false;
        listQuery.value = {
          pageCurrent: 1,
          pageSize: 20
        };
        dialogForm.value.pzlx = "";
        _getSelectPzfz();
      })
      .catch((err) => {
        buttonLoading.value = false;
      });
  });
};
//删除
const delRole = (row) => {
  buttonLoading.value = true;
  deletePzfz(row.id).then((res) => {
    ElMessage.success("删除成功");
    listQuery.value = {
      pageCurrent: 1,
      pageSize: 20
    };
    dialogForm.value.pzlx = "";
    _getSelectPzfz();
  });
};
//关闭弹窗
const closeDialog = () => {
  dialogForm.value = {};
  dialogFormVisible.value = false;
  buttonLoading.value = false;
};

//分配用户
const router = useRouter();

// 高度计算
const tabHeightFn = () => {
 tableHeight.value = window.innerHeight - searchBox.value.offsetHeight - 240;
};
onMounted(() => {
  tabHeightFn();
  window.onresize = function () {
    tabHeightFn();
  };

  _getSelectPzfz();
});
onUnmounted(() => {});
</script>

<style lang="scss" scoped>
@import "@/assets/css/layout.scss";
@import "@/assets/css/element-plus.scss";
.user-manage-container {
  .table-header-wrap {
    margin-bottom: 22px;
  }

  ::v-deep .avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
  }

  ::v-deep .el-tag {
    margin-right: 6px;
  }

  .pagination {
    margin-top: 20px;
  }
}
</style>
