<template>
  <div class="page-container column-container">
    <!-- 搜索区 -->
    <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true" style="margin-right: 0px">
      <el-form-item label="单据号：" prop="formCode">
        <el-input v-model="queryParams.formCode" placeholder="请输入单据号" style="width: 144px" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="表单日期：">
        <el-date-picker v-model="queryParams.startTime" type="date" style="width: 144px" value-format="YYYY-MM-DD"
          placeholder="选择起始日期" clearable />
        <span style="margin: 0 4px">到</span>
        <el-date-picker v-model="queryParams.endTime" type="date" style="width: 144px" value-format="YYYY-MM-DD"
          placeholder="选择结束日期" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-button type="primary" icon="Plus" @click="handleShowFormDialog(null)">
        新增
      </el-button>
      <el-button type="danger" icon="Delete" :disabled="ids.length == 0" @click="handleDelete">
        删除
      </el-button>
      <el-button type="warning" icon="Download" :disabled="ids.length != 1" @click="handlePrint">
        打印
      </el-button>
      <el-button type="warning" icon="Download" :disabled="ids.length != 1" @click="handleExport">
        导出
      </el-button>
    </div>
    <!-- 渲染数据区 -->
    <div class="el-table-container">
      <div class="el-table-inner-container">
        <el-table v-loading="loading" :data="dataList" size="small" border height="100%"
          @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="48" align="center" />
          <el-table-column label="收回单号" align="center" prop="formCode" width="120" />
          <el-table-column label="收回日期" align="center" prop="formDate" width="120">
            <template #default="scope">
              {{ parseTime(scope.row.formDate, "{y}-{m}-{d}") }}
            </template>
          </el-table-column>
          <el-table-column label="状态" width="100" align="center">
            <template #default="scope">
              <el-tag v-if="scope.row.isSubmit == 1" class="spacing" type="primary">{{ "已提交" }}</el-tag>
              <el-tag v-else class="spacing" type="danger">{{ "未提交" }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
            <template #default="scope">
              <el-button v-if="scope.row.isAudit == 1" link type="primary" icon="View"
                @click="handleShowFormDialog(scope.row)">
                查看
              </el-button>
              <el-button v-if="scope.row.isSubmit == 0 && scope.row.isAudit == 0" link type="warning" icon="Edit"
                @click="handleShowFormDialog(scope.row)">
                编辑
              </el-button>
              <el-button link v-if="scope.row.isSubmit == 1 && scope.row.isAudit == 0" icon="Close" type="danger"
                @click="handleCancelSubmit(scope.row)">
                取消提交
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 表单 -->
    <receipt-form ref="receiptRef" @handleSaveSuccess="getList" />
  </div>
</template>

<script setup name="Receipt">
import {
  listReceipt,
  delReceipt,
  printReturnReceipt,
  exportReturn,
  updateReceiptSubmitStatus
} from "@/api/business/returnReceipt";
import receiptForm from "./form";
const { proxy } = getCurrentInstance();

const dataList = ref([]);
const loading = ref(true);
const selections = ref([])
const ids = ref([]);
const total = ref(0);
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  formCode: null,
  formDate: null,
  startTime: null,
  endTime: null,
  categroy: 0
});

/***********************  方法区  ****************************/

/** 查询外协单主
带箱方式，是整单的。如果换新箱子，明细中，都需要更换箱子列表 */
function getList() {
  loading.value = true;
  listReceipt(queryParams.value).then((response) => {
    dataList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = ref({
    pageNum: 1,
    pageSize: 10,
    formCode: null,
    formDate: null,
    startTime: null,
    endTime: null,
    category: 0
  });
  handleQuery();
}
/**复选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  selections.value = selection.filter((item) => item.isSubmit == 0);
}
/** 修改按钮操作 */
function handleShowFormDialog(row) {
  if (row != null && row.isAudit == 1) {
    row.editStatus = false;
  }
  proxy.$refs.receiptRef.open(row ? row : null);
}
/** 导出按钮操作 */
function handleExport() {
  exportReturn({ id: ids.value[0] });
}
/** 删除按钮操作 */
function handleDelete() {
  const _ids = ids.value;
  let tempList = []
  let flag = true;
  for (let i = 0; i < ids.value.length; i++) {
    for (let j = 0; j < dataList.value.length; j++) {
      if (dataList.value[j].id == ids.value[i]) {
        tempList.push(dataList.value[j])
      }
    }
  }
  tempList.forEach(item => {
    if (item.isAudit == 1) {
      flag = false;
    }
  })
  console.log(tempList)
  if (flag) {
    proxy.$modal
      .confirm("是否确认删除选中的数据项？")
      .then(function () {
        return delReceipt(_ids);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功！");
      })
      .catch(() => { });
  } else {
    proxy.$modal.msgError("所选的数据项包含已确认的收回单，不能删除！");
  }
}

/** 打印按钮操作 */
function handlePrint() {
  console.log("999")
  if(selections.value.length > 0){
    proxy.$modal.msgError("当前收回单未提交，不能打印！");
  }else{
    printReturnReceipt({ id: ids.value[0] });
  }
}
//取消提交
function handleCancelSubmit(row) {
  updateReceiptSubmitStatus({ id: row.id }).then((response) => {
    proxy.$modal.msgSuccess("修改成功");
    getList()
  });
}

// /** 导出按钮操作 */
// function handleExport() {
//   proxy.download(
//     "business/returnReceipt/export",
//     {
//       ...queryParams.value,
//     },
//     `order_${new Date().getTime()}.xlsx`
//   );
// }
getList();
</script>
