<template>
  <div class="app-container">
    <SetShop ref="setShop" />
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入店铺名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="城市">
        <el-select v-model="queryParams.province" placeholder="请选择城市" clearable>
          <el-option
            v-for="item in provinceList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable @keyup.enter.native="handleQuery">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      </el-form-item>
      <el-form-item label="机器编码" prop="machineCode ">
        <el-input v-model="queryParams.machineCode" placeholder="请输入店铺地址" clearable @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="openSet()"
          v-hasPermi="['system:shop:add']">新增</el-button>
      </el-col>
      <!--<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>
    <el-table v-loading="loading" :data="shopList">      
      <el-table-column label="店铺名称" align="center" prop="name">
        <template slot-scope="scope">
          <a @click="openSet(scope.row.id)" style="color: blue">{{ scope.row.name }}</a>
        </template>
      </el-table-column>
      <el-table-column label="默认店铺" align="center" prop="isDefault">
        <template slot-scope="scope">
          <span v-if="scope.row.isDefault == 1">是</span>
          <span v-if="scope.row.isDefault == 0">否</span>
        </template>
      </el-table-column>
      <el-table-column label="城市" align="center">
        <template slot-scope="scope">
          {{getProvince(scope.row.province)}}
        </template>
      </el-table-column>
      <el-table-column label="设备数量" align="center" prop="machineCount" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="总销售金额" align="center" prop="salesAmount" />
      <el-table-column label="总销量" align="center" prop="salesVolume" />
      <el-table-column label="创建时间" align="center" prop="createdAt" />
      <el-table-column label="营业时间" align="center" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1">正常营业</span>
          <span v-if="scope.row.state == 2">暂停营业</span>
          <span v-if="scope.row.state == 3">已删除</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="scope">
          <el-button v-show="scope.row.state !='3'" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:shop:remove']">删除</el-button>
          <el-button size="mini" type="text" v-if="authBtn(['1'], scope.row.state)"
            @click="setShopState(scope.row, 2, 'setState')">
            暂停营业</el-button>
          <el-button size="mini" type="text" v-if="authBtn(['2', '3'], scope.row.state)"
            @click="setShopState(scope.row, 1, 'setState')">开始营业</el-button>
          <el-button v-show="scope.row.isDefault=='0'" size="mini" type="text" @click="setShopState(scope.row)">设为默认</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 {
  listShop,
  delShop,
  updateShop
} from "@/api/system/shop";
import SetShop from './setShop';
import piovince from '@/utils/piovince'
export default {
  components: { SetShop },
  name: "Shop",
  data() {
    return {
      piovince,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺表格数据
      shopList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,      
        province: null,       
        state: null,
        machineCode: null        
      },
      provinceList:[{
        value: '',
        label: '全部'
      }],
      options: [{
          value: '1',
          label: '已创建'
        }, {
          value: '2',
          label: '已完成编程'
        }, {
          value: '3',
          label: '已上架'
        }, {
          value: '4',
          label: '已下架'
        }, {
          value: '5',
          label: '已删除'
      }],
    };
  },
  created() {
    this.getList();
    this.getProvinceList();
  },
  methods: {
    getProvinceList(){
      let list=this.piovince;
      for(let i=0;i<list.length;i++){
        this.provinceList.push({value:list[i].value,label:list[i].name})       
      }
    },
    getProvince(province){
      let list=this.piovince;
      for(let i=0;i<list.length;i++){
        if(list[i].value == province){
          return list[i].name;
        }        
      }
    },
    handleUpdate() { },
    /** 查询店铺列表 */
    getList() {
      this.loading = true;
      listShop(this.queryParams).then((response) => {
        this.shopList = response.rows;
        this.total = response.total;
        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.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除店铺名称为"' + row.name + '"的数据项？')
        .then(function () {
          return delShop(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/shop/export",
        {
          ...this.queryParams,
        },
        `shop_${new Date().getTime()}.xlsx`
      );
    },
    openSet(id) {
      this.$refs.setShop.openModal(id)
    },
    setShopState(data, state, type) {
      let params = { ...data }
      if (type == 'setState') {
        params.state = state;
        params.isDelete = '0';
      } else {
        params.isDefault = 1
      }
      updateShop(params).then(res => {
        this.getList()
      })

    }
  },
};
</script>
 