<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :topList="topList"
      :moren="moren"
    ></topBar>
    <div class="thetopbox">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item class="dizhi" label="门店名称" prop="">
              <el-input v-model="queryParams.storeName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="dizhi" :label="'详细地址'" prop="">
              <el-cascader
                style="width: 100%"
                clearable
                v-model="queryParams.shoppingAddress"
                :options="areaData"
                :props="regionParams"
                @change="handleChange1"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="" label-width="40px">
              <el-input clearable v-model="queryParams.storeAddress"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div class="searchbox">
              <el-button class="my_search" @click="getSearch">
                {{ '搜索' }}</el-button
              >
              <el-button class="my_reset" @click="reChongzhi">
                {{ '重置' }}</el-button
              >
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="mini" :disabled="canbind" @click="bindDevices" style="background: #009b58" class="addBtn"
            >绑定</el-button
          >
          <el-button
            size="mini"
            style="background: #ff6723; color: #fff; border: none"
            class="addBtn"
            @click="handleImport"
            >导入</el-button
          >
          <el-button
            size="mini"
            style="background: #ffad41; color: #fff; border: none"
            class="addBtn"
            @click="handleExport"
            >{{'导出'}}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          ref="multipleTable"
          :data="tableData"
          v-loading="loading"
          height="720px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>

          <el-table-column align="center" prop="orderCode" label="门店图片">
            <template slot-scope="scope">
              <img
                style="width: 44px; height: 44px; border-radius: 8px"
                :src="scope.row.path"
                alt=""
              />
            </template>
          </el-table-column>
          <el-table-column align="center" prop="storeName" label="门店名称">
          </el-table-column>
          <el-table-column align="center" prop="provinceName" label="省">
          </el-table-column>
          <el-table-column align="center" prop="cityName" label="市">
          </el-table-column>
          <el-table-column align="center" prop="countyName" label="区/县">
          </el-table-column>
          <el-table-column align="center" prop="storeAddress" :label="'详细地址'">
          </el-table-column>

          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                class="button-s"
                type="text"
                style="color: #ffad41"
                size="small"
                @click="goDetail(scope.row)"
              >
                查看详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getSearch"
    />
    <el-dialog
      :title="upload.title"
      :close-on-click-modal="false"
      :visible.sync="upload.open"
      width="600px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :on-exceed="handleExceed"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          {{ '将文件拖到此处，或' }}
          <em>{{ '点击上传' }}</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-link type="info" style="font-size: 12px" @click="importTemplate">
            {{ '下载模板' }}</el-link
          >
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          {{ '提示：仅允许导入“xls”或“xlsx”格式文件！' }}
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">{{
          '确认'
        }}</el-button>
        <el-button @click="upload.open = false">{{
          '取消'
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

 <script>
import topBar from "@/components/topBar";
import * as api from "@/api/site.js";
import * as equ from "@/api/equipment.js";
import { getToken } from "@/utils/auth";
export default {
  name: "Devices",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "devices",
      topList: [
        {
          name: "设备列表",
          path: "devices",
        },
        {
          name: "购买次数",
          path: "purchases",
        },
      ],
      tableData: [],
      loading:false,
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: ["", "", ""],
      },
      total: 0,
      multipleSelection: [],
      areaData: [],
      regionParams: {
        label: "name",
        value: "id",
        children: "children",
        expandTrigger: "hover",
      },
      canbind:true,
      upload: {
        // 是否显示弹出层（物流导入）
        open: false,
        // 弹出层标题（物流导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的物流数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API +
          "/system/manage/bd-machine-maker-space/importData",
      },
    };
  },
  created() {
    this.getSearch();
    this.getData();
  },
  methods: {
    bindDevices(){
        this.$router.push({
            path:"bindDevices",
            query:{ data: JSON.stringify(this.multipleSelection) }
        })
    },
    goDetail(row){
        this.$router.push({
            path:"devicesDetail",
            query:{
                pkMakerSpace:row.pkMakerSpace,
                barCode:row.barCode
            }
        })
    },
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/system/manage/bd-machine-maker-space/export",
          Object.assign({}, this.queryParams),
          `设备列表${new Date().getTime()}.xlsx`
        );
      });
    },
    //下载模板
    importTemplate() {
      this.download(
        "/system/manage/bd-machine-maker-space/download-template",
        {},
        `模板${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "设备列表导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      var fileName = response.fileName;
      if (fileName != undefined && fileName != "") {
      }
      if (response.code == 200) {
        this.tableData = response.data;
      }

      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, '导入结果', {
        dangerouslyUseHTMLString: true,
      });
      // this.getList();
    },
    // 上传文件数量超出后的动作
    handleExceed() {
      this.upload.isUploading = false;
      this.msgInfo('仅可上传一个文件哦');
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    handleChange1(val) {
      this.queryParams.storeProvince = val[0];
      this.queryParams.storeCity = val[1];
      this.queryParams.storeCounty = val[2] || "";
    },

    getData() {
      api.areaList().then((res) => {
        this.areaData = res.data;
      });
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
      if(val.length>0){
        this.canbind = false
      }else{
        this.canbind = true
      }
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        shoppingAddress: ["", "", ""],
      };
      this.getSearch();
    },
    getSearch() {
      this.loading = true
      equ.getMakerSpaceList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.loading = false
      });
    },
  },
};
</script>

      <style lang="scss" scoped>
::v-deep .el-range-editor.el-input__inner {
  width: 100%;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 15px 20px 15px 0;
    background: #ffffff;
    border-radius: 8px;
    .searchbox {
      display: flex;
      align-items: center;
      margin-left: 40px;
    }
    .searchtitle {
      margin-right: 10px;
    }
    .searchbtn {
      background: #08143f;
      color: #ffffff;
    }
  }
  .main {
    //margin-top: 20px;
    background: #f9f9f9;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);

    .maintop {
      display: flex;
      // padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          color: #ffffff;
          background: #48b2fd;
        }
        .thebtn2 {
          color: #ffffff;
          background: #ffad41;
        }
      }
    }
  }
}
</style>
