<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <div class="search-area">
      <el-form :model="queryParams" ref="queryForm" :inline="true" size="small">
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="queryParams.supplierName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="联系人手机号" prop="contactPhone">
          <el-input v-model="queryParams.contactPhone" maxlength="11" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddSupplier">新增供应商</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格区域 -->
    <div class="table-container">
      <el-table v-loading="loading" :data="list">
        <el-table-column type="index" width="55" label="序号" align="center">
          <template slot-scope="scope">
            <div>
              {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="supplierName" label="供应商名称" align="center"></el-table-column>
        <el-table-column prop="region" label="所在区域" align="center">
          <template slot-scope="{row}">
            <div>
              {{row.provinceName+row.cityName+row.districtName}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="contactName" label="联系人姓名" align="center"></el-table-column>
        <el-table-column prop="contactPhone" label="联系人电话" align="center"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleEdit(scope.row,'show')">查看</el-button>
            <el-button type="text" size="small" @click="handleEdit(scope.row,'edit')">编辑</el-button>
            <el-button type="text" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页区域 -->
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { supplierPage, supplierDel } from "@/api/pointsMall";
import Pagination from "@/components/Pagination";

export default {
  name: "SupplierList",
  components: {
    Pagination,
  },
  data () {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierName: undefined,
        contactPhone: undefined
      },
    };
  },
  computed: {
    totalPages () {
      return Math.ceil(this.total / this.pageSize);
    },
  },
  created () {
    this.getList();
  },
  methods: {
    // 搜索
    getList () {
      this.loading = true;
      supplierPage(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 修改新增供应商方法
    handleAddSupplier () {
      this.$router.push({
        path: "/pointsMall/supplierList/details"
      });
    },
    // 编辑供应商
    handleEdit (row, type) {
      this.$router.push({
        path: "/pointsMall/supplierList/details",
        query: {
          type: type,
          supplierId: row.supplierId
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.supplierId || this.ids;
      this.$modal.confirm('是否确认删除供应商名称为"' + row.supplierName + '"的数据项？').then(() => {
        this.loading = true;
        return supplierDel(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
