<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 v-if="permission.server_eng_switch">
                    <el-button @click="handleStatus(row,'禁用')"
                               text
                               type="primary"
                               v-if="row.isDeleted =='0'"
                               :size="size">禁用</el-button>
                    <el-button @click="handleStatus(row,'启用')"
                               text
                               type="primary"
                               v-if="row.isDeleted =='3'"
                               :size="size">启用</el-button>
                </template>
            </template>

            <template #isDeleted="{row}">
                <el-tag type="primary" v-if="row.isDeleted == '0'">启用</el-tag>
                <el-tag type="info" v-if="row.isDeleted == '3'">禁用</el-tag>
            </template>
        </avue-crud>
    </basic-container>
</template>

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

  export default {
    data() {
      return {
        query: {
          role: '1'
        },
        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_eng_add, false),
          editBtn: this.validData(this.permission.server_eng_edit, false),
          delBtn: this.validData(this.permission.server_eng_del, false),
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    mounted(){
      sendThis(this);
    },
    methods: {
      onLoad() {
        this.loading = true;
        this.query.role = '1'
        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)) {
          getDetail(this.formData.id).then(res => {
            this.formData = res.data.data;
            if(type == 'edit'){
              this.formData.password = '';
            }
          });
        }
        if (["add"].includes(type)) {
          this.formData.role = '1';
        }
        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: '操作成功!',
            });
          });
      },
      //更新状态
      handleStatus(row, type) {
        let text = '';
        if((type == '启用'&&row.isDeleted != '3') || (type == '禁用'&&row.isDeleted != '0')){
          this.$message.warning("该数据不可"+type);
          return
        }
        if(row.isDeleted == '0'){
          text = '禁用'
        }else{
          text = '启用'
        }
        this.$confirm('是否确定'+ text +'这条数据?', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          return updateStatus({
            id:row.id,
            status:row.isDeleted == '0' ? '3' : '0'
          });
        }).then(() => {
          this.onLoad();
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          this.$refs.crud.toggleSelection();
        });
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
    }
  };
</script>

<style>
</style>
