<template>
  <vxe-modal v-model="mShow" width="70%" height="90%" :title="'设备管理 - ' + shopInfo.storeName">
    <template #default>
      <div class="title">未关联设备</div>
      <div class="search">
        <el-input v-model="searchText" class="search-text" placeholder="请输入关键字" clearable />
        <div>
          <el-button type="primary" icon="Search" @click="getNoEquipList">搜索</el-button>
          <el-button icon="Refresh" @click="resert">重置</el-button>
        </div>
      </div>
      <vxe-table show-overflow size="mini" :sync-resize="mShow" :data="noEquipvalue" v-loading="load2">
        <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>

      <!-- <div class="btn">
        <el-button @click="addEquip">添加设备</el-button>
      </div> -->
      <div class="title gl">已有设备</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="moveHandle(row)">移除</el-button>
          </template>
        </vxe-column>
      </vxe-table>
    </template>
  </vxe-modal>
</template>

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

  const noEquipvalue = ref([]);
  const mShow = ref(false);
  const shopInfo = ref({});
  const value = ref([]);
  const load = ref(false);
  const load2 = ref(false)
  const searchText = ref('');
  const setShow = v => {
    mShow.value = v
  }

  //重置
  const resert = () => {
    searchText.value = '';
    getNoEquipList()
  }

  const setValue = data => {
    shopInfo.value = data;
    getList();
    getNoEquipList()
  }

  // const addEquip = () => {
  //   mShow.value = false;
  //   emit('showAddEquip',shopInfo.value)
  // }

  const moveHandle = row => {
    load.value = true
    shopRemoveEquip({equipmentIds:[row.equipmentId]}).then(() =>{
      load.value = false;
      proxy.$message({
        message:'操作成功',
        type:'success'
      })
      emit('successHandle')
      getNoEquipList()
      getList()
    }).catch(() => {
      load.value = false
    })
  }

   //添加设备
   const addHandle = row => {
    load2.value = true
    shopAddEquip({
      storeId:shopInfo.value.storeId,
      equipmentId: row.equipmentId
    }).then(() => {
      load2.value = true
      proxy.$message({
        message:'操作成功',
        type:'success'
      });
      getNoEquipList()
      getList()
      emit('successHandle');
    }).catch(() => {
      load2.value = false
    })
  }

  const getList = () => {
    load.value = true
    shopEquip({storeId:shopInfo.value.storeId}).then(res =>{
      load.value = false
      value.value = res.data.records
    }).catch(() => {
      load.value = false
    })
  }

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

  defineExpose({
    setShow,setValue
  })

</script>

<style lang="scss" scoped>
.title{
  font-size: 14px;
  font-weight: 700;
  padding-bottom: 8px;
}
.gl{
  padding-top: 24px;
}
.btn{
  width: 100%;
  padding-bottom: 8px;
  display: flex;
  justify-content: flex-end;
}
.search{
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 8px;
  .search-text{
    width: 200px;
  }
}
</style>