<script lang="ts" setup name="SolveList">
import { ref } from 'vue'
import type { Ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import type { IlistQuery, IsolveListType } from '@/views/device/receive/receive'
import { exportExcel } from '@/utils/exportXlsx'
import ButtonBox from '@/views/device/receive/solveComponent/buttonBox.vue'
import ReceiveDialog from '@/views/device/receive/receiveDialog.vue'

const loadingTable = ref(false) // 表格loading
const active = ref('unused') // 选中的按钮
const type = {
  check: '查看',
  normal: '正常',
}
// 查询条件
const listQuery: Ref<IlistQuery> = ref({
  number: '', // 编号
  name: '', // 名称
  dep: '', // 部门
  person: '', // 人
  date: '', // 日期
  offset: 1,
  limit: 10,
})
// 表格数据
const list = ref([
  {
    number: 'string', // 编号
    name: 'string', // 名称
    dep: 'string', // 文件号
    person: 'string', // 类别
    date: 'string', // 创建人
    status: '审批中', // 审批状态
  },
  {
    number: 'string', // 编号
    name: 'string', // 名称
    dep: 'string', // 文件号
    person: 'string', // 类别
    date: 'string', // 创建人
    status: '审批中', // 审批状态
  },
  {
    number: 'string', // 编号
    name: 'string', // 名称
    dep: 'string', // 文件号
    person: 'string', // 类别
    date: 'string', // 创建人
    status: '审批中', // 审批状态
  },
])
const { proxy } = getCurrentInstance() as any
// 总数
const total = ref(20)
// 表头
const columns = [
  {
    text: '申请编号',
    value: 'number',
    // width: '120',
  },
  {
    text: '申请名称',
    value: 'name',
    // width: '160',
  },
  {
    text: '申请部门',
    value: 'dep',
  },
  {
    text: '申请人',
    value: 'person',
  },
  {
    text: '申请时间',
    value: 'date',
  },
  {
    text: '审批状态',
    value: 'status',
  },
]
// 选中的内容
const checkoutList = ref([])
// 重置
const clearList = () => {
  listQuery.value.number = ''
  listQuery.value.name = ''
  listQuery.value.dep = ''
  listQuery.value.person = ''
  listQuery.value.date = ''
}
// 搜索
const searchList = () => {

}
// 多选发生改变时
const handleSelectionChange = (e: any) => {
  checkoutList.value = e
}
// 导出
const exportExcelBtn = () => {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(255, 255, 255, 0.8)',
  })
  if (checkoutList.value.length <= 0) {
    exportExcel({
      json: list.value.map((item: IsolveListType, index: number) => ({ index: index + 1, number: item.number, name: item.name, dep: item.dep, person: item.person, date: item.date, status: item.status })),
      name: '设备领用处理',
      titleArr: ['序号', '申请编号', '申请名称', '申请部门', '申请人', '申请时间', '审批状态'],
      sheetName: 'sheet1',
    })
  }
  else {
    exportExcel({
      json: checkoutList.value.map((item: IsolveListType, index: number) => ({ index: index + 1, number: item.number, name: item.name, dep: item.dep, person: item.person, date: item.date, status: item.status })),
      name: '设备领用处理',
      titleArr: ['序号', '申请编号', '申请名称', '申请部门', '申请人', '申请时间', '审批状态'],
      sheetName: 'sheet1',
    })
  }
  loading.close()
}
const exportAll = () => {
  exportExcelBtn()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  // fetchData(true)
}
// 打印
const printObj = ref({
  id: 'print', // 需要打印元素的id
  popTitle: '设备领用处理', // 打印配置页上方的标题
  extraHead: '', // 最上方的头部文字，附加在head标签上的额外标签，使用逗号分割
  preview: false, // 是否启动预览模式，默认是false
  previewBeforeOpenCallback() { console.log('正在加载预览窗口！') }, // 预览窗口打开之前的callback
  previewOpenCallback() { console.log('已经加载完预览窗口，预览打开了！') }, // 预览窗口打开时的callback
  beforeOpenCallback() { console.log('开始打印之前！') }, // 开始打印之前的callback
  openCallback() { console.log('执行打印了！') }, // 调用打印时的callback
  closeCallback() { console.log('关闭了打印工具！') }, // 关闭打印的callback(无法区分确认or取消)
  clickMounted() { console.log('点击v-print绑定的按钮了！') },
  // url: 'http://localhost:8080/', // 打印指定的URL，确保同源策略相同
  // asyncUrl (reslove) {
  //   setTimeout(() => {
  //     reslove('http://localhost:8080/')
  //   }, 2000)
  // },
  standard: '',
  extarCss: '',
})
// 操作
const $router = useRouter()
const handleEdit = (index: number, row: IsolveListType, val: string) => {
  if (val === '领用') {
    ElMessageBox.confirm(
      '确认领用吗？',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      },
    )
      .then(() => {
        // 调领用接口
        ElMessage({
          type: 'success',
          message: '领用成功',
        })
      })
  }
  else if (val === '删除') {
    ElMessageBox.confirm(
      '确认删除吗？',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      },
    )
      .then(() => {
        // 调删除接口
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
      })
  }
  else if (val === '退领') {
    ElMessageBox.confirm(
      '确认退领吗？',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      },
    )
      .then(() => {
        // 调退领接口
        ElMessage({
          type: 'success',
          message: '退领成功',
        })
      })
  }
  else if (val === '查看') {
    $router.push({ path: '/receive/solveDetail', query: { deviceName: row.name } })
  }
}
// 右上角按钮切换
const changeCurrentButton = (val: string) => {
  clearList()
  console.log('button 切换', val)
  active.value = val
  if (val === 'unused') { // 未领用

  }
  else if (val === 'used') { // 已领用

  }
}
// 标签识别
const execTag = () => {

}
</script>

