<template>
  <div class="content-box">
    <div class="left">
      <!-- <el-icon :size="23" class="camera"><VideoCameraFilled /></el-icon> -->
      <span class="cameratxt">系统设置</span>
    </div>
    <div class="middle">
      <div class="roles">考核规则</div>
      <div class="filter">
        <!-- <el-checkbox v-model="checked1" label="提醒次数扣分" size="large" /> -->
        <span style="font-size: 14px; margin-right: 10px">提醒次数扣分</span>
        <el-input :disabled="!btnFlag('system-xiugaikeheguize')" @blur="updateRemind" v-model="remind" placeholder="请输入次数" clearable />
        <span class="grade">分/次</span>
      </div>
      <div class="filter">
        <!-- <el-checkbox v-model="checked1" label="预警次数扣分" size="large" /> -->
        <span style="font-size: 14px; margin-right: 10px">预警次数扣分</span>
        <el-input :disabled="!btnFlag('system-xiugaikeheguize')" @blur="updateWarn" v-model="warning" placeholder="请输入次数" clearable />
        <span class="grade">分/次</span>
      </div>
      <div class="roles">考核维度管理</div>
      <!-- 按钮列表 -->
      <!-- <div class="gongneng">
        <el-button @click="addProjectbtn" class="add"
          ><el-icon> <CirclePlus /> </el-icon
          ><span>添加考核维度</span></el-button
        >
      </div> -->
    </div>
    <div class="scroll">
      <div class="footer" v-loading="loading">
        <el-table
          :row-class-name="tableRowClassName"
          :data="tableData.list"
          @selection-change="handleSelectionChange"
          style="width: 100%"
          :header-cell-style="{
            background: 'rgba(240, 243, 247, 1)',
            height: '35px',
            border: 0,
          }"
        >
          <!-- <el-table-column type="selection" width="55" /> -->
          <el-table-column align="center" prop="configKey" label="维度名称" />
          <el-table-column align="center" prop="configValue" label="维度权重" />
          <!-- <el-table-column align="center" prop="createBy" label="创建人" /> -->
          <el-table-column align="center" label="操作" width="220">
            <template #default="scope">
              <div class="options">
                <div class="look" v-if="btnFlag('system-xiugaiweidukaohe')" @click="edit(scope.row)">编辑</div>
                <!-- <el-popconfirm
                  width="150"
                  confirm-button-text="确认"
                  cancel-button-text="取消"
                  :icon="InfoFilled"
                  icon-color="#f89626"
                  title="是否删除此数据?"
                  @confirm="del(scope.row)"
                  @cancel="cancelEvent"
                >
                  <template #reference>
                    <div class="del">删除</div>
                  </template>
                </el-popconfirm> -->
              </div>
            </template>
          </el-table-column>
        </el-table>

        <!-- <div class="gongneng">
          <el-button @click="addProjectbtn" class="add"
            ><span>修改</span></el-button
          >
          <el-button @click="projectImportBtn" class="imp"
            ><span>取消</span></el-button
          >
          <el-button @click="addProjectbtn" class="add"
            ><span>保存</span></el-button
          >
        </div> -->
        <!-- 添加考核维度 -->
        <el-dialog
          class="account"
          v-model="accountVisible"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          :title="accountTitle"
          align-center
          width="600"
          :before-close="cancelAccount"
        >
          <el-form
            ref="accountRef"
            :model="accountRuleForm"
            :rules="accountRules"
            class="demo-ruleForm"
            label-width="120px"
            :size="formSize"
            label-position="right"
            status-icon
          >
            <el-form-item label="维度名称" prop="configKey">
              <el-input
                disabled
                v-model="accountRuleForm.configKey"
                placeholder="请输入维度名称"
                clearable
                style="width: 220px"
              />
            </el-form-item>
            <!-- <span class="resetPassword"
              >注：建设单位时用户名可填写单位名称</span
            > -->
            <el-form-item label="权重" prop="configValue">
              <el-input
                v-model="accountRuleForm.configValue"
                placeholder="请输入权重"
                clearable
                style="width: 220px"
              />
            </el-form-item>
            <el-form-item label="是否属于进度" prop="isSpeed">
              <el-radio-group disabled v-model="accountRuleForm.isSpeed">
                <el-radio :value="0" size="large">质量</el-radio>
                <el-radio :value="1" size="large">进度</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item class="options">
              <el-button
                class="queding"
                type="primary"
                @click="confirmAccount(accountRef)"
              >
                &nbsp;&nbsp;确认&nbsp;&nbsp;
              </el-button>
              <el-button class="quxiao" @click="cancelAccount"
                >&nbsp;&nbsp;取消&nbsp;&nbsp;</el-button
              >
            </el-form-item>
          </el-form>
        </el-dialog>
      </div>

      <!-- 分页组件 -->
      <!-- <el-pagination
        background
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total,sizes, prev, pager, next, jumper, slot"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      /> -->
    </div>
  </div>
