<template>
  <basic-container>
    <avue-crud :option="optionChild"
               :table-loading="loadingChild"
               :data="dataChild"
               :page="pageChild"
               :permission="permissionListChild"
               :before-open="beforeOpenChild"
               v-model="formChild"
               ref="crud"
               @row-update="rowUpdateChild"
               @row-save="rowSaveChild"
               @row-del="rowDelChild"
               @search-change="searchChangeChild"
               @search-reset="searchResetChild"
               @selection-change="selectionChangeChild"
               @current-change="currentChangeChild"
               @size-change="sizeChangeChild"
               @refresh-change="refreshChangeChild"
               @on-load="onLoadChild">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.alarmgroupuser_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/device/iotalarmgroupuser";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        formChild: {},
        query: {},
        loadingChild: true,
        pageChild: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        optionChild: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "组id",
              prop: "groupId",
              rules: [{
                required: true,
                message: "请输入组id",
                trigger: "blur"
              }]
            },
            {
              label: "责任人",
              prop: "person",
              rules: [{
                required: true,
                message: "请输入责任人",
                trigger: "blur"
              }]
            },
            {
              label: "责任人电话",
              prop: "phone",
              rules: [{
                required: true,
                message: "请输入责任人电话",
                trigger: "blur"
              }]
            },
            {
              label: "是否为处理人",
              prop: "phone",
              type: "switch",
              mock:{
                type:'dic'
              },
              rules: [{
                required: true,
                message: "请输入是否为处理人",
                trigger: "blur"
              }]
            },
          ]
        },
        dataChild: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionListChild() {
        return {
          addBtn: this.vaildData(this.permission.alarmgroupuser_add, false),
          viewBtn: this.vaildData(this.permission.alarmgroupuser_view, false),
          delBtn: this.vaildData(this.permission.alarmgroupuser_delete, false),
          editBtn: this.vaildData(this.permission.alarmgroupuser_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSaveChild(row, done, loading) {
        add(row).then(() => {
          this.onLoadChild(this.pageChild);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdateChild(row, index, done, loading) {
        update(row).then(() => {
          this.onLoadChild(this.pageChild);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDelChild(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoadChild(this.pageChild);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDeleteChild() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoadChild(this.pageChild);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpenChild(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.formChild.id).then(res => {
            this.formChild = res.data.data;
          });
        }
        done();
      },
      searchResetChild() {
        this.query = {};
        this.onLoadChild(this.pageChild);
      },
      searchChangeChild(params, done) {
        this.query = params;
        this.pageChild.currentPage = 1;
        this.onLoadChild(this.pageChild, params);
        done();
      },
      selectionChangeChild(list) {
        this.selectionList = list;
      },
      selectionClearChild() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChangeChild(currentPage){
        this.pageChild.currentPage = currentPage;
      },
      sizeChangeChild(pageSize){
        this.pageChild.pageSize = pageSize;
      },
      refreshChangeChild() {
        this.onLoadChild(this.pageChild, this.query);
      },
      onLoadChild(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.pageChild.total = data.total;
          this.dataChild = data.records;
          this.loadingChild = false;
          this.selectionClearChild();
        });
      }
    }
  };
</script>

<style>
</style>