<template>
  <app-container>
    <search-area
      :need-clear="true"
      @search="searchList" @clear="clearList"
    >
      <search-item>
        <el-input
          v-model.trim="listQuery.number"
          placeholder="申请编号"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.name"
          placeholder="申请名称"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.dep"
          placeholder="申请部门"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.person"
          placeholder="申请人"
          clearable
        />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="listQuery.date"
          type="date"
          placeholder="申请日期"
        />
      </search-item>
    </search-area>

    <table-container>
      <template #btns-right>
        <icon-button icon="icon-device" title="标签识别" @click="execTag" />
        <icon-button v-if="proxy.hasPerm('/measure/price/export')" icon="icon-export" title="导出" type="primary" @click="exportAll" />
        <icon-button v-if="proxy.hasPerm('/measure/price/print')" v-print="printObj" icon="icon-print" title="打印" type="primary" />
      </template>
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" is-showmulti-select @change="changePage" @multiSelect="handleSelectionChange"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column label="操作" align="center" width="130" fixed="right">
            <template #default="scope">
              <el-button
                size="small"
                type="primary"
                link
                @click="handleEdit(scope.$index, scope.row, '查看')"
              >
                查看
              </el-button>
              <el-button
                v-if="active === 'unused'"
                size="small"
                link
                type="primary"
                @click="handleEdit(scope.$index, scope.row, '领用')"
              >
                领用
              </el-button>
              <!-- 发起者和有删除权限的可以删除，已通过未通过状态不可以删除 -->
              <el-button
                v-if="active === 'unused'"
                size="small"
                link
                type="danger"
                @click="handleEdit(scope.$index, scope.row, '删除')"
              >
                删除
              </el-button>
              <el-button
                v-if="active === 'used'"
                size="small"
                link
                type="primary"
                @click="handleEdit(scope.$index, scope.row, '退领')"
              >
                退领
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
    <button-box
      :active="active"
      @changeCurrentButton="changeCurrentButton"
    />
  </app-container>
</template>

<style lang="scss" scoped>
// 样式
</style>

