<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"
    >
      <template slot-scope="{ disabled, size, value }" slot="activationNumForm">
        <el-input
          :disabled="disabled"
          :size="size"
          v-model="value == -1 ? '-' : value"
        ></el-input>
      </template>
      <template slot-scope="{ disabled, size, value }" slot="accountForm">
        <el-input
          :disabled="disabled"
          :size="size"
          v-model="value == '' ? '-' : value"
        ></el-input>
      </template>
    </avue-crud>
  </basic-container>
</template>

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

export default {
  data() {
    return {
      form: {

      },
      query: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      isSources: {
        isSource: 2,
      },
      option: {
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        addBtn: true,
        dialogDrag: true, //表单窗口拖拽
        viewBtn: true,
        delBtn: true,
        columnBtn: false,
        selection: false,
        dialogClickModal: false,
        dialogWidth: "60%",
        labelWidth: 150,
        column: [
          {
            label: "通道名称",
            prop: "channelId",
            type: "select",
            span: 12,

            search: true,
            dataType: "String",
            dicUrl: "/api/iot-sim/channel/fillData",
            props: {
              label: "name",
              value: "id",
            },
            rules: [
              {
                required: true,
                message: "请选择通道名称",
                trigger: "change",
              },
            ],
          },
          {
            label: "目标流量池编号",
            prop: "code",
            span: 12,

            searchLabelWidth: 90,
            search: false,
            rules: [
              {
                required: true,
                message: "请输入设备序列号",
                trigger: "blur",
              },
            ],
          },
          {
            label: "目标流量池名称",
            prop: "name",
            span: 12,

            searchLabelWidth: 130,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入流量池名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "目标流量池总量",
            prop: "total",
            span: 12,

            searchLabelWidth: 90,
            search: false,
            hide: false,
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入流量池总量",
                trigger: "blur",
              },
              {
                pattern: /^\d+(\.\d+)?$/,
                message: "请输入正确的流量池总量",
                trigger: "blur",
              },
            ],
          },
          {
            label: "提醒阈值",
            prop: "remindRatio",
            span: 12,

            searchLabelWidth: 90,
            search: false,
            hide: false,
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入提醒阈值",
                trigger: "blur",
              },
              {
                pattern: /^\d+(\.\d+)?$/,
                message: "请输入正确的提醒阈值",
                trigger: "blur",
              },
            ],
          },

          {
            label: "是否立即生效",
            prop: "isNowActivate",
            type: "select",
            dicUrl: "/api/iot-system/dict/dictionary?code=now_activate",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            span: 12,

            search: false,
            hide: true,
            rules: [
              {
                required: true,
                message: "请选择是否立即生效",
                trigger: "change",
              },
            ],
          },
          {
            label: "流量池类型",
            type: "select",
            span: 12,
            dicUrl: "/api/iot-system/dict/dictionary?code=flow_pool_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            prop: "type",

            searchLabelWidth: 140,
            search: false,
            rules: [
              {
                required: true,
                message: "请选择流量池类型",
                trigger: "change",
              },
            ],
          },

          {
            label: "单卡最大使用量(M)",
            prop: "maxUsage",
            span: 12,

            search: false,
            hide: true,
            type: "number",
            rules: [
              {
                required: true,
                message: "请输入单卡最大使用量(M)",
                trigger: "blur",
              },
              {
                pattern: /^\d+(\.\d+)?$/,
                message: "请输入正确的使用量",
                trigger: "blur",
              },
            ],
          },
          {
            label: "已激活数量",
            prop: "activationNum",
            span: 12,

            search: false,
            hide: true,
            addDisplay: false,
            editDisplay: false,
            formslot: true,
            rules: [
              {
                required: true,
                message: "请输入已激活数量",
                trigger: "blur",
              },
              {
                pattern: /^\d+?$/,
                message: "请输入正确的已激活数量",
                trigger: "blur",
              },
            ],
          },
          {
            label: "管理员名称",
            prop: "account",
            span: 12,

            search: false,
            addDisplay: false,
            editDisplay: false,
            hide: true,
            formslot: true,
            rules: [
              {
                required: true,
                message: "请输入管理员账号",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        // viewBtn: this.vaildData(this.permission.sourceApi_view, false),
        // editBtn: this.vaildData(this.permission.sourceApi_edit, false),
        // delBtn: this.vaildData(this.permission.sourceApi_delete, false),
      };
    },
  },
  methods: {
    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();
    },
    //新增
    rowSave(row, done, loading) {
      add(Object.assign(row, this.isSources)).then(
        (res) => {
          if (res.data.data == 1) {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            done();
          } else {
            this.$message({
              type: "error",
              message: "操作失败!",
            });
            done(); //关闭表单
            loading(); // 按钮停止加载
          }
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    //修改
    rowUpdate(row, index, done, loading) {
      update(row).then(
        (res) => {
          if (res.data.data == 1) {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            done();
          } else {
            this.$message({
              type: "error",
              message: "操作失败!",
            });
            done(); //关闭表单
            loading(); // 按钮停止加载
          }
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    //删除
    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;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, this.isSources)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
  },
};
</script>

<style></style>