</template>

<script setup>
import {
  ref,
  reactive,
  nextTick,
  onMounted,
  watch,
  onBeforeMount,
  onUnmounted,
  inject
} from "vue";
import COS from "cos-js-sdk-v5";
import { useRouter } from "vue-router";
import { genFileId, ElMessage, ElMessageBox } from "element-plus";
import { Calendar } from "@element-plus/icons-vue";
import { dayjs } from "element-plus";
import lodash, { reduce } from "lodash";
import axios from "axios";
import { useStore } from "vuex";

const store = useStore();
const router = useRouter();
const btnFlag = inject('$btnFlag')

const remind = ref();
const remindList = ref();
const warning = ref();
const warningList = ref();

const tableData = reactive({
  list: [{}],
});
const currentPage = ref(1); // 当前页
const pageSize = ref(10);
const total = ref(40); // 当前总数

const api = ref("");

// 新增账号 (--------------------------------------------------------)
const accountVisible = ref(false);
const accountTitle = ref();
const accountRef = ref();

const accountRuleForm = reactive({
  configKey: "", // 名称
  configValue: "", // 权重
  isSpeed: "1", // 是否进度
  id: "",
});
// 新增账号表单验证
const accountRules = reactive({
  configKey: [{ required: true, message: "维度名称不能为空", trigger: "blur" }],
  configValue: [{ required: true, message: "权重不能为空", trigger: "blur" }],
  isSpeed: [{ required: true, message: "进度不能为空", trigger: "blur" }],
});

// 获取账户列表
const getList = async () => {
  let params = {
    page: currentPage.value, // 当前页
    rows: pageSize.value, // 一页数据条数
  };
  let data = new FormData();
  data.append("name", "");
  let res = await axios({
    method: "post",
    url: api.value + "/api/sysConfig/queryPage",
    headers: {
      tokenP: sessionStorage.getItem("tokenP"),
      user_head: sessionStorage.getItem("userhead"),
    },
    params: params,
    data,
  });
  console.log(res, "考核维度管理");
  if (res.data.code == 200) {
    remind.value = res.data.data.rows[0].configValue;
    remindList.value = res.data.data.rows[0];
    warning.value = res.data.data.rows[1].configValue;
    warningList.value = res.data.data.rows[1];
    tableData.list = [res.data.data.rows[2], res.data.data.rows[3]];
  } else {
    window.location.href = 'https://chtech.ncjti.edu.cn/kpi/template/api/sysUser/authorize';
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};

// 修改扣分项
const updateRemind = async () => {
  if(!remind.value){
    getList()
    ElMessage({
      type: "warning",
      showClose: true,
      message: '提醒次数扣分不能为空',
      center: true,
    });
    return 
  }
  let data = {
    configKey: remindList.value.configKey,
    isSpeed: remindList.value.isSpeed, // 是否属于进度
    configValue: remind.value,
    remark: "",
    id: remindList.value.id,
  };
  // 判断是否有id存在，有则是修改账号
  let res = await axios({
    method: "post",
    url: api.value + "/api/sysConfig/update",
    headers: {
      tokenP: sessionStorage.getItem("tokenP"),
      user_head: sessionStorage.getItem("userhead"),
    },
    data: data,
  });
  console.log(res, "修改扣分项");
  if (res.data.code == 200) {
    getList();
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};
const updateWarn = async () => {
  if(!warning.value){
    getList()
    ElMessage({
      type: "warning",
      showClose: true,
      message: '预警次数扣分不能为空',
      center: true,
    });
    return 
  }
  let data = {
    configKey: warningList.value.configKey,
    isSpeed: warningList.value.isSpeed, // 是否属于进度
    configValue: warning.value,
    remark: "",
    id: warningList.value.id,
  };
  // 判断是否有id存在，有则是修改账号
  let res = await axios({
    method: "post",
    url: api.value + "/api/sysConfig/update",
    headers: {
      tokenP: sessionStorage.getItem("tokenP"),
      user_head: sessionStorage.getItem("userhead"),
    },
    data: data,
  });
  console.log(res, "修改扣分项");
  if (res.data.code == 200) {
    getList();
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};

// 添加考核维度
const addProjectbtn = () => {
  accountVisible.value = true;
  accountTitle.value = "添加考核维度";
  accountRuleForm.configKey = "";
  accountRuleForm.configValue = "";
  accountRuleForm.isSpeed = "";
  accountRuleForm.id = "";
};
// 取消添加考核维度
const cancelAccount = () => {
  accountVisible.value = false;
  accountRuleForm.configKey = "";
  accountRuleForm.configValue = "";
  accountRuleForm.isSpeed = "";
  accountRuleForm.id = "";
  accountRef.value.resetFields();
};

//编辑考核维度
const edit = (row) => {
  accountVisible.value = true;
  accountTitle.value = "编辑考核维度";
  accountRuleForm.configKey = row.configKey;
  accountRuleForm.configValue = row.configValue;
  accountRuleForm.isSpeed = row.isSpeed;
  accountRuleForm.id = row.id;
  console.log(row);
};
// 确定考核维度
const confirmAccount = (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid, fields) => {
    if (valid) {
      let data = {
        configKey: accountRuleForm.configKey,
        isSpeed: accountRuleForm.isSpeed, // 是否属于进度
        configValue: accountRuleForm.configValue,
        remark: "",
      };
      let res = "";

      // 判断是否有id存在，有则是修改账号
      if (accountRuleForm.id) {
        data.id = accountRuleForm.id;
        res = await axios({
          method: "post",
          url: api.value + "/api/sysConfig/update",
          headers: {
            tokenP: sessionStorage.getItem("tokenP"),
            user_head: sessionStorage.getItem("userhead"),
          },
          data: data,
        });
      } else {
        res = await axios({
          method: "post",
          url: api.value + "/api/sysConfig/add",
          headers: {
            tokenP: sessionStorage.getItem("tokenP"),
            user_head: sessionStorage.getItem("userhead"),
          },
          data: data,
        });
      }
      console.log(res, "考核维度管理");
      if (res.data.code == 200) {
        nextTick(() => {
          accountRef.value.resetFields();
        });
        accountVisible.value = false;
        getList();

        ElMessage({
          type: "success",
          showClose: true,
          message: res.data.message,
          center: true,
        });
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: res.data.message,
          center: true,
        });
      }
    }
  });
};

