<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <QueryFormWrapper>
        <el-col :span="8">
          <el-form-item label="行号" prop="lineNo">
            <el-input
              v-model="queryParams.lineNo"
              placeholder="请输入行号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="物料编码" prop="materialNo">
            <el-input
              v-model="queryParams.materialNo"
              placeholder="请输入物料编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="物料名称" prop="materialName">
            <el-input
              v-model="queryParams.materialName"
              placeholder="请输入物料名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <div slot="right">
          <el-form-item label-width="0" class="text-right">
            <EasyAdvancedQuery
              class="mr10"
              label-width="98px"
              @advQuery="advQuery"
            >
              <template #default="{ model }">
                <el-col :span="12">
                  <el-form-item label="物料规格" prop="materialSpec">
                    <el-input
                      v-model="queryParams.materialSpec"
                      placeholder="请输入物料规格"
                      clearable
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="批号" prop="batchNumber">
                    <el-input
                      v-model="queryParams.batchNumber"
                      placeholder="请输入批号"
                      clearable
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="单位" prop="unit">
                    <el-input
                      v-model="queryParams.unit"
                      placeholder="请输入单位"
                      clearable
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="计划数量" prop="planQty">
                    <el-input
                      v-model="queryParams.planQty"
                      placeholder="请输入计划数量"
                      clearable
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="实际数量" prop="actualQty">
                    <el-input
                      v-model="queryParams.actualQty"
                      placeholder="请输入实际数量"
                      clearable
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="已完成数量" prop="completedQty">
                    <el-input
                      v-model="queryParams.completedQty"
                      placeholder="请输入已完成数量"
                      clearable
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="质量状态" prop="qualityStatus">
                    <el-select
                      v-model="queryParams.qualityStatus"
                      placeholder="请选择质量状态"
                      clearable
                      size="small"
                    >
                      <el-option label="请选择字典生成" value="" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="生产日期" prop="productionDate">
                    <el-date-picker
                      v-model="queryParams.productionDate"
                      style="width: 240px"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      type="daterange"
                      range-separator="-"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      :default-time="['00:00:00', '23:59:59']"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="失效日期" prop="expirationDate">
                    <el-date-picker
                      v-model="queryParams.expirationDate"
                      style="width: 240px"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      type="daterange"
                      range-separator="-"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      :default-time="['00:00:00', '23:59:59']"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="库存状态" prop="inventoryStatus">
                    <el-select
                      v-model="queryParams.inventoryStatus"
                      placeholder="请选择库存状态"
                      clearable
                      size="small"
                    >
                      <el-option label="请选择字典生成" value="" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="源明细行号" prop="sourceLineNo">
                    <el-input
                      v-model="queryParams.sourceLineNo"
                      placeholder="请输入源明细行号"
                      clearable
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="源批次号" prop="sourceBatchNumber">
                    <el-input
                      v-model="queryParams.sourceBatchNumber"
                      placeholder="请输入源批次号"
                      clearable
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="行备注" prop="remark">
                    <el-input
                      v-model="queryParams.remark"
                      placeholder="请输入行备注"
                      clearable
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="行状态" prop="status">
                    <el-select
                      v-model="queryParams.status"
                      placeholder="请选择行状态"
                      clearable
                      size="small"
                    >
                      <el-option label="请选择字典生成" value="" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="创建时间" prop="createTime">
                    <el-date-picker
                      v-model="queryParams.createTime"
                      style="width: 240px"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      type="daterange"
                      range-separator="-"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      :default-time="['00:00:00', '23:59:59']"
                    />
                  </el-form-item>
                </el-col>
              </template>
            </EasyAdvancedQuery>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </div>
      </QueryFormWrapper>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['rtkwms:inout-request-detail:create']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openForm(undefined)"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['rtkwms:inout-request-detail:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <el-table-column label="行号" align="center" prop="lineNo" />
      <el-table-column label="物料编码" align="center" prop="materialNo" />
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="物料规格" align="center" prop="materialSpec" />
      <el-table-column label="批号" align="center" prop="batchNumber" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="计划数量" align="center" prop="planQty" />
      <el-table-column label="实际数量" align="center" prop="actualQty" />
      <el-table-column label="已完成数量" align="center" prop="completedQty" />
      <el-table-column label="质量状态" align="center" prop="qualityStatus" />
      <el-table-column label="生产日期" align="center" prop="productionDate" />
      <el-table-column label="失效日期" align="center" prop="expirationDate" />
      <el-table-column label="库存状态" align="center" prop="inventoryStatus" />
      <el-table-column label="源明细行号" align="center" prop="sourceLineNo" />
      <el-table-column
        label="源批次号"
        align="center"
        prop="sourceBatchNumber"
      />
      <el-table-column label="行备注" align="center" prop="remark" />
      <el-table-column label="行状态" align="center" prop="status" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template v-slot="scope">
          <el-button
            v-hasPermi="['rtkwms:inout-request-detail:update']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openForm(scope.row.id)"
            >修改</el-button
          >
          <el-button
            v-hasPermi="['rtkwms:inout-request-detail:delete']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 对话框(添加 / 修改) -->
    <InoutRequestDetailForm ref="formRef" @success="getList" />
  </div>
