<template>
  <page-header-wrapper>
    <a-card>
      <JeepaySearchForm :searchConditionNum="3" :searchFunc="searchFunc" :resetFunc="() => { vdata.searchData= {} }">
        <JeepaySelect ref="selectClientType" :flag="selectFlag">
          <a-select v-model:value="vdata.searchData.clientType" :placeholder="'客户端类型'">
            <a-select-option value="">全部</a-select-option>
            <a-select-option value="MCH_APP">商户APP</a-select-option>
            <a-select-option v-if="$hasAgentEnt()" value="AGENT_APP">代理商APP</a-select-option>
          </a-select>
        </JeepaySelect>
        <jeepay-text-up v-model:value="vdata.searchData['versionSerialNumber']" :placeholder="'版本序列号'" />
      </JeepaySearchForm>

      <!-- 列表渲染 -->
      <JeepayTable
        ref="infoTable"
        :init-data="true"
        :req-table-data-func="reqTableDataFunc"
        :table-columns="tableColumns"
        :search-data="vdata.searchData"
        row-key="sysUserId"
        @btnLoadClose="vdata.btnLoading=false"
      >
        <template #topBtnSlot>
          <div>
            <a-button v-if="$access('ENT_CLIENT_VERSION_ADD')" type="primary" class="mg-b-30" @click="addFunc">
              <plus-outlined />新建
            </a-button>
          </div>
        </template>
        <template #bodyCell="{column,record}">
          <template v-if="column.key === 'forceUpdate'">
            <span v-if="record.forceUpdate == 1">是</span>
            <span v-else>否</span>
          </template>
          <template v-if="column.key === 'clientType'">
            <span v-if="record.clientType == 'MCH_APP'">商户APP</span>
            <span v-if="record.clientType == 'AGENT_APP'">代理商APP</span>
          </template>
          <template v-if="column.key === 'op'">
            <JeepayTableColumns>
              <a v-if="$access('ENT_CLIENT_VERSION_VIEW')" style="margin-right: 20px;" @click="viewFunc(record.versionId)">详情</a>
              <a v-if="$access('ENT_CLIENT_VERSION_EDIT')" style="margin-right: 20px;" @click="editFunc(record.versionId)">编辑</a>
              <a v-if="$access('ENT_CLIENT_VERSION_DEL')" style="color: red;margin-right: 20px;" @click="delFunc(record.versionId)">删除</a>
            </JeepayTableColumns>
          </template>
        </template>
      </JeepayTable>
    </a-card>

    <!-- 新增 / 修改 页面组件  -->
    <InfoAddOrEdit ref="infoAddOrEdit" :callback-func="searchFunc" />

    <!-- 详情 页面组件  -->
    <InfoDetail ref="infoDetail" :callback-func="searchFunc" />
  </page-header-wrapper>
</template>
<script setup lang="ts">
import { API_URL_CLIENT_VERSION_LIST, req, reqLoad } from '@/api/manage'
import InfoAddOrEdit from './AddOrEdit.vue'
import InfoDetail from './Detail.vue'
import { reactive,ref,getCurrentInstance} from 'vue'
import { message } from 'ant-design-vue'
const { $infoBox,$access, $hasAgentEnt } = getCurrentInstance()!.appContext.config.globalProperties

    const tableColumns = reactive([
      {key:'versionName', title: '版本名称', dataIndex: 'versionName', fixed: 'left'  },
      {key:'clientType', title: '客户端', dataIndex: 'clientType' },
      {key:'versionSerialNumber', title: '版本序列号', dataIndex: 'versionSerialNumber' },
      {key:'forceUpdate', title: '是否强制更新' },
      {key:'createdAt', title: '创建时间', dataIndex: 'createdAt', width: 200 },
      {key: 'op',title: '操作',width: 200, fixed: 'right',align: 'center' }
    ])

    const infoTable = ref()
    const infoAddOrEdit = ref()
    const infoDetail = ref()
    const selectClientType = ref()
    const vdata = reactive ({
      tableColumns: tableColumns,
      searchData: { 'clientType':  ''} as any,
      btnLoading: false
    })
      // 请求table接口数据
    function reqTableDataFunc(params){
        return req.list(API_URL_CLIENT_VERSION_LIST, params)
    }

    function searchFunc() { // 点击【查询】按钮点击事件
      vdata.btnLoading = true // 打开查询按钮的loading
      infoTable.value.refTable(true)
    }

    function addFunc () { // 业务通用【新增】 函数
      infoAddOrEdit.value.show(null, vdata.searchData.clientType)
    }

    function editFunc (recordId) { // 业务通用【修改】 函数
      infoAddOrEdit.value.show(recordId, vdata.searchData.clientType)
    }

    function viewFunc (recordId) {  // 业务通用【详情】 函数
      infoDetail.value.show(recordId)
    }

    function delFunc(recordId) { // 业务通用【删除】 函数
       $infoBox.confirmDanger('确认删除？', '', () => {
          return req.delById(API_URL_CLIENT_VERSION_LIST, recordId).then(res => {
            $infoBox.message.success('删除成功！')
           infoTable.value.refTable(false)
          })
        })
    }

    function updateState (recordId, state) { // 【更新状态】
        const title = state === 1 ? '确认[启用]该用户？' : '确认[停用]该用户？'
        const content = state === 1 ? '启用后用户可进行登陆等一系列操作' : '停用后该用户将立即退出系统并不可再次登陆'

        return new Promise<void>((resolve, reject) => {
          $infoBox.confirmDanger(title, content, () => {
            return reqLoad.updateById(API_URL_CLIENT_VERSION_LIST, recordId, { state: state }).then(res => {
              searchFunc()
              resolve()
            }).catch(err => reject(err))
          },
            () => {
            reject(new Error())
          })
        })
    }

    let selectFlag = ref(0) // 下拉搜索标志位

</script>
