<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      服务管理
    </h2>
    <div class="m-core">
      <save-form
      :inline="true"
      class="searchForm"
      ref="searchForm"
      @submit="search"
      @reset="reset"
      :saveKey="'serviceManager'"
      :model.sync="searchForm"
    >
      <el-form-item :rules="[]">
        <el-input v-model="searchForm.search" placeholder="服务名称"></el-input>
      </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="'platformServe/add'" @click="openService('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="serveName">
        </el-table-column>
        <el-table-column align="center" label="产品服务" prop="productName">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="openService('update', scope.row)"
              v-permission="'platformServe/update'"
              type="text"
              >修改</el-button
            >
            <el-button
              @click="delService(scope.row)"
              v-permission="'platformServe/delete'"
              type="text"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
       :saveKey="'serviceManager'"
        sync
        :total.sync="pageInfo.total"
        :page.sync="pageInfo.page"
        :limit.sync="pageInfo.limit"
        :page-sizes="pageInfo.page_size"
        @pagination="getList"
      />
    </div>
    </div>
  </div>
</template>
<script>
import pagination from "@/components/Pagination/index";
import { platformServeQueryPage,platformServeDelete } from "./api";
const initSearch = {
  search: null
};
export default {
  components: { pagination },
  data() {
    return {
      tableList: [],
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      searchForm: { ...initSearch },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      try {
        const res = await platformServeQueryPage({
          ...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();
    },
    openService(type, row) {
      const tagTitle = type == 'update' ? '修改' : '创建'
      this.$router.push({
        path: '/platformManager/serviceOperation',
        query: {
          type: type,
          id: row?.id || null
        }
      }, (route) => {
        route.meta.title = tagTitle + '服务'
      })
    },
    async delService(row) {
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then( async() => {
        try{
         await platformServeDelete({ id:row.id})
         this.$message.success('删除成功')
         this.getList()
        }catch{}
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.courseMask .el-dialog__body {
  padding-bottom: 0;
}
</style>
