<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 {
  getDetail,
  getList,
  add,
  remove,
  update,
} from "@/api/packagemanage/package";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";
export default {
  data() {
    return {
      form: {},
      query: {},
      loading: false,
      isSources: {
        isSource: 1,
      },
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        tip: false,
        searchShow: true,
        dialogDrag: true, //表单窗口拖拽
        searchMenuSpan: 6,
        border: true,
        index: true,
        addBtn: true,
        viewBtn: true,
        delBtn: true,
        editBtn: true,
        columnBtn: false,
        selection: false,
        dialogClickModal: false,
        dialogWidth: "60%",
        column: [
          {
            label: "通道名称",
            prop: "channelId",
            type: "select",
            dataType: "String",
            span: 12,
            dicUrl: "/api/iot-sim/channel/fillData",
            search: true,
            labelWidth: 130,
            props: {
              label: "name",
              value: "id",
            },
            rules: [
              {
                required: true,
                message: "请选择通道名称",
                trigger: "change",
              },
            ],
          },
          {
            label: "套餐组名称",
            prop: "dataPlanGroupId",
            span: 12,
            type: "select",
            searchLabelWidth: 120,
            search: true,
            labelWidth: 130,
            dicUrl: "/api/iot-sim/dataplangroup/fillData?isSource=" + 1,
            props: {
              label: "name",
              value: "id",
            },
            dataType: "String",
            rules: [
              {
                required: true,
                message: "请选择套餐组名称",
                trigger: "change",
              },
            ],
          },
          {
            label: "元套餐名称",
            prop: "name",
            span: 12,
            labelWidth: 130,
            searchLabelWidth: 100,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入元套餐名称",
                trigger: "blur",
              },
            ],
          },

          {
            label: "套餐类型",
            prop: "type",
            span: 12,
            type: "select",
            searchLabelWidth: 100,
            search: true,
            labelWidth: 130,
            dicUrl: "/api/iot-system/dict/dictionary?code=dataPlanType",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            rules: [
              {
                required: true,
                message: "请选择套餐类型",
                trigger: "change",
              },
            ],
          },
          {
            label: "生效类型",
            prop: "activateType",
            span: 12,
            type: "select",
            searchLabelWidth: 100,
            search: true,
            labelWidth: 130,
            dicUrl: "/api/iot-system/dict/dictionary?code=activateType",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            rules: [
              {
                required: true,
                message: "请选择生效类型",
                trigger: "change",
              },
            ],
          },
          {
            label: "套餐使用类型",
            prop: "usageType",
            span: 12,
            type: "select",
            searchLabelWidth: 100,
            search: true,
            labelWidth: 130,
            dicUrl: "/api/iot-system/dict/dictionary?code=usageType",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            rules: [
              {
                required: true,
                message: "请选择套餐使用类型",
                trigger: "change",
              },
            ],
          },
          {
            label: "套餐总量(M)",
            prop: "total",
            span: 12,
            type: "number",
            labelWidth: 130,
            searchLabelWidth: 100,
            search: false,
            rules: [
              { required: true, message: "请输入套餐总量", trigger: "blur" },
              {
                pattern: /^\d+(\.\d+)?$/,
                message: "请输入正确的套餐总量",
                trigger: "blur",
              },
            ],
          },
          {
            label: "成本价",
            prop: "costPrice",
            span: 12,
            type: "number",
            labelWidth: 130,
            searchLabelWidth: 100,
            search: false,
            hide: true,
            rules: [
              { required: true, message: "请输入成本价", trigger: "blur" },
              {
                pattern: /^\d+(\.\d+)?$/,
                message: "请输入正确的成本价",
                trigger: "blur",
              },
            ],
          },
          {
            label: "代理价",
            prop: "agentPrice",
            span: 12,
            type: "number",
            labelWidth: 130,
            searchLabelWidth: 100,
            search: false,
            hide: true,
            rules: [
              { required: true, message: "请输入代理价", trigger: "blur" },
              {
                pattern: /^\d+(\.\d+)?$/,
                message: "请输入正确的代理价",
                trigger: "blur",
              },
            ],
          },
          {
            label: "停机多少天后注销账户",
            prop: "stopClose",
            span: 12,
            type: "number",
            labelWidth: 170,
            searchLabelWidth: 100,
            search: false,
            hide: true,
            rules: [
              {
                required: true,
                message: "请输入停机多少天后注销账户",
                trigger: "blur",
              },
              {
                pattern: /^\d+?$/,
                message: "请输入正确的天数",
                trigger: "blur",
              },
            ],
          },
          {
            label: "元套餐内容",
            prop: "content",
            span: 24,
            type: "textarea",
            labelWidth: 130,
            searchLabelWidth: 100,
            hide: true,
            search: false,
            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);
    },
    //导出
    handleExport() {
      if (process.env.NODE_ENV === "production") {
        window.open(
          `http://223.99.228.240:18081/bd-core/tenant/dailybill/export-excel?${
            this.website.tokenHeader
          }=${getToken()}`
        );
      } else {
        window.open(
          `/api/iot-sim/simcard/export-simCard?${
            this.website.tokenHeader
          }=${getToken()}`
        );
      }
    },
    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>