// 删除考核维度
const del = async (row) => {
  let data = new FormData();
  data.append("id", row.id);
  let res = await axios({
    method: "post",
    url: api.value + "/api/sysConfig/del",
    headers: {
      tokenP: sessionStorage.getItem("tokenP"),
      user_head: sessionStorage.getItem("userhead"),
    },
    data: data,
  });
  if (res.data.code == 200) {
    getList();
    ElMessage({
      type: "success",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  } else {
    ElMessage({
      type: "error",
      showClose: true,
      message: res.data.message,
      center: true,
    });
  }
};

// 多选框功能
const handleSelectionChange = (val) => {
  console.log(val);
  selectData.list = val;
};

// 表格斑马纹颜色修改
const tableRowClassName = ({ row, rowIndex }) => {
  if (rowIndex % 2 === 0) {
    return "even";
  } else if (rowIndex % 2 !== 0) {
    return "odd";
  }
  return "";
};

onBeforeMount(() => {
  api.value = store.state.user.api;
  getList();
});
onUnmounted(() => {
  // document.removeEventListener("keyup", Enters);
});
</script>

<style scoped lang="scss">
.content-box {
  min-width: 1000px;
  width: calc(100vw - 260px);
  height: calc(100vh - 105px);
  margin: 20px auto;
  background-color: #fff;
  color: #fff;
  display: flex;
  flex-direction: column;
  box-shadow: 0px 3px 10px rgba(0, 97, 255, 0.2);

  .left {
    // width: calc(100wh - 40px);
    display: flex;
    align-items: center;
    height: 60px;
    margin: 0 30px;
    border-bottom: 1px solid #ccc;
    color: #000;
    font-size: 18px;
    font-weight: 600;

    .cameratxt {
      display: block;
      height: 60px;
      line-height: 60px;
    }
  }
  .middle {
    width: calc(100% - 60px);
    margin: 0 auto;

    color: #000;
    .roles {
      padding: 10px 0 10px 0;
      font-weight: 600;
      letter-spacing: 1px;
    }
    // border-bottom: 1px solid rgb(231, 231, 231);
    .filter {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      margin: 0 0 10px 0;
      .el-checkbox {
        margin-right: 15px;
        color: #000;
      }
      .grade {
        font-size: 14px;
        padding: 0 0 0 15px;
      }
    }

    .gongneng {
      margin: 8px 0;
      // .el-button {
      //   color: #fff;
      //   border: none;
      // }
      .add {
        color: #fff;
        border: none;
        background: linear-gradient(
          90deg,
          rgba(33, 107, 255, 1) 0%,
          rgba(102, 182, 255, 1) 100%
        );
      }
      .imp {
        border: 1px solid rgba(33, 107, 255, 1);
        color: rgba(33, 107, 255, 1);
      }
    }
  }
  .scroll {
    width: 700px;
    height: 500px;
  }
  .footer {
    width: calc(100% - 60px);
    height: calc(100% - 82px);
    margin: 10px auto 10px;

    .el-table--fit {
      height: 100%;
      :deep(.el-table__header-wrapper) {
        background-color: #000;
        font-size: 14px;
        color: #000;

        .cell {
          color: #000;
        }
      }

      :deep(.el-table__row) {
        height: 40px;
        font-size: 14px;
        color: #000;
      }

      :deep(.el-table__row td) {
        padding: 0;
        border: 0;
      }

      .el-button--primary {
        margin-left: 5px;
      }

      :deep(.el-table__body .even) {
        background-color: #fff;
      }

      :deep(.el-table__body .odd) {
        background-color: rgba(240, 243, 247, 1);
      }

      :deep(.options) {
        display: flex;
        justify-content: center;
        align-items: center;

        .reset {
          margin-right: 15px;
          color: rgba(30, 125, 251, 1);
          cursor: pointer;
        }

        .look {
          margin-right: 15px;
          color: rgba(30, 125, 251, 1);
          cursor: pointer;
        }

        .del {
          color: rgba(212, 48, 48, 1);
          cursor: pointer;
        }
      }
    }
    .gongneng {
      margin: 15px 0;
      // .el-button {
      //   color: #fff;
      //   border: none;
      // }
      .add {
        color: #fff;
        border: none;
        background: linear-gradient(
          90deg,
          rgba(33, 107, 255, 1) 0%,
          rgba(102, 182, 255, 1) 100%
        );
      }
      .imp {
        border: 1px solid rgba(33, 107, 255, 1);
        color: rgba(33, 107, 255, 1);
      }
    }

    // 项目新增样式
    :deep(.account) {
      // height: 300px;
      border-radius: 11px;

      .el-dialog__header {
        border-radius: 11px 11px 0 0;
        background: rgba(237, 241, 245, 1);
        font-weight: 600;
        margin: 0;
        height: 32px;

        .el-dialog__headerbtn {
          outline: none;
        }
      }

      .el-dialog__body {
        padding: 25px 30px 0 30px;
        .el-form {
          .el-form-item {
            display: flex;
            align-items: center;
          }
          .resetPassword {
            color: rgba(43, 151, 252, 1);
            padding-left: 40px;
            display: inline-block;
            margin-bottom: 15px;
          }
          .options {
            margin: 60px 0 40px 0;
            .el-form-item__content {
              display: flex;
              flex-direction: row-reverse;
              .queding {
                margin-left: 20px;
                background: linear-gradient(
                  90deg,
                  rgba(33, 107, 255, 1) 0%,
                  rgba(102, 182, 255, 1) 100%
                );
                border: none;
              }
              .quxiao {
                border: 1px solid rgba(43, 151, 252, 1);
                color: rgba(43, 151, 252, 1);
              }
            }
          }
        }
      }
    }
  }

  .el-pagination {
    width: calc(100vw - 300px);
    margin: 20px auto;
    justify-content: flex-end;
    :deep(.el-pagination__total) {
      color: #000;
    }

    :deep(.el-pagination__goto) {
      color: #000;
    }

    :deep(.el-pagination__classifier) {
      color: #000;
    }

    :deep(.el-input__wrapper) {
      // border: 1px solid rgba(0, 0, 0, 1);
      // border-radius: 5px;
      // box-shadow: none;
      width: 100px;
    }

    :deep(.el-pager li) {
      margin: 0 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }

    :deep(.el-pager li.is-active) {
      // background-color: rgba(111, 182, 184, 1);
      color: rgba(0, 149, 255, 1);
      border: 1px solid rgba(0, 149, 255, 1);
    }

    :deep(.btn-prev) {
      margin-right: 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }

    :deep(.btn-next) {
      margin-left: 5px;
      border: 1px solid rgba(0, 0, 0, 1);
      border-radius: 5px;
      background-color: transparent;
    }
  }
}

.el-input {
  width: 192px;
}
</style>
