<template>
  <view class="page">
    <view class="mch-header">
      <JHeaderTitle title="刷脸设备" bgColor="#f2f2f2" />
      <JSearchInput @search="searchHandle" @resetSearch="reset" ref="search" place="搜索设备名称，设备号">
        <view class="header-assign" @tap="cancel">
          <image src="/static/iconImg/icon-file.svg" mode="scaleToFill" />
          划拨
        </view>
      </JSearchInput>
    </view>
    <view v-for="(v, index) in useDataResult.dataList" :key="v.deviceId">
      <JPreview
        :activeBox="vdata.activeBox"
        :isLast="index === useDataResult.dataList.length - 1"
        :img="imageList[v.state]"
        :title="v.deviceName"
        :qrcId="v.deviceId"
        :active="v.active"
        :spot="v.state == 0 ? '#b2b2b2' : '#7737FE'"
        :status="v.state == 1 ? '已启用' : '已禁用'"
        :disabled="!!v.bindState"
        @activeClick="handleActive(v)"
        @click="toDetail(v.deviceId)"
      >
        <template #titleTag>
          <JeepayTag :type="v.provider == 'wxpayQWPro' ? 'green-rgba' : 'blue'">{{ v.provider == 'wxpayQWPro' ? '青蛙刷脸Pro' : '蜻蜓F4' }}</JeepayTag>
        </template>
        <template #bottom>
          <view class="info-wrapper">
            <view class="info" v-if="!v.isSelf">
              <image src="/static/iconImg/icon-mini-agent.svg" mode="aspectFit" class="mch-img" />
              <view class="name">{{ v.agentName }}</view>
              <view class="number">{{ v.agentNo }}</view>
            </view>
            <view class="info" v-if="v.bindState">
              <image src="@/static/equipmentImg/mch-little.svg" mode="aspectFit" class="mch-img" />
              <view class="name">{{ v.mchName }}</view>
              <view class="number">{{ v.mchNo }}</view>
            </view>
          </view>
        </template>
      </JPreview>
    </view>
    <jeepayListNull :isShow="true" :list="useDataResult.dataList.length" />
    <view class="footer-button" v-show="vdata.activeBox">
      <view class="button-main">
        <button @tap="cancel">取消</button>
        <button class="confirm" @tap="openNext">下一步</button>
      </view>
    </view>
  </view>
  <JSinglePopup :list="selectedList" ref="refSingle" />
  <SelectedAgent ref="refSelected" @confirm="toAgent" />
</template>

<script setup>
import { reactive, ref } from 'vue'
import { $faceList, $unBind, $allotORtakeBack } from '@/http/apiManager'
import JHeaderTitle from '@/components/newComponents/JHeaderTitle/JHeaderTitle.vue'
import JSearchInput from '@/components/newComponents/JSearchInput/JSearchInput.vue'
import JPreview from '@/components/newComponents/JPreview/JPreview.vue'
import SelectedAgent from '../deviceManagement/components/SelectedAgent.vue'
import useGetList from '@/hooks/useList.js'
// 展示图片列表
const imageList = ['/pageWork/static/images/icon-face-0.svg', '/pageWork/static/images/icon-face-1.svg']
// 初始化列表数据
const { useDataResult, getList } = useGetList({
  requestFun: $faceList,
  pageSize: 15,
  params: { deviceType: 6 },
})
const refSingle = ref(null)
const refSelected = ref(null)
const vdata = reactive({
  activeBox: false,
})
const params = {
  pageSize: 10,
  pageNumber: 1,
  deviceType: 6,
}

const selectedList = [
  { label: '收回所有已选择设备', value: 'takeBack', confirmText: '确定要收回所有已选设备吗？', fun: takeBackDev },
  {
    label: '划拨已选设备至代理商',
    value: 'toAgent',
    fun: () => {
      refSelected.value.open()
    },
  },
]
// 跳转详情页
const toDetail = (deviceId) => uni.navigateTo({ url: `/pageWork/face/details?deviceId=${deviceId}` })
// 划拨功能区
const devNoList = []
const openNext = () => {
  if (devNoList.length == 0) return uni.showToast({ title: '请勾选要进行操作的设备', icon: 'none' })
  refSingle.value.open()
}
const cancel = () => {
  if (!vdata.activeBox) return (vdata.activeBox = true)
  useDataResult.dataList.forEach((v) => {
    v.active = false
  })
  devNoList.length = 0
  vdata.activeBox = false
}
// 勾选
const handleActive = (val) => {
  if (val.disabled) return
  if (val.active) {
    devNoList.splice(
      devNoList.findIndex((v) => v == val.deviceId),
      1
    )
    val.active = false
    return
  }
  devNoList.push(val.deviceId)
  val.active = true
}
// 收回划拨
function takeBackDev() {
  const data = {
    allotDeviceIds: devNoList.join(','),
    allotOrRecover: 'recover',
    allotType: 'select',
  }
  $allotORtakeBack(data).then((res) => {
    uni.showToast({
      title: '收回成功',
      icon: 'success|none',
    })
    devNoList.length = 0
    vdata.activeBox = false
    getList({
      pageNumber: 1,
    })
  })
}
// 划拨
function toAgent(e) {
  const data = {
    agentNo: e.text,
    allotDeviceIds: devNoList.join(','),
    allotOrRecover: 'allot',
    allotType: 'select',
  }
  $allotORtakeBack(data).then((res) => {
    uni.showToast({
      title: '划拨成功',
      icon: 'success|none',
    })
    devNoList.length = 0
    vdata.activeBox = false
    refSelected.value.close()
    getList({
      pageNumber: 1,
    })
  })
}
</script>

<style lang="scss" scoped>
.header-assign {
  display: flex;
  align-items: center;
  padding: 0 30rpx;
  font-size: 30rpx;
  font-weight: 500;
  color: #404040;

  image {
    margin-right: 5rpx;
    width: 36rpx;
    height: 36rpx;
  }
}

.mch-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: #f2f2f2;
}
.mch-footers {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  backdrop-filter: blur(30px);
  border-top: 1px solid rgba($color: #000000, $alpha: 0.1);
}
.info-wrapper {
  margin-top: 20rpx;
  width: 100%;
  border-radius: 10rpx;
}
.info {
  // width: 100%;
  display: flex;
  justify-content: center;
  padding: 20rpx;
  box-sizing: border-box;
  background-color: #f7f7f7;

  .mch-img {
    width: 40rpx;
    height: 40rpx;
    flex-shrink: 0;
  }
  .name {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin: 0 10rpx;
    width: 350rpx;
    color: #000;
    font-size: 28rpx;
    flex-grow: 1;
  }
  .number {
    white-space: nowrap;
    color: #8c8c8c;
    font-size: 28rpx;
  }
}
.footer-button {
  height: 170rpx;
}
.button-main {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1;
  display: flex;
  padding: 0 30rpx;
  align-items: center;
  height: 170rpx;
  border-top: 1rpx solid #ededed;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(20rpx);
  button {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 110rpx;
    font-size: 33rpx;
    font-weight: 500;
    color: #575757;
    background-color: #e6e6e6;
    &::after {
      border: none;
    }
  }
  .confirm {
    margin-left: 30rpx;
    background-color: $primaryColor;
    color: #fff;
  }
}
</style>
