<template>
    <basic-container>
        <avue-crud
                ref="crud"
                :option="option"
                :table-loading="loading"
                :data="listData"
                :permission="permissionList"
                @on-load="onLoad"
                @row-del="rowDel"
                @refresh-change="onLoad"
                @search-change="searchChange"
                @search-reset="searchReset"
                @selection-change="selectionChange"
                v-model="formData"
                @row-save="rowSave"
                :before-open="beforeOpen"
                @row-update="rowUpdate"
                v-model:page="page"
                @current-change="currentChange"
                @size-change="sizeChange">
            <template #menu-left>

            </template>
            <template #menu="{size,row,index}">
            </template>
        </avue-crud>
    </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, updateStatus, remove} from "@/api/systemSet/serverRole";
  import {option,sendThis} from "@/option/systemSet/serverRole";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        query: {},
        loading: false,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        option: option,
        listData: [{}],
        selectionList: [],
        formData: {},
        // search: {},
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.server_role_add, false),
          editBtn: this.validData(this.permission.server_role_edit, false),
          delBtn: this.validData(this.permission.server_role_del, false),
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    mounted(){
      sendThis(this);
    },
    methods: {
      onLoad() {
        this.loading = true;
        getList(this.page, this.query).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.listData = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
      searchReset() {
        this.query = {};
        this.onLoad();
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad();
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {

        }
        done();
      },
      rowSave(row, done, loading) {
        add(row).then(res => {
          this.onLoad();
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          done(row);
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      handleEdit(){
        this.formData = this.selectionList[0];
        this.beforeOpen(()=>{
          this.$refs.crud.rowEdit(this.formData);
        },"edit")
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm('确定将选择数据删除?', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: 'success',
              message: '操作成功!',
            });
          });
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
    }
  };
</script>

<style>
</style>
