<!-- 首序外协列表 -->
<template>
  <div class="page-container column-container">
    <!-- 搜索区 -->
    <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true">
      <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="表单日期：" prop="formDate">
        <el-date-picker v-model="queryParams.formDate" type="date" style="width: 144px" clearable
          value-format="YYYY-MM-DD" placeholder="请选择表单日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="外协商名称：" prop="supplierName">
        <el-input v-model="queryParams.supplierName" placeholder="请输入关键字" style="width: 144px" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="送货方式：" prop="deliveryMethod">
        <el-select v-model="queryParams.deliveryMethod" style="width: 144px" clearable placeholder="请选择">
          <el-option v-for="dict in delivery_method" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="带箱方式：" prop="packagingMethod">
        <el-select v-model="queryParams.packagingMethod" style="width: 144px" clearable placeholder="请选择">
          <el-option v-for="dict in packaging_method" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </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="handleAdd" v-hasPermi="['business:firstOutsource:add']">
        新增
      </el-button>
      <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete"
        v-hasPermi="['business:firstOutsource:remove']">
        删除
      </el-button>
      <el-button type="warning" icon="Download" :disabled="ids.length != 1" @click="handleExport"
        v-hasPermi="['business:firstOutsource:export']">
        导出
      </el-button>
      <el-button type="warning" icon="Download" :disabled="ids.length != 1" @click="handlePrint"
        v-hasPermi="['business:firstOutsource:export']">
        打印
      </el-button>
    </div>

    <!-- 渲染数据区 -->
    <div class="el-table-container">
      <div class="el-table-inner-container">
        <el-table v-loading="loading" :data="orderList" 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="外协商名称" align="center" prop="supplierName" width="320" />
          <el-table-column label="结算方式" align="center" prop="settlementType" width="120">
            <template #default="scope">
              <dict-tag :options="settlement_type" :value="scope.row.settlementType" />
            </template>
          </el-table-column>
          <el-table-column label="送货方式" align="center" prop="deliveryMethod" width="120">
            <template #default="scope">
              <dict-tag :options="delivery_method" :value="scope.row.deliveryMethod" />
            </template>
          </el-table-column>
          <el-table-column label="带箱方式" align="center" prop="packagingMethod" width="120">
            <template #default="scope">
              <dict-tag :options="packaging_method" :value="scope.row.packagingMethod" />
            </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="200">
            <template #default="scope">
              <el-button v-if="scope.row.isSubmit == 1" link type="warning" icon="Edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['business:firstOutsource:edit']">
                编辑
              </el-button>
              <el-button link type="primary" icon="View" @click="handleView(scope.row)"
                v-hasPermi="['business:firstOutsource:query']">
                查看
              </el-button>
              <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"
                v-hasPermi="['business:firstOutsource:remove']">
                删除
              </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" />

    <!-- 表单 -->
    <order-form ref="orderRef" :get-list="getList" :delivery-method="delivery_method" :settlement-type="settlement_type"
      :packaging-method="packaging_method" />
    <!-- 打印箱码 -->
    <div id="image-group" style="height: 0px; width: 0px">
      <el-image v-for="item in printCarriers" :key="item.id" :src="webHost + item.qcCode" fit="cover"
        style="width: 100mm; height: 100mm">
      </el-image>
    </div>
  </div>
</template>

<script setup name="FirstOutsource">
import {
  listOrder,
  delOrderForFirst,
  exportOutsource,
  printOutsource,
  getCarrierInfo
} from "@/api/business/outsourcedOrder";
import orderForm from "./form";
import printJS from "print-js";
import { nextTick } from "vue";
const { proxy } = getCurrentInstance();
/** 字典数组区 */
const { delivery_method } = proxy.useDict("delivery_method");
const { settlement_type } = proxy.useDict("settlement_type");
const { packaging_method } = proxy.useDict("packaging_method");

const orderList = ref([]);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const printCarriers = ref([])
const webHost = import.meta.env.VITE_APP_PRODUCTION_API;
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  formCode: null,
  formDate: null,
  startTime: null,
  endTime: null,
  supplierName: null,
  deliveryMethod: null,
  packagingMethod: null,
  freightPrice: null,
  freightAmount: null,
  isFirstProcess: 1
});

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

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

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs.orderRef.open();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const id = row.id || ids.value;
  row.flag = true
  proxy.$refs.orderRef.open(row);
}
function handleView(row) {
  const id = row.id || ids.value;
  row.flag = false
  proxy.$refs.orderRef.open(row);
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm("是否确认删除选中的数据项？")
    .then(function () {
      return delOrderForFirst(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功！");
    })
    .catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  exportOutsource({ id: ids.value[0] });
}

/** 导出按钮操作 */
function handlePrint() {
  printOutsource({ id: ids.value[0] });
}
/** 打印箱码 */
function handlePrintCarrierCode() {
  // 获取箱码地址
  if (ids.value.length === 0) {
    // 判断是否选中行
    proxy.$modal.msgWarning('请选择需要打印箱码的外协单')
    return
  }
  getCarrierInfo(ids.value).then(res => {
    if (res.code === 200) {
      printCarriers.value = res.data
      nextTick(_ => {
        if (printCarriers.value.length == 0) {
          proxy.$modal.msgWarning('选中外协单无可打印箱码')
          return
        }
        const imageGroup = document.getElementById("image-group");
        printJS({
          printable: "image-group", // 这里是你要打印内容的DOM元素的id
          type: "html",
          style: "@page { size: auto;  margin: 0mm; }", // 可以添加打印样式
        });
      })
    }
  })
}

getList();
</script>
