<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.storeId" class="rate-input" placeholder="搜索门店ID" />
            <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="storeId"
              scrollX="100%"
            >
              <template #bodyCell="{ column, record }">
                <template v-if="column.key === 'storeId'">
                  {{ record.storeId }}
                </template>

                <template v-if="column.key === 'storeName'">
                  {{ record.storeName }}
                </template>

                <template v-if="column.key === 'bindState'">
                  <JeepayTableColState :state="vdata.selectRecordIdList.includes(record.storeId) ? 1 : 0" :showSwitchType="true" :onChange="(state) => { return updateBindState(record.storeId, state)}" />
                </template>
              </template>
            </JeepayTable>
          </div>
        </div>
      </div>
    </a-modal>
  </div>
</template>

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

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

// 定义向父组件 通讯 func 
const emit = defineEmits(['selectFinishFunc'])

const tableColumns = ref([
  { key: 'storeId', title: '门店ID'},
  { key: 'storeName', title: '门店名称'},
  { key: 'bindState', title: '是否绑定'},
])

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

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

// 请求table接口数据
function reqTableDataFunc (params){
  return req.list(API_URL_MCH_STORE_LIST, Object.assign({ mchNo: vdata.belongInfoId }, 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)
      }
      resolve()
    })
  })
}

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

// 显示
function show (bindList, belongInfoId) {
  vdata.selectRecordIdList = [] // 清空已选择的数据
  vdata.belongInfoId = belongInfoId
  if (bindList) {
    vdata.selectRecordIdList = JSON.parse(JSON.stringify(bindList))
  }

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

  vdata.visible = true
}

function close(){
  vdata.visible = false
}


// 定义对外输出函数
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>
