<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="模板名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入模板名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['store:shippingTemplates:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['store:shippingTemplates:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['store:shippingTemplates:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table border v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="模板ID" align="center" prop="id" width="80" />
      <el-table-column label="模板名称" align="center" prop="name" width="180" show-overflow-tooltip />
      <el-table-column label="计费方式" align="center" prop="type" width="90">
        <template slot-scope="scope"><dict-tag :options="typeOptions" :value="scope.row.type" /></template>
      </el-table-column>
      <el-table-column label="包邮开关" align="center" prop="appoint" width="90">
        <template slot-scope="scope"><dict-tag :options="appointOptions" :value="scope.row.appoint" /></template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" width="70" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)" v-hasPermi="['store:shippingTemplates:query']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['store:shippingTemplates:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['store:shippingTemplates:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { getRequest, delRequest } from "@/api/common";

export default {
  name: "ShippingTemplates",
  data() {
    return {
      loading: true, exportLoading: false, ids: [], multiple: true, showSearch: true, total: 0, list: [],
      typeOptions: [], appointOptions: [],
      queryParams: { pageNum: 1, pageSize: 10, name: undefined }
    };
  },
  created() {
    this.getDicts("shipping_type").then(res => { this.typeOptions = res.data; });
    this.getDicts("sys_yes_no").then(res => { this.appointOptions = res.data; });
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getRequest("/store/store/shippingTemplates/list", this.queryParams).then(res => {
        this.list = res.rows || []; this.total = res.total || 0; this.loading = false;
      }).catch(() => { this.list = []; this.total = 0; this.loading = false; });
    },
    handleQuery() { this.queryParams.pageNum = 1; this.getList(); },
    resetQuery() { this.resetForm("queryForm"); this.handleQuery(); },
    handleSelectionChange(selection) { this.ids = selection.map(item => item.id); this.multiple = !selection.length; },
    handleAdd() { this.$router.push({ path: "/shippingTemplatesEdit" }); },
    handleUpdate(row) { this.$router.push({ path: "/shippingTemplatesEdit", query: { id: row.id } }); },
    handleDetail(row) { this.$router.push({ path: "/shippingTemplatesDetail", query: { id: row.id } }); },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('确认删除模板"' + (row.name || ids) + '"吗？').then(function() {
        return delRequest("/store/store/shippingTemplates/" + ids);
      }).then(() => { this.getList(); this.$modal.msgSuccess("删除成功"); }).catch(() => {});
    },
    handleExport() {
      this.$modal.confirm("确认导出所有模板数据吗？").then(() => {
        this.exportLoading = true;
        getRequest("/store/store/shippingTemplates/export", this.queryParams).then(res => { this.download(res.msg); this.exportLoading = false; });
      }).catch(() => {});
    }
  }
};
</script>
