<template>
  <div>
    <a-modal v-model:visible="vdata.visible" width="80%" title="选择产品" @ok="okFunc">
      <div class="modal-div">
        <!-- 码牌列表 -->
        <div class="modal-div1">
          <div class="search">
            <a-input v-model:value="vdata.searchData.productName" class="rate-input" placeholder="搜索产品名称" />
            <a-button type="primary" @click="searchFunc"><template #icon><search-outlined /></template>查询</a-button>
            <a-button style="margin-left: 8px" @click="() => vdata.searchData = {} "><reload-outlined />重置</a-button>
          </div>

          <div class="list-table">
            <JeepayTable
                ref="infoTable"
                :initData="false"
                :reqTableDataFunc="reqTableDataFunc"
                :tableColumns="tableColumns"
                :searchData="vdata.searchData"
                rowKey="productId"
                scrollX="100%"
            >
              <template #bodyCell="{ column, record }">

                <template v-if="column.key === 'productState'">
                  <a-badge v-if="record.productState == 1" status="success" text="启用" />
                  <a-badge v-if="record.productState == 0" status="error" text="禁用" />
                </template>

                <template v-if="column.key === 'charge'">
                  <a-badge v-if="record.charge == 1" status="success" text="是" />
                  <a-badge v-if="record.charge == 0" status="error" text="否" />
                </template>
                <template v-if="column.key === 'bindState'">
                  <JeepayTableColState :state="vdata.selectRecordIdList.includes(record.productId) ? 1 : 0" :showSwitchType="true" :onChange="(state) => { return updateBindState(record, state)}" />
                </template>
              </template>
            </JeepayTable>
          </div>
        </div>
      </div>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
import {API_URL_MCH_QR_CODE_LIST, req, $qrcShellViewByQrc, API_URL_PRODUCT_LIST} from '@/api/manage'
import { reactive, ref, defineExpose, nextTick, getCurrentInstance } from 'vue'

// 导入全局函数
const { $viewerApi } = getCurrentInstance()!.appContext.config.globalProperties

// 定义向父组件 通讯 func
const emit = defineEmits(['selectFinishFunc'])
const props = defineProps({
  configMode: { type: String, default: '' },   // 模式
})

const tableColumns = ref([
  { key: 'productName', title: '产品名称', fixed: 'left', dataIndex: 'productName'  },
  { key: 'typeName', title: '产品分类', dataIndex: 'typeName'  },
  { key: 'productState', title: '状态', dataIndex: 'productState' },
  // { key: 'productDesc', title: '描述', dataIndex: 'productDesc',width:500 },
  // { key: 'charge', title: '是否收费', dataIndex: 'price' },
  // { key: 'applyForCount', title: '申请数量', dataIndex: 'applyForCount' },
  // { key: 'orderCount', title: '订购数量', dataIndex: 'orderCount' },
  // { key: 'productSort', title: '排序', dataIndex: 'productSort' },
  { key: 'createdAt', dataIndex: 'createdAt', title: '创建日期' },
  { key: 'bindState', dataIndex: 'bindState', title: '是否绑定'},
])

// 当前 table 组件
const infoTable = ref()

// 响应式数据
const vdata :any = reactive ({
  visible: false,
  searchData: {},
  selectRecordIdList: []
})

// 请求table接口数据
function reqTableDataFunc (params){
  params.productState=1
  return req.list(API_URL_PRODUCT_LIST, params)
}

// 表格搜索
function searchFunc () {
  nextTick(() => {
    infoTable.value.refTable(true)
  })
}

// 表格行开关开启、关闭
function updateBindState(recordId, bindState) {
  return new Promise<void>((resolve, reject) => {
    nextTick(() => {
      if (bindState) {
        vdata.selectRecordIdList.push(recordId)
      } else {
        vdata.selectRecordIdList.splice(vdata.selectRecordIdList.indexOf(recordId), 1)
      }
      console.log(vdata.selectRecordIdList,'vdata.selectRecordIdList')
      resolve()
    })
    // return reqLoad.updateById(API_URL_STORE_DEVICE, recordId, { state: state }).then(res => {
    //   searchFunc()
    //   resolve()
    // }).catch(err => reject(err))
  })
}

// 选择完毕
function okFunc(){
  console.log(vdata.selectRecordIdList)
  emit('selectFinishFunc',  vdata.selectRecordIdList)
}

// 显示
function  show (storeId, bindQrcList) {
  vdata.selectRecordIdList = [] // 清空已选择的数据

  // vdata.searchData.qrcBelongType = 2
  vdata.searchData.getType = 1
  if (bindQrcList) {
    vdata.selectRecordIdList =  JSON.parse(JSON.stringify(bindQrcList))
  }

  nextTick(() => {
    searchFunc()
  })

  vdata.visible = true
}

function close(){
  vdata.visible = false
}

// 显示二维码图片
function showQrImgFunc(recordId){
  $qrcShellViewByQrc(recordId).then((res) => {
    $viewerApi({images: [res]})
  })
}

// 定义对外输出函数
defineExpose( { show, close } )
</script>

<style scoped lang="less">
.modal-div {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  margin-top: 20px;

  .modal-div1 {
    width: 100%
  }

  .list-table {
    border: 1px solid #ddd;
    border-radius: 6px;
  }
}

.search {

  display: flex;
  margin: 10px 0;
  .rate-input {
    width: 200px;
    margin-right: 10px;
  }
}

</style>
