<template>
  <!-- 搜索 -->
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="90px"
    >
      <el-form-item label="单据类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" class="!w-240px">
          <el-option
            v-for="(dict, index) in getIntDictOptions(DICT_TYPE.DOCUMENT_TYPE)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单据状态" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" class="!w-240px">
          <el-option
            v-for="(dict, index) in getIntDictOptions(DICT_TYPE.DOCUMENT_STATUS)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商名称" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" class="!w-240px">
          <el-option
            v-for="(dict, index) in getIntDictOptions(DICT_TYPE.DOCUMENT_STATUS)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库名称" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" class="!w-240px">
          <el-option
            v-for="(dict, index) in getIntDictOptions(DICT_TYPE.APPROVAL_STATUS)"
            :key="index"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入库单号" prop="bedId">
        <el-input v-model="queryParams.bedId" placeholder="请输入床位号" class="!w-240px" />
      </el-form-item>
      <el-form-item label="入库日期" prop="bedId">
        <TgDatePicker type="daterange" v-model="queryParams.queryDayTime" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-row style="align-items: center">
      <el-button type="primary" plain @click="handleAdd" v-hasPermi="['putStorage:warehouseManage:add']">
        <Icon icon="ep:plus" class="mr-5px" /> 新增
      </el-button>
      <el-button type="danger" plain @click="handleAdd" v-hasPermi="['warehouseManage:delete']">
        <Icon icon="ep:delete" class="mr-5px" /> 删除
      </el-button>
      <el-button :disabled="(selectionChangeList.length==0)" type="success" plain @click="backfillHan" v-hasPermi="['putStorage:warehouseManage:backfill']" >
        <Icon icon="ep:edit" class="mr-5px" /> 回填发票
      </el-button>
      <div style="color: red;font-size: 13px;margin-left: 1vw">为确保库存数据准确，按照标准管理流程，当月要入库的物资，请当月操作完成，不能跨自然月录入！撤销功能只可当月撤销！</div>
    </el-row>


    <Table2
      v-loading="loading"
      style="margin-top: 10px"
      :data="list"
      selection
      @selection-change="selectionChange"
      :columns="BedChangeColumns"
      :queryParams="queryParams"
      @edit="openForm"
      @detail="openDetail"
      @cancel="handleDelete"
    />

    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <Form ref="formRef" @success="getList" />
<!--  <Detail ref="detailRef" />-->
</template>
<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { getBedChangePage } from '@/api/elderly/elder/bed-change'
import { BedChangeColumns } from '../column'
import { cancelprocessInstance } from '@/api/elderly/common'
import Form from './Form.vue'
import Detail from './Detail.vue'
import { getTenantId } from '@/utils/auth'
import {a} from "../../../../dist/assets/avatar-BG6NdH5s";
defineOptions({ name: 'BedChange' })
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  elderName: undefined,
  bedId: undefined,
  type: undefined,
  tenantId: undefined,
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getBedChangePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleAdd = () => {
  formRef.value.open()

}

const selectionChangeList = ref([])
const selectionChange = (res) => {
  selectionChangeList.value = res
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const backfillHan = async () => {
  try {
    const res = await message.confirm(`确认回填选中的${selectionChangeList.value.length}项`, '提示')
    if (res == 'confirm') {
      try {
        const res = await message.prompt('请输入供应商发票号', '提示')
        if (res.action == 'confirm') {
          // // 发起撤销
          // await cancelprocessInstance({
          //   id: id,
          //   reason: res.value
          // })
          // message.success(t('common.cancelSuccess'))
        }
        // 刷新列表
        await getList()
      } catch {}
    }
  } catch {}
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (row: any = {}) => {
  formRef.value.open(row.id, false)
}

// 详情
const detailRef = ref()
const openDetail = (row: any = {}) => {
  //detailRef.value.open(row.id, row.processInstanceId, row.status)
  formRef.value.open(row.id, true)

}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    const res = await message.prompt('请输入撤销原因', '提示')
    if (res.action == 'confirm') {
      // // 发起撤销
      await cancelprocessInstance({
        id: id,
        reason: res.value
      })
      message.success(t('common.cancelSuccess'))
    }
    // 刷新列表
    await getList()
  } catch {}
}

const route = useRoute()
/** 初始化 **/
onMounted(() => {
  if(route.query && route.query.elderName){
    queryParams.elderName = route.query.elderName as string
  }
  getList()
})
</script>
