<template>
  <div class="app-container">
    <SetShop ref="setShop" @getList="getList"/>
    <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-cascader :options="provinceList" :show-all-levels="false" v-model="province" clearable></el-cascader>
      </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" v-hasPermi="['system:shop:query']">搜索</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.city)}}
        </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">
        <template slot-scope="scope">
          <div>
            <span >{{scope.row.startTime}}</span>
          </div>
          <div>
            <span >~</span>
          </div>
          <div>
            <span >{{scope.row.endTime}}</span>
          </div>        
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 0">测试中</span>
          <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')" v-hasPermi="['system:shop:suspended']">
            暂停营业</el-button>
          <el-button size="mini" type="text" v-if="authBtn([2], scope.row.state)"
            @click="setShopState(scope.row, 1, 'setState')" v-hasPermi="['system:shop:start']">开始营业</el-button>
          <el-button size="mini" type="text" v-if="authBtn([3], scope.row.state)"
            @click="setShopState(scope.row, 1, 'setState')" v-hasPermi="['system:shop:restart']">恢复营业</el-button>
          <el-button v-show="scope.row.isDefault==0" size="mini" type="text" @click="setShopState(scope.row)" v-hasPermi="['system:shop:default']">设为默认</el-button>
          <el-button size="mini" type="text" v-if="authBtn([0], scope.row.state)"
            @click="testFinish(scope.row)" v-hasPermi="['system:shop:testFinish']">测试完成</el-button>
          <el-button size="mini" type="text" v-if="authBtn([2], scope.row.state)"
            @click="testStart(scope.row)" v-hasPermi="['system:shop:testStart']">开始测试</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,getShopCity,testFinish,testStart} 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,      
        city: null,       
        state: null,
        machineCode: null        
      },
      province:[],
      provinceList:[],
      options: [{
          value: '',
          label: '全部'
        },{
          value: '1',
          label: '正常营业'
        }, {
          value: '2',
          label: '暂停营业'
        }, {
          value: '3',
          label: '已删除'
        }]
    };
  },
  created() {
    this.getList();
    this.getProvinceList();
  },
  methods: {
    getProvinceList(){      
      let list=[];
      this.provinceList=[];
      getShopCity().then((response) => {
        if(response.code==200){
          list=response.data;
          let piovinceList=this.piovince;
          for(let i=0;i<piovinceList.length;i++){
            if(piovinceList[i].children.length>0){            
              let childrenList=piovinceList[i].children;
              let obj={};
              let tag=false;
              obj.value=piovinceList[i].value;
              obj.label=piovinceList[i].label;             
              let children=[];  
              for(let j=0;j<childrenList.length;j++){
                if(list.indexOf(childrenList[j].value)>-1){                                  
                  children.push({"value":childrenList[j].value,"label":childrenList[j].name});   
                    tag=true;          
                }
              }
              obj.children=children; 
              if(tag){
                this.provinceList.push(obj);    
              }                 
            }                 
          } 
        }
      });
    },
    getProvince(province){
      let list=this.piovince;
      for(let i=0;i<list.length;i++){
        if(list[i].children.length>0){
          let childrenList=list[i].children;
          for(let j=0;j<childrenList.length;j++){           
            if(childrenList[j].value == province){
              return childrenList[j].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.queryParams.city=this.province[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 => {
        if(res.code="200"){
          this.getList();
        }
        
      })

    },
    //测试完成
    testFinish(row){
      testFinish(row.id).then(res => {
        if(res.code="200"){
          this.getList();
        }        
      })
    },
    //测试开始
    testStart(row){
      testStart(row.id).then(res => {
        if(res.code="200"){
          this.getList();
        }        
      })
    }
  },
};
</script>
 