<template>
  <el-form ref="queryFormRef" :model="queryParams" :inline="true" style="height: 65px;">
    <el-form-item label="角色名称">
      <el-input v-model="queryParams.roleName" placeholder="角色名称" width="120px" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" class="button" @click="queryList">查询角色</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" class="button" @click="handleAdd" :disabled="!adminFlag">新增角色</el-button>
    </el-form-item>
  </el-form>
  <!-- <div class="wrapper"> -->
  <el-table v-loading="loading" :data="state.tableData" border stripe style="width: 100%" :height="tableHeight"
    highlight-current-row @current-change="handleCurrentRowChange"
    :header-cell-style="{ background: '#606266', color: '#FFFFFF' }">
    <el-table-column fixed type="index" width="50" align="center" />
    <el-table-column fixed prop="roleName" label="角色名称" header-align="center" width="120" />
    <el-table-column label="角色状态" header-align="center" align="center" width="100">
      <template #default="scope">
        <span v-if="scope.row.roleFlag === '0'" style="color:red;">✖</span>
        <span v-if="scope.row.roleFlag === '1'" style="color:green;">✔</span>
      </template>
    </el-table-column>
    <el-table-column label="台账权限" header-align="center" align="center" width="100">
      <template #default="scope">
        <span v-if="scope.row.accAuth === '0'" style="color:red;">✖</span>
        <span v-if="scope.row.accAuth === '1'" style="color:green;">✔</span>
      </template>
    </el-table-column>
    <el-table-column label="字典权限" header-align="center" align="center" width="100">
      <template #default="scope">
        <span v-if="scope.row.dicAuth === '0'" style="color:red;">✖</span>
        <span v-if="scope.row.dicAuth === '1'" style="color:green;">✔</span>
      </template>
    </el-table-column>
    <el-table-column label="拷机权限" header-align="center" align="center" width="100">
      <template #default="scope">
        <span v-if="scope.row.checkAuth === '0'" style="color:red;">✖</span>
        <span v-if="scope.row.checkAuth === '1'" style="color:green;">✔</span>
      </template>
    </el-table-column>
    <el-table-column label="审核权限" header-align="center" align="center" width="100">
      <template #default="scope">
        <span v-if="scope.row.reviewAtuth === '0'" style="color:red;">✖</span>
        <span v-if="scope.row.reviewAtuth === '1'" style="color:green;">✔</span>
      </template>
    </el-table-column>
    <el-table-column label="远程配置权限" header-align="center" align="center" width="110">
      <template #default="scope">
        <span v-if="scope.row.cfgAuth === '0'" style="color:red;">✖</span>
        <span v-if="scope.row.cfgAuth === '1'" style="color:green;">✔</span>
      </template>
    </el-table-column>
    <el-table-column label="召测权限" header-align="center" align="center" width="100">
      <template #default="scope">
        <span v-if="scope.row.acqAuth === '0'" style="color:red;">✖</span>
        <span v-if="scope.row.acqAuth === '1'" style="color:green;">✔</span>
      </template>
    </el-table-column>
    <el-table-column label="库房权限" header-align="center" align="center" width="100">
      <template #default="scope">
        <span v-if="scope.row.stkAuth === '0'" style="color:red;">✖</span>
        <span v-if="scope.row.stkAuth === '1'" style="color:green;">✔</span>
      </template>
    </el-table-column>
    <el-table-column label="借还权限" header-align="center" align="center" width="100">
      <template #default="scope">
        <span v-if="scope.row.lrAuth === '0'" style="color:red;">✖</span>
        <span v-if="scope.row.lrAuth === '1'" style="color:green;">✔</span>
      </template>
    </el-table-column>
    <el-table-column label="维修权限" header-align="center" align="center" width="100">
      <template #default="scope">
        <span v-if="scope.row.frAuth === '0'" style="color:red;">✖</span>
        <span v-if="scope.row.frAuth === '1'" style="color:green;">✔</span>
      </template>
    </el-table-column>
    <el-table-column label="质检权限" header-align="center" align="center" width="100">
      <template #default="scope">
        <span v-if="scope.row.certifAuth === '0'" style="color:red;">✖</span>
        <span v-if="scope.row.certifAuth === '1'" style="color:green;">✔</span>
      </template>
    </el-table-column>
    <el-table-column fit fixed="right" label="" align="left" min-width="150">
      <template #default="scope">
        <el-button type="text" @click="handleEdit(scope.row)" :disabled="!adminFlag">编辑</el-button>
        <el-button type="text" @click="submitDeleteForm(scope.row)" :disabled="!adminFlag">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog :title="dialog.title" v-model="dialog.visible" width="450px" draggable center>
    <el-form ref="dataFormRef" :model="formData" label-width="100px" :rules="state.rules">
      <el-form-item label="角色名称:" prop="roleName">
        <el-input v-model="formData.roleName" placeholder="角色名称" width="120px" />
      </el-form-item>
      <el-form-item label="角色状态:">
        <el-select v-model="formData.roleFlag" class="filter-item" placeholder="角色状态" style="width: 300px;">
          <el-option v-for="(key, value) in RoleStateMap" :key="key" :label="key" :value="value" />
        </el-select>
      </el-form-item>
      <el-descriptions title="权限列表" direction="vertical" column="5" border>
        <el-descriptions-item label="台账">
          <el-switch v-model="formData.accAuth" inline-prompt="true" active-value="1" inactive-value="0" />
        </el-descriptions-item>
        <el-descriptions-item label="字典">
          <el-switch v-model="formData.dicAuth" inline-prompt="true" active-value="1" inactive-value="0" />
        </el-descriptions-item>
        <el-descriptions-item label="拷机">
          <el-switch v-model="formData.checkAuth" inline-prompt="true" active-value="1" inactive-value="0" />
        </el-descriptions-item>
        <el-descriptions-item label="审核">
          <el-switch v-model="formData.reviewAtuth" inline-prompt="true" active-value="1" inactive-value="0" />
        </el-descriptions-item>
        <el-descriptions-item label="远程配置">
          <el-switch v-model="formData.cfgAuth" inline-prompt="true" active-value="1" inactive-value="0" />
        </el-descriptions-item>
        <el-descriptions-item label="召测">
          <el-switch v-model="formData.acqAuth" inline-prompt="true" active-value="1" inactive-value="0" />
        </el-descriptions-item>
        <el-descriptions-item label="库房">
          <el-switch v-model="formData.stkAuth" inline-prompt="true" active-value="1" inactive-value="0" />
        </el-descriptions-item>
        <el-descriptions-item label="借还">
          <el-switch v-model="formData.lrAuth" inline-prompt="true" active-value="1" inactive-value="0" />
        </el-descriptions-item>
        <el-descriptions-item label="维修">
          <el-switch v-model="formData.frAuth" inline-prompt="true" active-value="1" inactive-value="0" />
        </el-descriptions-item>
        <el-descriptions-item label="质检">
          <el-switch v-model="formData.certifAuth" inline-prompt="true" active-value="1" inactive-value="0" />
        </el-descriptions-item>
      </el-descriptions>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, toRefs, toRef, ref, onMounted } from "vue";
