<script lang="ts" setup name="borrowHandleList">
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import approveAction from './approvalActionDialog.vue'
import { printJSON } from '@/utils/printUtils'
const props = defineProps({
  status: {
    type: String,
    required: true,
  },
})
const { proxy } = getCurrentInstance() as any
const $router = useRouter()
const time = ref()
const searchQuery = reactive({
  applyNo: '',
  applyName: '',
  applyUnit: '',
  applyPerson: '',
  limit: 10,
  offset: 1,
  ids: [],
}) // 查询参数
watch(() => time.value, (newVal) => {
  if (newVal) {
    // searchQuery.noticeStartTime = newVal[0]
    // searchQuery.noticeEndTime = newVal[1]
  }
  else {
    // searchQuery.noticeStartTime = ''
    // searchQuery.noticeEndTime = ''
  }
})
const loadingTable = ref<boolean>(false) // 表格loading
const total = ref<number>(0) // 数据总条数
const columns = ref([
  {
    text: '申请编号',
    value: 'applyNo',
    align: 'center',
  },
  {
    text: '申请名称',
    value: 'applyName',
    align: 'center',
  },
  {
    text: '申请部门',
    value: 'applyUnit',
    align: 'center',
  },
  {
    text: '申请人',
    value: 'applyPerson',
    align: 'center',
  },
  {
    text: '申请时间',
    value: 'time',
    align: 'center',
  },
  {
    text: '审批状态',
    value: 'approvalStatusName',
    align: 'center',
  },
]) // 表格
const list = ref([]) // 表格数据
const dialogVisible = ref<boolean>(false) // 添加组件的显示与隐藏
// 获取数据列表
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
  // })
  list.value = [
    {
      applyName: '借用测试设备',
      applyNo: '1433223',
      applyUnit: '顶级',
      applyPerson: '张三',
      time: '2020-12-12 00:00:00',
      approvalStatusName: '审批中',
    },
    {
      applyName: '借用电脑',
      applyNo: '1433223',
      applyUnit: '徐州分中心',
      applyPerson: '里斯',
      time: '2020-12-12 00:00:00',
      approvalStatusName: '已通过',
    },
  ] as any
  total.value = 2
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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 detail = (row) => {
  // console.log(row, 'row')
  // dialogVisible.value = true
  // addRef.value.initDialog({
  //   title: '详情',
  //   ...row,
  // })
  $router.push({
    name: 'stateManageDetail',
    params: {
      type: 'detail',
    },
    query: {
      title: '详情',
      name: '设备借用处理',
    },
  })
}
// 编辑
const update = (row) => {
  // dialogVisible.value = true
  // addRef.value.initDialog({
  //   title: '编辑',
  //   ...row,
  // })
  $router.push({
    name: 'stateManageDetail',
    params: {
      type: 'edit',
    },
    query: {
      title: '编辑',
      name: '设备借用处理',
    },
  })
}
// 删除
const remove = (row) => {
  ElMessageBox.confirm(
    `确认删除${row.applyName}吗？`,
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      // assetsDeleteApi({ id: row.id as string }).then((res) => {
      //   if (res.code === 200) {
      //     ElMessage({
      //       type: 'success',
      //       message: '删除成功',
      //     })
      //     getList()
      //   }
      // })
    })
}
// 搜索
const search = () => {
  getList()
}
// 重置
const reset = () => {
  // searchQuery.equipmentCategory = ''
  getList()
}
// 模板下载
const templateDownload = () => {

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

}
// 借用
const borrow = (row) => {
  ElMessageBox.confirm(
    `确认借用${row.applyName}吗？`,
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
}
// 归还
const returnBtn = (row) => {
  ElMessageBox.confirm(
    `确认归还${row.applyName}吗？`,
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
}
// 新增
const addBtn = () => {
  // dialogVisible.value = true
  // addRef.value.initDialog({
  //   title: '新建',
  // })
  $router.push({
    name: 'stateManageDetail',
    params: {
      type: 'add',
    },
    query: {
      title: '新建',
      name: '设备借用处理',
    },
  })
}
// 表格被选中的行
const selectList = ref([])
// 表格多选
const multiSelect = (row) => {
  selectList.value = row
}
// 导出
const exportExcelBtn = () => {
  // const loading = ElLoading.service({
  //   lock: true,
  //   text: 'Loading',
  //   background: 'rgba(255, 255, 255, 0.8)',
  // })
  // if (selectList.value.length) {
  //   selectList.value.forEach((item) => {
  //     searchQuery.ids?.push(item.id as string)
  //   })
  // }
  // listExportApi({ ...searchQuery, limit: undefined, offset: undefined }).then((res) => {
  //   exportFile(res.data, '固定资产列表')
  //   loading.close()
  // searchQuery.ids = []
  // }).catch((_) => {
  //   loading.close()
  // })
}
// 打印
function printList() {
  const selectIds = selectList.value.map(item => item.id)
  const properties = columns.value.map((item) => {
    return {
      field: item.value,
      displayName: item.text,
    }
  })
  if (selectIds.length <= 0 && list.value.length > 0) {
    printJSON(list.value, properties, '设备借处理')
  }
  else if (selectIds.length > 0) {
    const printList = list.value.filter(item => selectIds.includes(item.id))
    printJSON(printList, properties, '设备借处理')
  }
  else {
    ElMessage('无可打印内容')
  }
}
const refreshDta = () => {
  getList()
  dialogVisible.value = false
}
onMounted(() => {
  getList()
  // 获取设备类别
  // getDictByCode('equipmentCategory').then((response) => {
  //   equipmentCategoryList.value = response.data
  // })
})
</script>

<template>
  <div>
    <!-- 添加/编辑/查看 -->
    <app-container>
      <!-- 审批操作 -->
      <approve-action ref="approveRef" />
      <!-- 筛选条件 -->
      <search-area :need-clear="true" @search="search" @clear="reset">
        <search-item>
          <el-input v-model="searchQuery.applyNo" placeholder="申请编号" clearable class="w-50 m-2" />
        </search-item>
        <search-item>
          <el-input v-model="searchQuery.applyName" placeholder="申请名称" clearable class="w-50 m-2" />
        </search-item>
        <search-item>
          <el-input v-model="searchQuery.applyUnit" placeholder="申请部门" clearable class="w-50 m-2" />
        </search-item>
        <search-item>
          <el-input v-model="searchQuery.applyPerson" placeholder="申请人" clearable class="w-50 m-2" />
        </search-item>
        <search-item>
          <!-- <el-input v-model="searchQuery.equipmentCategory" placeholder="设备类型" clearable class="w-50 m-2" /> -->
          <!-- <el-input v-model="searchQuery.equipmentSpecifications" placeholder="申请日期" clearable class="w-50 m-2" /> -->
          <el-date-picker
            v-model="time"
            type="datetimerange"
            format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
            range-separator="至"
            start-placeholder="申请开始时间"
            end-placeholder="申请结束时间"
            clearable
          />
        </search-item>
      </search-area>
      <table-container>
        <!-- 表头区域 -->
        <template #btns-right>
          <icon-button icon="icon-device" title="标签识别" @click="identify" />
          <icon-button icon="icon-export" title="导出" @click="exportExcelBtn" />
          <icon-button icon="icon-print" title="打印" @click="printList" />
        </template>
        <!-- 表格区域 -->
        <normal-table
          id="print"
          :data="list" :total="total" :columns="columns as any"
          :is-showmulti-select="true"
          :query="{ limit: searchQuery.limit, offset: searchQuery.offset }"
          :list-loading="loadingTable"
          :is-multi="true"
          @change="changePage"
          @multi-select="multiSelect"
        >
          <template #preColumns>
            <el-table-column label="序号" width="55" align="center">
              <template #default="scope">
                {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
              </template>
            </el-table-column>
          </template>
          <template #columns>
            <el-table-column label="操作" align="center" :width="status === '待借用' ? '130' : status === '已借用' ? '110' : '80'">
              <template #default="{ row }">
                <el-button size="small" type="primary" link @click="detail(row)">
                  查看
                </el-button>
                <el-button v-if="status === '待借用'" size="small" type="primary" link @click="borrow(row)">
                  借用
                </el-button>
                <el-button v-if="status === '已借用'" size="small" type="primary" link @click="returnBtn(row)">
                  归还
                </el-button>
                <el-button v-if="status === '待借用'" size="small" type="danger" link @click="remove(row)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
    </app-container>
  </div>
</template>

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