<template>
  <div>
    <crud-template ref="crudRef" class="as-weight" :submit-state="false" :home-data="field" :tableData="tableData"
      @btnSearch="btnSearch" @CurrentChange="handleCurrentChange" @importData="importData" @download="downloadHandle"
      @refreshLeft="refreshLeft" @add="Adds" @cancel="cancel" @submit="submitAdd" @handleEdit="itemEdit">
      <template #search>
        <el-input maxlength="30" v-trim clearable v-model="searchForm.batchNo" style="width: 200px"
          placeholder="请输入批次号" />
<!--        <el-input maxlength="60" v-trim clearable v-model="searchForm.cardNo" style="width: 200px"-->
<!--          placeholder="请输入卡号" />-->
        <el-input maxlength="20" v-trim clearable v-model="searchForm.startCardNo" style="width: 200px"
          placeholder="请输入起始卡号" />
        <el-input maxlength="20" v-trim clearable v-model="searchForm.endCardNo" style="width: 200px"
          placeholder="请输入结束卡号" />
<!--        <el-input maxlength="60" v-trim clearable v-model="searchForm.version" style="width: 200px" placeholder="请输入卡片版本号" />-->
        <el-select clearable v-model="searchForm.cardType" style="width: 150px;" placeholder="请选择卡片类型">
          <el-option v-for="item in CARD_TYPE" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <el-select clearable v-model="searchForm.status" style="width: 150px;" placeholder="请选择发行状态">
          <el-option v-for="item in ISSUE_STATUS" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </template>
    </crud-template>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
// @ts-ignore crudFrom模板
import CrudTemplate from '@/crud/index.vue'
import BaseService from '@/utils/baseService' //引入接口请求
import {
  ElMessage,
} from 'element-plus' //提示
import { getZxqdFindConfig,CARD_TYPE,ISSUE_STATUS } from "./data.js";
import { useRoute } from 'vue-router'
import { exportFn } from '@/views/settlement/exportFn'
import $storeinitData from "@/store/initData"; //引入tab vuex
//或取路由传入过来的对象数据
const route = useRoute()
const widthBase = '120px';
const crudRef = ref()
let tableData: any = ref([])
const itemStartValue = ref("1")
//查询参数
const searchForm = ref({})
const agencyIdList = ref([])
onMounted(() => {
  getList()
})
const field = ref(getZxqdFindConfig(route));
// 导出
function downloadHandle() {
  exportFn('/invw/api/cards/export', searchForm.value, '卡片一发')
}
//获取列表
function getList() {
  crudRef.value.tableLoding = true
  let params: any = {
    pageNo: field.value.paging.currentPage,
    pageSize: field.value.paging.pageSize,
  }
  //赋值查询参数
  let searchFormList = { ...searchForm.value }
  for (let key in searchFormList) {
    if (searchFormList[key]) {
      params[key] = searchFormList[key]
    }
  }
  if(searchFormList.status === 0){
    params.status = 0
  }
  BaseService.postN('/invw/api/cards/page', params).then((res: any) => {
    if (res && res.code === 0) {
      //数据转换
      let bizContent = res.data
      let data = bizContent.result || []
      //数据渲染
      tableData.value = data
      crudRef.value.tableLoding = false
      //分页总数
      field.value.paging.total = bizContent.totalCount
    } else {
      crudRef.value.tableLoding = false
      ElMessage.error(res.message)
    }
  })
}

const Adds = () => {
  itemStartValue.value = 1
}
//表单编辑按钮
function itemEdit(idx: any, row: any) {
  itemStartValue.value = 2
}

// 添加
const submitAdd = (data: any) => {
  request(data, itemStartValue.value)
}

const request = (data: any, type: any) => {
  data.optType = type //操作类型 1新增 2修改 
  BaseService.postN(itemStartValue.value == 1 ? '/userw/wxCar/add' : '/userw/wxCar/update', data).then((res: any) => {
    if (res && res.code === 0) {
      getList()
      crudRef.value.reset();
      crudRef.value.dialogFormVisible = false;
    } else {
      ElMessage.error(res.message)
    }
  })
}
// 删除
const deleteHandle = (index) => {
  addForm.value.locationModels.splice(index, 1)
}
// 搜索按钮
function btnSearch() {
  field.value.paging.currentPage = 1
  getList()
}

//标签分页
function handleClick(tab, event) {
  orderStep.value = tab.props.name
  getList()
}

//分页
function handleCurrentChange(val: number) {
  field.value.paging.currentPage = val
  getList()
}

// 搜索重置
function refreshLeft() {
  searchForm.value = {}
  getList()
}

</script>
<style lang="scss" scoped></style>
