<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.shopName" clearable size="mini" class="filter-item"
        style="width: 200px;"
        placeholder="请输入店铺名称"
      />
      <el-input
        v-model="listQuery.shopCode" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请输入店铺编码"
      />
      <el-input
        v-model="listQuery.keyword" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请输入手机号"
      />
      <el-input
        v-model="listQuery.chargePersonName" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请输入负责人"
      />
      <el-select
        v-model="listQuery.contractState" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请选择合同状态"
      >
        <el-option label="有效" :value="1" />
        <el-option label="无效" :value="0" />
      </el-select>
      <el-button
        size="mini" class="filter-item" type="primary" icon="el-icon-search"
        style="margin-left:10px;"
        @click="handleSearch"
      >
        查找
      </el-button>
      <br />
      <el-button
        size="mini" type="primary" icon="el-icon-plus"
        @click="$refs.EditModal && $refs.EditModal.handleOpen({ shopId: '' })"
      >
        新建商家
      </el-button>
    </div>

    <!-- 查询结果 -->
    <div v-tableHeight>
      <el-table
        v-loading="listLoading" height="100%" element-loading-text="正在查询中。。。" :data="list"
        v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
      >
        <el-table-column label="店铺ID" align="center">
          <template slot-scope="scope">{{ scope.row.shopId }}</template>
        </el-table-column>
        <el-table-column label="店铺名称" width="200" align="center">
          <template slot-scope="scope">{{ scope.row.shopName }}</template>
        </el-table-column>
        <el-table-column width="120" prop="shopCode" label="店铺编码" />
        <el-table-column width="120" label="是否支持消费金">
          <template slot-scope="scope">
            <span v-if="scope.row.isBeeCoin === 1">支持</span>
            <span v-else-if="scope.row.isBeeCoin === 2">不支持</span>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" min-width="120" label="商家消费金提现比例" prop="beecoinRatio">
          <template slot-scope="{ row }">
          <span v-if="row.beeCoinRatio">{{ row.beeCoinRatio }}：1</span>
          <span v-else>--</span>
          </template>
          </el-table-column> -->
        <el-table-column prop="chargePersonName" label="负责人" />
        <el-table-column width="120" prop="chargePersonPhone" label="联系电话" />
        <el-table-column label="合同状态">
          <template slot-scope="scope">
            <span v-if="scope.row.contractState === 0">无效</span>
            <span v-if="scope.row.contractState === 1">有效</span>
          </template>
        </el-table-column>
        <el-table-column width="150" prop="createTime" label="创建时间" />
        <el-table-column align="center" label="操作" width="280" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{ row }">
            <el-button type="warning" size="mini" @click="handleDetail(row)">
              详情
            </el-button>
            <el-button size="mini" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button v-if="row.state == 1" type="text" @click="handleDisabled(row)">
              禁用
            </el-button>
            <el-button v-else type="text" @click="handleEnable(row)">
              启用
            </el-button>
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div>
      <el-pagination
        :current-page="listQuery.page" :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="(val) => ((listQuery.pageSize = val) && getList())"
        @current-change="(val) => ((listQuery.page = val) && getList())"
      />
    </div>

    <!-- 新增编辑 -->
    <EditModal ref="EditModal" @success="getList" />
    <!-- 查看详情 -->
    <DetailModal ref="DetailModal" />
  </div>
</template>

<script>
import EditModal from './components/EditModal'
import DetailModal from './components/DetailModal'
import {
  businessListGetAll,
  businessListStart,
  delBusinessById
} from '@/api/business'

export default {
  name: 'BusinessList',
  components: {
    EditModal,
    DetailModal
  },
  data() {
    // 这里存放数据
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        shopName: '', // 店铺名称
        shopCode: '', // 店铺编码
        chargePersonName: '', // 店铺负责人
        contractState: '', // 合同状态 1-有效 0-无效
        shopType: 2,
        keyword:'' // 手机号搜索
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await businessListGetAll(this.listQuery)
        this.list = res.data.list
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDetail(row) {
      this.$refs.DetailModal && this.$refs.DetailModal.handleOpen(row)
    },
    handleEdit(row) {
      this.$refs.EditModal && this.$refs.EditModal.handleOpen(row)
    },
    handleDelete(row) {
      this.$confirm('确定删除此项？')
        .then(async () => {
          await delBusinessById({ shopId: row.shopId })
          this.$message({ message: '删除成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    },
    handleDisabled(row) {
      this.$confirm('确定停用此项？')
        .then(async () => {
          await businessListStart({ shopName: row.shopName, state: 0, shopId: row.shopId })
          this.$message({ message: '停用成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    },
    handleEnable(row) {
      this.$confirm('确定启用此项？')
        .then(async () => {
          await businessListStart({ shopName: row.shopName, state: 1, shopId: row.shopId })
          this.$message({ message: '启用成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
	padding: 20px;
	display: flex;
	flex-direction: column;

	.filter-container {
		.filter-item {
			display: inline-block;
			vertical-align: middle;
			margin-bottom: 10px;
		}
	}

	.small-padding {
		.cell {
			padding-left: 5px;
			padding-right: 5px;
		}
	}

	.fixed-width {
		.el-button--mini {
			padding: 7px 10px;
		}
	}
}
</style>
