<template>
  <div class="app-container">
    <div class="top-bar">
      <SearchBar :config="searchConfig" @search="search" @reset="reset" />
      <el-button
        type="primary"
        @click.native="handleOperation({ type: 'add' })"
        icon="el-icon-plus"
      >
        新建</el-button
      >
    </div>
    <TablePage
      :data="list.records"
      :tableTitle="tableTitle"
      :operates="tableOperates"
      v-loading="loading"
    >
      <template v-slot:status="data">
        <el-popconfirm
          :title="getStatusTitle(data.scope.status)"
          @confirm="handleChangeStatus(data.scope)"
        >
          <el-switch
            slot="reference"
            :value="Boolean(Number(data.scope.status))"
          ></el-switch>
        </el-popconfirm>
      </template>

      <template v-slot:operates="scope">
        <TableOperation
          :operations="tableOperations"
          :rawData="scope.scope.row"
          @handleOperation="handleOperation"
        ></TableOperation>
      </template>
    </TablePage>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="Number(list.current)"
      :page-sizes="[10, 20, 40, 50]"
      :page-size="Number(list.size)"
      layout="total, sizes, prev, pager, next, jumper"
      :total="Number(list.total)"
      class="pagination"
    >
    </el-pagination>
    <InfoEditDialog
      :visible="drawerVisible"
      :form="form"
      @handleClose="handleClose"
      @refresh="loadData"
    />
    <DataEditDialog
      :visible="dataEditDialog"
      :id="dataScopeForm.id"
      :dataScope="dataScopeForm.dataScope"
      @handleCloseDataEdit="handleCloseDataEdit"
      @refresh="loadData"
    />
  </div>
</template>

<script>
import TablePage from "@/components/Table/TablePage.vue";
import TableOperation from "@/components/Table/TableOperation.vue";
import { title, operates, operations } from "./config";
import { getRoleList, getRoleById, deleteRole, upadateRole } from "@/api/user";
import InfoEditDialog from "./components/InfoEditDialog";
import DataEditDialog from "./components/DataEditDialog";
import SearchBar from "@/components/SearchBar";

export default {
  components: {
    TablePage,
    TableOperation,
    InfoEditDialog,
    DataEditDialog,
    SearchBar,
  },
  data() {
    return {
      list: {
        records: [],
        size: 10,
        current: 1,
        total: 0,
      },
      searchForm: {
        name: "",
        status: "",
      },
      searchConfig: [
        {
          prop: "name",
          type: "input",
          label: "角色名称",
        },
        {
          prop: "status",
          type: "select",
          label: "状态",
          selectOptions: [
            {
              label: "正常",
              value: "1",
            },
            {
              label: "弃用",
              value: "0",
            },
          ],
        },
      ],
      drawerVisible: false,
      form: {
        name: "",
        remark: "",
        status: 1,
        permissionRespNodes: [],//菜单权限树
        dataScope: 5,//默认数据权限为自己
      },
      dataScopeForm: {},//编辑数据权限的表单
      dataEditDialog: false, //编辑数据的对话框
      loading:false
    };
  },

  computed: {
    tableTitle() {
      return title;
    },
    tableOperates() {
      return operates;
    },
    tableOperations() {
      return operations;
    },
    getStatusTitle(status) {
      return (status) => {
        if (Number(status)) {
          return "是否确定要下架？";
        } else {
          return "是否确定要上架？";
        }
      };
    },
  },

  mounted() {
    this.loadData();
  },
  methods: {
    async search(form) {
      this.loading = true
      var params = {
        page: this.list.current,
        limit: this.list.size,
        ...form,
      };
      if (params.status == "") {
        delete params.status;
      }
      const res = await getRoleList(params);
      if (res.code == 0) {
        this.list = res.data;
      }
      this.loading = false
    },

    reset() {
      this.loadData();
    },

    //   加载表格数据
    async loadData() {
      var params = {
        page: this.list.current,
        limit: this.list.size,
      };
      const res = await getRoleList(params);
      if (res.code == 0) {
        this.list = res.data;
      }
    },
    async handleOperation(value, row) {
      switch (value.type) {
        case "add":
          this.drawerVisible = true;
          this.form = {
            status: 1,
            permissionRespNodes: null,
          };
          break;
        case "edit":
          const { id } = row;
          let res = await getRoleById(id);
          this.form = res.data;
          this.drawerVisible = true;
          break;
        case "delete":
          let deleteRes = await deleteRole(row.id);
          if (deleteRes.code == 0) {
            this.$message.success("刪除成功！");
            this.loadData();
          }
          break;
        case "dataPermission":
          this.dataScopeForm = row;
          this.dataEditDialog = true;
          break;
      }
    },

    async handleChangeStatus(row) {
      const { status } = row;
      let newStatus = status === "0" ? "1" : "0";
      const params = {
        ...row,
        status: newStatus,
      };
      let res = await upadateRole(params);
      if (res.code == 0) {
        this.loadData();
        this.$message.success("修改成功！");
      }
    },

    // 改变页容量
    handleSizeChange(value) {
      this.list.size = value;
      this.loadData();
    },

    // 改变当前显示页
    handleCurrentChange(value) {
      this.list.current = value;
      this.loadData();
    },

    // 关闭Dialog
    handleClose() {
      this.drawerVisible = false;
      this.form = {
        status: 1,
        permissionRespNodes: [],
        dataScope: 5,
      };
    },

    handleCloseDataEdit() {
      this.dataEditDialog = false;
      this.dataScopeForm = {
        dataScope: 5,
      };
    },
  },
};
</script>

<style lang="scss" scoped>
.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.pagination {
  margin: 16px;
}
.el-button {
  margin-bottom: 22px;
}
::v-deep .el-card__header {
  padding: 10px 20px;
  font-weight: bold;
}
</style>