</template>

<script>
import * as InoutRequestDetailApi from "@/api/wms/output/inoutrequestdetail";
import InoutRequestDetailForm from "./components/InoutRequestDetailForm.vue";
import QueryFormWrapper from "@/components/QueryFormWrapper";
import EasyAdvancedQuery from "@/components/EasyAdvancedQuery";
export default {
  name: "InoutRequestDetail",
  components: {
    InoutRequestDetailForm,
    QueryFormWrapper,
    EasyAdvancedQuery,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出入库申请明细列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        requestId: null,
        lineNo: null,
        materialNo: null,
        materialName: null,
        materialSpec: null,
        batchNumber: null,
        unit: null,
        planQty: null,
        actualQty: null,
        completedQty: null,
        qualityStatus: null,
        productionDate: [],
        expirationDate: [],
        inventoryStatus: null,
        sourceLineNo: null,
        sourceBatchNumber: null,
        sourceReference: null,
        remark: null,
        status: null,
        createTime: [],
        stockCode: null,
        areaCode: null,
        locationCode: null,
        sourceRequestId: null,
        sourceDetailId: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 高级查询处理 */
    advQuery(advQueryParams) {
      this.advParams = advQueryParams;
      this.handleQuery();
    },

    /** 查询列表 */
    async getList() {
      this.loading = true;
      const params = { ...this.queryParams, ...this.advParams };
      await InoutRequestDetailApi.getInoutRequestDetailPage(params)
        .then((res) => {
          this.list = res.data.list;
          this.total = res.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      // 重置表单
      if (this.$refs.queryForm) {
        this.$refs.queryForm.resetFields();
      }
      // 重置查询参数
      Object.assign(this.queryParams, {
        pageNo: 1,
        pageSize: 10,
        requestId: null,
        lineNo: null,
        materialNo: null,
        materialName: null,
        materialSpec: null,
        batchNumber: null,
        unit: null,
        planQty: null,
        actualQty: null,
        completedQty: null,
        qualityStatus: null,
        productionDate: [],
        expirationDate: [],
        inventoryStatus: null,
        sourceLineNo: null,
        sourceBatchNumber: null,
        sourceReference: null,
        remark: null,
        status: null,
        createTime: [],
        stockCode: null,
        areaCode: null,
        locationCode: null,
        sourceRequestId: null,
        sourceDetailId: null,
      });
      this.handleQuery();
    },

    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },

    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm(
        '是否确认删除出入库申请明细编号为"' + id + '"的数据项?'
      );
      try {
        await InoutRequestDetailApi.deleteInoutRequestDetail(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {}
    },

    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm("是否确认导出所有出入库申请明细数据项?");
      try {
        this.exportLoading = true;
        // 处理导出参数，与getList保持一致
        const params = { ...this.queryParams };

        // 处理日期范围参数
        const handleDateRange = (dateRange) => {
          if (Array.isArray(dateRange) && dateRange.length === 2) {
            return {
              beginTime: dateRange[0],
              endTime: dateRange[1],
            };
          }
          return {};
        };

        // 合并高级查询参数
        const exportParams = {
          ...params,
          ...handleDateRange(
            params.productionDate
              ? {
                  productionBeginDate: params.productionDate[0],
                  productionEndDate: params.productionDate[1],
                }
              : {}
          ),
          ...handleDateRange(
            params.expirationDate
              ? {
                  expirationBeginDate: params.expirationDate[0],
                  expirationEndDate: params.expirationDate[1],
                }
              : {}
          ),
          ...handleDateRange(
            params.createTime
              ? {
                  createBeginTime: params.createTime[0],
                  createEndTime: params.createTime[1],
                }
              : {}
          ),
        };

        // 删除原始日期范围数组
        delete exportParams.productionDate;
        delete exportParams.expirationDate;
        delete exportParams.createTime;

        const data = await InoutRequestDetailApi.exportInoutRequestDetailExcel(
          exportParams
        );
        this.$download.excel(data, "出入库申请明细.xls");
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
  },
};
</script>
