<template>
  <vxe-modal v-model="mShow" width="70%" height="80%" :title="'设备管理 - ' + shopInfo.storeName" @hide="hide">
    <template #default>
      <div class="search">
        <el-input v-model="searchText" class="search-text" placeholder="请输入关键字" clearable />
        <div>
          <el-button type="primary" icon="Search" @click="getList">搜索</el-button>
          <el-button icon="Refresh" @click="resert">重置</el-button>
        </div>
      </div>
      <vxe-table show-overflow size="mini" :sync-resize="mShow" :data="value" v-loading="load">
        <vxe-column type="seq" width="60" title="序号"></vxe-column>
        <vxe-column field="equipmentName" title="设备"></vxe-column>
        <vxe-column field="addrCodeInfo" title="地区"></vxe-column>
        <vxe-column field="addrInfo" title="详细地址"></vxe-column>
        <vxe-column title="操作" fixed width="100">
          <template #default="{row}">
            <el-button type="primary" link @click="addHandle(row)">关联店铺</el-button>
          </template>
        </vxe-column>
      </vxe-table>
    </template>
  </vxe-modal>
</template>

<script setup>
  import {shopNoEquip,shopAddEquip} from '@/api/base/shopManage'
  import { computed,getCurrentInstance,ref,markRaw,shallowRef,defineEmits } from "vue";
  const { proxy } = getCurrentInstance();
  const emits = defineEmits(['addEquipClose','successHandle']);

  const mShow = ref(false)
  const shopInfo = ref({})
  const value = ref([])
  const load = ref(false)
  const searchText = ref('')
  const setShow = v => {
    mShow.value = v
  }
  //设置店铺信息
  const setValue = data => {
    shopInfo.value = data;
    getList()
  }
  //关闭弹窗
  const hide = () => {
    emits('addEquipClose',shopInfo.value)
  }
  //重置
  const resert = () => {
    searchText.value = '';
    getList()
  }

  //添加设备
  const addHandle = row => {
    load.value = true
    shopAddEquip({
      storeId:shopInfo.value.storeId,
      equipmentId: row.equipmentId
    }).then(() => {
      load.value = true
      proxy.$message({
        message:'操作成功',
        type:'success'
      });
      if(value.value.length > 1) {
        getList()
      }else{
        mShow.value = false
        emits('successHandle');
      }
    }).catch(() => {
      load.value = false
    })
  }

  //获取未绑定设备列表
  const getList = () => {
    load.value = true
    value.value = []
    shopNoEquip({searchText:searchText.value}).then(res =>{
      load.value = false
      value.value = res.data.records
    }).catch(() => {
      load.value = false
    })
  }

  defineExpose({
    setShow,setValue
  })
</script>

<style lang="scss" scoped>
.search{
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 8px;
  .search-text{
    width: 200px;
  }
}
</style>