<!-- 选择签章 -->
<script lang="ts" setup name="selectSignature">
import { ElMessage } from 'element-plus'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getTypeSelect } from '@/api/system/price'
import { getDictByCode } from '@/api/system/dict'
import type { typeofSign } from '@/views/system/tool/tool_interface'
import { listPageApi } from '@/api/system/tool'

const emits = defineEmits(['add'])
const dialogFormVisible = ref(false) // 控制弹窗显隐
// 查询参数
const searchQuery = reactive({
  signNo: '', // 编号
  signName: '', // 名称
  signDirector: '', // 负责人
  createEndTime: '',
  createStartTime: '',
  ids: [],
  limit: 20,
  offset: 1,
  signType: '',
}) // 查询参数
const loadingTable = ref(false) // loading
const list = ref([]) // 表格参数
const total = ref(0)
const columns = ref<TableColumn[]>([
  { text: '签章编号', value: 'signNo', align: 'center', width: '180' },
  { text: '签章名称', value: 'signName', align: 'center', width: '150' },
  { text: '签章负责人', value: 'signDirector', align: 'center', width: '100' },
  { text: '创建时间', value: 'createTime', align: 'center', width: '250' },
  { text: '描述', value: 'signDesc', align: 'center' },
])
const checkoutList = ref<string[]>([]) // 多选选中参数
// 获取类型
const getType = async () => {
  const res = await getDictByCode('signType')
  searchQuery.signType = res.data.filter((item: typeofSign) => item.name === '签章')[0].value
}
// 获取数据列表
const getList = () => {
  loadingTable.value = true
  listPageApi(searchQuery).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows
      total.value = res.data.total
    }
    loadingTable.value = false
  }).catch((_) => {
    loadingTable.value = false
  })
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.limit = val.size
  }
  if (val && val.page) {
    searchQuery.offset = val.page
  }
  getList()
}

// 搜索
const search = () => {
  getList()
}
// 重置
const clearList = () => {
  // createTime.value = ''
  searchQuery.createEndTime = ''
  searchQuery.createStartTime = ''
  searchQuery.signDirector = ''
  searchQuery.signName = ''
  searchQuery.signNo = ''
  getList()
}

// 点击保存
const submitForm = () => {
  if (!checkoutList.value.length) {
    ElMessage.warning('请选中')
  }
  else {
    emits('add', checkoutList.value[0])
    dialogFormVisible.value = false
  }
}

// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  clearList()
}

// 多选选中
const handleSelectionChange = (val: any) => {
  checkoutList.value = val
}

// 初始化
const initDialog = () => {
  dialogFormVisible.value = true
  getType().then((_) => {
    getList()
  })
}
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="选择签章" width="65%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="clearList">
      <search-item>
        <el-input v-model="searchQuery.signNo" placeholder="签章编号" clearable class="w-50 m-2 normal-input" />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.signName" placeholder="签章名称" clearable class="w-50 m-2 normal-input" />
      </search-item>
      <search-item>
        <el-input v-model="searchQuery.signDirector" placeholder="负责人" clearable class="w-50 m-2 normal-input" />
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        :data="list"
        :total="total"
        :columns="columns"
        :query="searchQuery"
        is-showmulti-select
        :list-loading="loadingTable"
        :is-multi="false"
        @change="changePage"
        @multi-select="handleSelectionChange"
      >
        <!-- 序号 -->
        <template #preColumns>
          <el-table-column label="#" width="55" align="center" fixed>
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="submitForm">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
:deep(.el-radio__label) {
  display: none;
}
</style>
