<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
    </avue-crud>
  </basic-container>
</template>

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

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        columnBtn: false,
        selection: false,
        dialogClickModal: false,
        dialogWidth: "45%",
        column: [
          {
            label: "阿里云配置",
            prop: "configId",
            type: "tree",
            dicUrl: "/api/iot/ali/config/select",
            props: {
              label: "configName",
              value: "id"
            },
            span: 24,
            labelWidth: 110,
            search: true,
            searchLabelWidth: 90,
            searchPlaceholder: "请选择阿里云配置",
            rules: [{
              required: true,
              message: "请选择阿里云配置",
              trigger: "click"
            }],
            editDisplay: false,
          },
          {
            label: "消费组名称",
            prop: "consumerName",
            span: 24,
            labelWidth: 110,
            searchPlaceholder: "请输入消费组名称",
            rules: [{
              required: true,
              message: "请输入消费组名称",
              trigger: "blur"
            }]
          },
          {
            label: "消费组ID",
            prop: "consumerId",
            labelWidth: 110,
            search: true,
            searchPlaceholder: "请输入消费组ID",
            rules: [{
              required: true,
              message: "请输入消费组ID",
              trigger: "blur"
            }],
            addDisplay: false,
            editDisplay: false,
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.ali_config_add, false),
        viewBtn: this.vaildData(this.permission.ali_config_view, false),
        delBtn: this.vaildData(this.permission.ali_config_delete, false),
        editBtn: this.vaildData(this.permission.ali_config_edit, false)
      };
    }
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        window.console.log(error);
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        this.onLoad(this.page);
        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: "操作成功!"
          });
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    }
  }
};
</script>

<style>
</style>
