<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      学练宝流量包
    </h2>
    <save-form
      :inline="true"
      class="searchForm"
      ref="searchForm"
      @submit="search"
      @reset="reset"
      :saveKey="'platformProductManager'"
      :model.sync="searchForm"
    >
      <el-form-item>
        <el-select
          v-model.trim="searchForm.minuteUp"
          clearable
          filterable
          placeholder="请选择分钟数"
        >
          <el-option
            v-for="min in minuteList"
            :label="min"
            :value="min"
            :key="min"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button native-type="submit" type="danger">搜索</el-button>
        <el-button native-type="reset" type="primary">重置</el-button>
      </el-form-item>
    </save-form>
    <el-button
      style="margin-bottom: 20px;"
      type="primary"
      v-permission="'cloudRoomFlowConfig/add'"
      @click="openCloudPackage('create')"
      icon="el-icon-plus"
      >新增流量包</el-button
    >
    <!-- 列表 -->
    <div class="tableWrap">
      <el-table
        :data="tableList"
        :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
      >
        <el-table-column align="center" prop="id" label="流量包编号">
        </el-table-column>
        <el-table-column align="center" label="分钟数" prop="minuteUpLimit">
        </el-table-column>
        <el-table-column align="center" label="原价（元）" prop="price">
        </el-table-column>
        <el-table-column align="center" label="描述" prop="remark">
          <template slot-scope="scope">
            <tooltip :content="scope.row.remark" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="resetCloudPackage(scope.row)"
              v-permission="'cloudRoomFlowConfig/update'"
              type="text"
              >修改</el-button
            >
            <el-button
              @click="delCloudPackage(scope.row)"
              v-permission="'cloudRoomFlowConfig/delete'"
              type="text"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :saveKey="'platformProductManager'"
        sync
        :total.sync="pageInfo.total"
        :page.sync="pageInfo.page"
        :limit.sync="pageInfo.limit"
        :page-sizes="pageInfo.page_size"
        @pagination="getList"
      />
    </div>

    <el-dialog
      :title="isAdd ? '新增流量包' : '修改流量包'"
      class="courseMask"
      width="500px"
      :visible.sync="courseVisible"
    >
      <cloudModel
        ref="cloudModel"
        :activeRow="activeRow"
        :pageType="pageType"
        v-if="courseVisible"
        @close="close"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="courseVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitInfo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import pagination from "@/components/Pagination/index";
import { queryPage, getMinuteUp, cloudDelete } from "./api";
import cloudModel from "./cloudModel";
const initSearch = {
  minuteUp: null
};
export default {
  components: { pagination, cloudModel },
  data() {
    return {
      tableList: [],
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
      searchForm: { ...initSearch },
      isAdd: true,
      courseVisible: false,
      activeRow: null,
      minuteList: []
    };
  },
  mounted() {
    this.__init();
    this.getList();
  },
  methods: {
    async __init() {
      try {
        const res = await getMinuteUp();
        this.minuteList = res.data || [];
      } catch (e) {}
    },
    async getList() {
      try {
        const res = await queryPage({
          ...this.searchForm,
          page: this.pageInfo.page,
          rows: this.pageInfo.limit
        });
        this.pageInfo.total = res.data.total;
        this.tableList = res.data.rows;
      } catch (e) {}
    },
    search() {
      this.pageInfo.page = 1;
      this.$refs.searchForm.save(this.searchForm);
      this.$refs.searchForm.save(this.pageInfo, "page");
      this.getList();
    },
    reset() {
      this.searchForm = { ...initSearch };
      this.search();
    },
    resetCloudPackage(row) {
      this.isAdd = false;
      this.activeRow = row;
      this.pageType = "update";
      this.courseVisible = true;
    },
    submitInfo() {
      const str = this.isAdd ? "create" : "update";
      this.$refs.cloudModel.submitInfo(str);
    },
    close() {
      this.courseVisible = false;
      this.getList();
    },
    openCloudPackage() {
      this.isAdd = true;
      this.activeRow = null;
      this.pageType = "create";
      this.courseVisible = true;
    },
    async delCloudPackage(row) {
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        try {
          const res = await cloudDelete({ id: row.id });
          this.$message.success("删除成功");
          this.getList();
        } catch {}
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.courseMask .el-dialog__body {
  padding-bottom: 0;
}
</style>