import { ElForm, ElMessage, ElMessageBox } from 'element-plus';
import FacUserRoleAction from "@/api/usermanagement/facuserrole";
import { FacUserRoleQueryParam, FacUserRoleModel, Dialog } from '@/types';
import { PageResult } from "@/types/api/base";
import { stat } from "fs";

const queryFormRef = ref(ElForm); // 属性名必须和元素的ref属性值一致
const dataFormRef = ref(ElForm); // 属性名必须和元素的ref属性值一致

const RoleStateMap = {
  0: '停用',
  1: '使用中',
};

const loading = ref(false)
const adminFlag = localStorage.getItem("AdminFlag")?.toString() === '1';
const state = reactive({
  currentType: 0,
  queryParams: {} as FacUserRoleQueryParam,
  dialog: {} as Dialog,
  formData: {} as FacUserRoleModel,
  RoleStateMap,
  tableData: [] as FacUserRoleModel[],
  rules: {
    roleName: [{ required: true, message: "角色名称不能为空", trigger: "blur", },],
  },
});

const {
  queryParams,
  dialog,
  formData,
  tableData
} = toRefs(state);

const tableHeight = ref(document.documentElement.clientHeight - 150);

const currentRow = ref()

const handleCurrentRowChange = (val: FacUserRoleModel | undefined) => {
  currentRow.value = val
}

const handleAdd = () => {
  state.currentType = 0;
  state.formData = {} as FacUserRoleModel;
  state.formData.roleFlag = '1';
  state.dialog = {
    title: '新增角色',
    visible: true,
    disabled: false,
  };
}

const handleEdit = (row: any) => {
  state.currentType = 1;
  state.formData = JSON.parse(JSON.stringify(row));;
  state.dialog = {
    title: '编辑角色',
    visible: true,
    disabled: false,
  };
}

const queryList = () => {
  loading.value = true;
  FacUserRoleAction.QueryRoleList().then(result => {
    if (result.status === 200) {
      const { res, flag, error } = result.data;
      if (flag != undefined && flag == 1) {
        state.tableData = res as FacUserRoleModel[];
      } else {
        ElMessage.error(error);
      }
    }
  }).catch(error => {
    //请求失败时的操作     
    ElMessage.error(error);
  })
  loading.value = false;
};

const submitForm = () => {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      if (state.currentType == 0) {
        state.formData.createMan = "admin";
        FacUserRoleAction.AddFacUserRole(state.formData).then(result => {
          if (result.status === 200) {
            const { res, flag, error } = result.data;
            if (flag === "1") {
              ElMessage.success('新增成功');
              cancel();
              queryList();
            } else {
              ElMessage.error(error);
            }
          }
        });
      }
      else {
        FacUserRoleAction.EditFacUserRole(state.formData).then(result => {
          if (result.status === 200) {
            const { res, flag, error } = result.data;
            if (flag === "1") {
              ElMessage.success('编辑成功');
              cancel();
              queryList();
            } else {
              ElMessage.error(error);
            }
          }
        });
      }
    }
  });
};

const submitDeleteForm = (row: any) => {
  ElMessageBox.confirm(
    ' 是否删除角色[' + row.roleName + ']? ',
    '提示',
    {
      cancelButtonText: '取消',
      confirmButtonText: '确认',
      type: 'warning',
      center: true,
    }
  )
    .then(() => {
      FacUserRoleAction.DeleteFacUserRole(row.id).then(result => {
        if (result.status === 200) {
          const { res, flag, error } = result.data;
          if (flag === "1") {
            ElMessage.success('删除成功');
            queryList();
          } else {
            ElMessage.error(error);
          }
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除成功!',
      })
    })


};

const cancel = () => {
  dataFormRef.value.resetFields();
  state.formData = {} as FacUserRoleModel;
  state.dialog.visible = false;
}

onMounted(() => {
  window.onresize = function () {
    tableHeight.value = document.documentElement.clientHeight - 150
  }

  queryList();
})
</script>

<style lang="scss" scoped>
.button {
  margin: 10px 0;
}
</style>