<template>
  <div>
    <!--条件-->
    <el-form :model="listQuery" ref="queryForm" :inline="true">
      <el-form-item label="角色编码">
        <el-input
          v-model="listQuery.roleId"
          placeholder="请输入角色编码"
        ></el-input>
      </el-form-item>
      <el-form-item label="角色名称">
        <el-input
          v-model="listQuery.roleName"
          placeholder="请输入角色名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getData(true, 'search')"
          >查询</el-button
        >
      </el-form-item>
    </el-form>

    <!--列表-->
    <div>
      <el-table
        ref="multipleTable"
        row-key="id"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        :data="list"
        element-loading-text="加载中"
        fit
        stripe
      >
        <template slot="empty">
          <img src="@/assets/nopage.png" alt />
          <p>暂无数据</p>
        </template>
        <el-table-column type="selection" width="55" reserve-selection>
        </el-table-column>
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column label="角色编码" prop="id" />

        <el-table-column label="角色名称" prop="name" />
      </el-table>
    </div>

    <!--分页-->
    <yl-pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getData(true)"
    />
    <div slot="footer" class="dialog-footer" style="text-align: right">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </div>
</template>

<script>
import { getRoleData } from "@/api/system/role";
import ylPagination from "@/components/yl-pagination";

export default {
  components: {
    ylPagination
  },

  props: {
    checkRole: {
      type: Array,
      default: []
    }
  },

  data() {
    return {
      multipleSelection: [],
      list: [], //列表
      total: 0, //分页
      listLoading: true,
      listQuery: {
        //条件查询
        roleName: "",
        current: 1,
        size: 10,
        roleId: ""
      }
    };
  },

  created() {
    this.getData();
  },

  methods: {
    // 选中数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 回显数据
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row, true);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },

    // 关闭
    close() {
      this.$emit("rolesClose", false);
    },

    // 提交
    submit() {
      this.$emit("rolesSubmit", this.multipleSelection);
    },

    //获取列表数据
    getData(query, type) {
      if (type == "search") {
        this.listQuery.current = 1;
      }
      this.listLoading = true;
      getRoleData(this.listQuery)
        .then((response) => {
          this.list = [];
          if (response.data.records.length > 0) {
            response.data.records.forEach((item) => {
              this.list.push({
                id: item.roleId,
                name: item.roleName
              });
            });
          }
          this.total = response.data.total;
          this.listLoading = false;
          if (!query) {
            this.toggleSelection(this.checkRole);
          }
        })
        .catch((err) => {
          this.listLoading = false;
        });
    }
  }
};
</script>

<style scoped>
</style>
