
<template>
  <a-divider orientation="left" style="color: #1A66FF;">
    用户号：{{ vdata.mchNo }}
    <a-button :loading="vdata.bizQueryInfoBtnLoading" size="small" style="margin-left: 20px; margin-bottom: 20px;" type="primary" @click="queryStoreState()"><reload-outlined />刷新</a-button>
  </a-divider>
  <a-table bordered :data-source="vdata.storeParamsList" :columns="tableColumns">
    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'state'">
        <a-badge v-if="record.state == 0" status="default" text="未入驻" />
        <a-badge v-if="record.state == 1" status="warning" text="审核中" />
        <a-badge v-if="record.state == 2" status="success" text="已入驻" />
        <a-badge v-if="record.state == 3" status="error" text="入驻失败" />
        <a-tooltip v-if="record.state == 3" :title="record.reject_reason">
          <info-circle-outlined />
        </a-tooltip>
      </template>
      <template v-if="column.key === 'op'">
        <!-- 操作列插槽 -->
        <JeepayTableColumns>
          <a-button type="link" :disabled="vdata.btnLoading || record.state == 1 || record.state == 2" @click="applyment(record.storeId, record.state, record.storeNo)">入驻门店</a-button>
        </JeepayTableColumns>
      </template>
    </template>
  </a-table>
</template>

<script lang="ts" setup>
import { API_URL_MCH_STORE_LIST, req, $applymentStore, $hmpayQueryStoreInfo } from '@/api/manage'
import { reactive, getCurrentInstance } from 'vue'
const { $infoBox } = getCurrentInstance()!.appContext.config.globalProperties

const tableColumns = reactive([
  { title: '门店编号', dataIndex: 'storeId', fixed: 'left' },
  { title: '门店名称', dataIndex: 'hmStoreName' },
  { title: '入驻状态', key: 'state', dataIndex: 'state' },
  { title: '入驻编号', dataIndex: 'storeNo' },
  { key: 'op',title: '操作', fixed: 'right', align: 'center' }
])

const vdata : any = reactive({
  recordId: null, // 更新对象ID
  tableColumns: tableColumns,
  btnLoading: false,
  storeParamsListRes: [], // 河马门店信息原数据
  storeParamsList: [] // 门店信息(包含河马付和我方数据)
})

function show (recordId, mchNo) { // 弹层打开事件
  vdata.recordId = recordId
  vdata.mchNo = mchNo
  queryStoreState()
}

// 获取门店入驻信息
function queryStoreState(){
  vdata.storeParamsListRes = []
  vdata.storeParamsList = []
  $hmpayQueryStoreInfo(vdata.recordId).then(res => {
    vdata.storeParamsListRes = JSON.parse(res || '[]')
   
    if (vdata.storeParamsListRes && vdata.storeParamsListRes.length > 0) {
      vdata.storeParamsListRes.forEach(item => {
        item.hmStoreName = item.store_name
        item.storeNo = item.store_no
        vdata.storeParamsList.push(item)
      }) 
    }
    
    reqTableDataFunc(vdata.storeParamsList)
  }).catch(error => {
    // reqTableDataFunc(vdata.storeParamsListRes)
  })
}

// 请求商户门店信息并将数据与入驻信息进行对比
function reqTableDataFunc (storeParamsList:any)  {
  if (storeParamsList && storeParamsList.length > 0) {
    vdata.storeParamsList = storeParamsList

    // 放入其他未入驻的门店
     req.list(API_URL_MCH_STORE_LIST, {mchNo: vdata.mchNo, pageSize: -1}).then((res) => {
          let storeList:any = res.records
          const ids =  vdata.storeParamsList.map(v=>v.storeId)
          for(let i= 0;i < storeList.length; i++){     
            const index = ids.findIndex(v=>v ==storeList[i].storeId )         
            if(index==-1){
              storeList[i].state = 0
              storeList[i].storeNo = ''
              storeList[i].reject_reason = ''
              storeList[i].hmStoreName = storeList[i].storeName
              storeParamsList.push(storeList[i])
            }
          }
          vdata.storeParamsList = storeParamsList
      })  
  } else {
    // 放入全部的门店
    req.list(API_URL_MCH_STORE_LIST, {mchNo: vdata.mchNo, pageSize: -1}).then((res) => {
        let storeList:any = res.records
        for(let i= 0;i < storeList.length; i++){     
            storeList[i].state = 0
            storeList[i].storeNo = ''
            storeList[i].reject_reason = ''
            storeList[i].hmStoreName = storeList[i].storeName
            storeParamsList.push(storeList[i])
        }
        vdata.storeParamsList = storeParamsList
    })  
  }
}

// 门店入驻
function applyment (storeId, state, hmStoreNo) {
  $infoBox.confirmDanger('确认入驻该门店吗？', '', () => {
    vdata.btnLoading = true
    $applymentStore(storeId, state, vdata.recordId, {storeNo: hmStoreNo}).then(res => {
        $infoBox.message.success('入驻成功')
        queryStoreState()
        vdata.btnLoading = false
      }).finally(() => {
        vdata.btnLoading = false
    })
  })
}


defineExpose({ show })
</script>
<style scoped>

.form-item-content{
  width: 70%
}

</style>