<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="店铺名称" prop="storeName">
        <el-input v-model="queryParams.storeName" placeholder="请输入店铺名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="isAudit">
        <el-select v-model="queryParams.isAudit" placeholder="请选择" clearable size="small">
          <el-option v-for="dict in auditOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属公司" prop="companyId">
        <el-select v-model="queryParams.companyId" placeholder="请选择公司" clearable filterable size="small">
          <el-option v-for="item in companyOptions" :key="item.companyId" :label="item.companyName" :value="item.companyId" />
        </el-select>
      </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="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['store:store: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:store: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="storeId" width="80" />
      <el-table-column label="店铺名称" align="center" prop="storeName" width="160" show-overflow-tooltip />
      <el-table-column label="所属公司" align="center" prop="companyName" width="160" show-overflow-tooltip />
      <el-table-column label="登录帐号" align="center" prop="account" width="120" />
      <el-table-column label="店铺电话" align="center" prop="phone" width="120" />
      <el-table-column label="地址" align="center" prop="address" min-width="180" show-overflow-tooltip />
      <el-table-column label="商品数" align="center" prop="productCount" width="80" />
      <el-table-column label="销量" align="center" prop="salesCount" width="80" />
      <el-table-column label="余额" align="center" prop="balance" width="100">
        <template slot-scope="scope"><span>¥{{ scope.row.balance }}</span></template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="isAudit" width="90">
        <template slot-scope="scope"><dict-tag :options="auditOptions" :value="scope.row.isAudit" /></template>
      </el-table-column>
      <el-table-column label="店铺状态" align="center" prop="status" width="90">
        <template slot-scope="scope"><dict-tag :options="statusOptions" :value="scope.row.status" /></template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleDetail(scope.row)" v-hasPermi="['store:store:query']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['store:store: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";
import { getCompanyList } from "@/api/company/company";

export default {
  name: "Store",
  data() {
    return {
      loading: true,
      exportLoading: false,
      ids: [],
      multiple: true,
      showSearch: true,
      total: 0,
      list: [],
      auditOptions: [],
      statusOptions: [],
      companyOptions: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeName: undefined,
        isAudit: undefined,
        status: undefined,
        companyId: undefined
      }
    };
  },
  created() {
    this.getDicts("sys_audit_status").then(res => { this.auditOptions = res.data; });
    this.getDicts("sys_normal_disable").then(res => { this.statusOptions = res.data; });
    getCompanyList().then(res => { this.companyOptions = res.data || []; });
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      getRequest("/store/store/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.storeId);
      this.multiple = !selection.length;
    },
    handleDetail(row) {
      this.$router.push({ path: "/storeShopDetail", query: { storeId: row.storeId } });
    },
    handleDelete(row) {
      const ids = row.storeId || this.ids;
      this.$modal.confirm('确认删除店铺"' + (row.storeName || ids) + '"吗？').then(function() {
        return delRequest("/store/store/" + ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleExport() {
      this.$modal.confirm("确认导出所有店铺数据吗？").then(() => {
        this.exportLoading = true;
        getRequest("/store/store/export", this.queryParams).then(res => {
          this.download(res.msg);
          this.exportLoading = false;
        });
      }).catch(() => {});
    }
  }
};
</script>

