<template>
  <el-card class="cards">
    <div slot="header">
      <span class="card-title"> 子卷信息 </span>
    </div>
    <el-row :gutter="10" class="mb5">
      <el-col :span="24" class="text-right">
        <el-button
          v-hasPermi="['wms:split-order-detail:delete']"
          type="danger"
          size="mini"
          plain
          icon="el-icon-delete"
          class="text-danger"
          @click="handleDelete()"
          >删除</el-button
        >
      </el-col>
    </el-row>
    <!-- 列表 -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="list"
      border
      max-height="320px"
      show-summary
      :summary-method="getSummaries"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" fixed="left" />
      <el-table-column
        label="条码号"
        align="center"
        prop="batchNumber"
        width="210"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        label="条码号"
        align="center"
        prop="qrCodeOld"
        fixed="left"
        min-width="140"
        show-overflow-tooltip
      /> -->
      <el-table-column
        label="单据日期"
        align="center"
        prop="createTime"
        width="160"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="子条码"
        align="center"
        prop="qrCode"
        min-width="140"
        show-overflow-tooltip
      /> -->
      <el-table-column
        label="物料编码"
        align="center"
        prop="materialNo"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料名称"
        align="center"
        prop="materialName"
        width="210"
        show-overflow-tooltip
      />
      <el-table-column
        label="规格型号"
        align="center"
        prop="size"
        width="210"
        show-overflow-tooltip
      />
      <el-table-column
        label="来料批次"
        align="center"
        prop="materialLots"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        label="总宽度/米数"
        align="center"
        prop="sumWidth"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        label="数量"
        align="center"
        prop="qty"
        width="80"
        show-overflow-tooltip
      />
      <el-table-column
        label="宽度(mm)"
        align="center"
        prop="width"
        width="60"
        show-overflow-tooltip
      />
      <el-table-column
        label="长度(m)"
        align="center"
        prop="length"
        width="60"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <!-- <span v-if="paramsQuery.splitType === '0'">{{ scope.row.length }}</span> -->
          <el-button type="text" @click="handleAccurate(scope.row)">{{
            scope.row.length
          }}</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="面积"
        align="center"
        prop="area"
        width="60"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        label="批号"
        align="center"
        prop="batchNumber"
        width="100"
        show-overflow-tooltip
      /> -->
      <!-- <el-table-column
        label="中间仓"
        align="center"
        prop="midbin"
        width="120"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.midbin * 1 === 1"
            v-hasPermi="['wms:split-order-detail:update']"
            type="success"
            size="mini"
            plain
            @click="handlePutBack({ id: scope.row.id, midbin: 0 })"
            >放回原仓库</el-button
          >
          <el-button
            v-else
            v-hasPermi="['wms:split-order-detail:update']"
            type="primary"
            size="mini"
            plain
            @click="handlePutBack({ id: scope.row.id, midbin: 1 })"
            >放回中间仓</el-button
          >
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="是否损耗"
        align="center"
        prop="lossFlag"
        width="80"
        show-overflow-tooltip
      >
        <template slot-scope="scope"> -->
      <!-- <dict-tag :type="DICT_TYPE.WMS_SLITTING_MATERIAL_STATUS" :value="scope.row.status" /> -->
      <!-- <el-button
            v-if="scope.row.lossFlag === 1"
            v-hasPermi="['wms:split-order-detail:update']"
            type="success"
            size="mini"
            plain
            @click="handleShowLossDialog(scope.row)"
          >可用</el-button>
          <el-tag v-else type="danger" style="padding: 0 15px;">损耗</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" fixed="right" width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewLogs(scope.row)"
            >操作日志</el-button
          >
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="80"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['wms:split-order-detail:delete']"
            type="text"
            size="mini"
            plain
            class="text-danger"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>

      </el-table-column> -->
    </el-table>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="25%">
      <DictSelect
        v-model="currentRow.lossType"
        placeholder="请选择损耗状态"
        dict-type="SPLIT_LOSS_TYPE"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleToLoss">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 操作日志 -->
    <ViewLogs ref="viewLogs" />
    <AccurateDemergeVue ref="accurateDemergeVue" @saveAccurate="getList" />
  </el-card>
</template>

<script>
import {
  toStock,
  createSplitDetailToloss,
  deleteSplitOrderDetail,
} from "@/api/wms/inLibrary/demergeOrder";
import AccurateDemergeVue from "./AccurateDemerge.vue";
import ViewLogs from "./ViewLogs.vue";

export default {
  name: "SecondaryMaterials",
  components: {
    ViewLogs,
    AccurateDemergeVue,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 设为损耗弹框状态
      dialogVisible: false,
      // 总条数
      total: 0,
      // 子卷信息列表
      list: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 当前选中当前行数据
      currentRow: {
        id: undefined,
        lossType: undefined,
      },
      // 当前选中
      currentRows: [],
    };
  },
  created() {},
  methods: {
    getList() {
      this.$emit("refresh");
    },
    handleShowLossDialog(data) {
      this.dialogVisible = true;
      Object.assign(this.currentRow, data);
    },
    // 小数点问题处理
    getHandleNumber(val) {
      return Math.round(parseFloat(val) * 100000000) / 100000000;
    },
    // 总计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总计";
          return;
        }

        const values = data.map((item) => Number(item[column.property]));
        if (column.property === "qty") {
          sums[index] = values.reduce((acc, cur) => {
            const value = Number(cur);
            if (!isNaN(value)) {
              return this.getHandleNumber(acc + cur);
            } else {
              return acc;
            }
          }, 0);
          sums[index];
        }
      });
      return sums;
    },
    // 设为损耗
    handleToLoss() {
      const { id, lossType } = this.currentRow;
      createSplitDetailToloss({ id, lossType }).then(() => {
        this.dialogVisible = false;
        this.$modal.msgSuccess("操作成功!");
        this.$emit("toLossSuccess");
      });
      // this.$modal.confirm('确认是否要将该物料设为损耗吗?').then(() => {
      //   const { id, lossType } = this.currentRow
      //   createSplitDetailToloss({ id, lossType }).then(() => {
      //     this.dialogVisible = false
      //     this.$modal.msgSuccess('操作成功!')
      //     this.$emit('toLossSuccess')
      //   })
      // }).catch(() => {
      //   this.$modal.msg('您已取消操作')
      // })
    },
    handlePutBack({ id, midbin }) {
      this.$modal
        .confirm(
          midbin
            ? "确认是否要将该物料放回中间仓？"
            : "确认是否要将该物料放回原仓库？"
        )
        .then(() => {
          toStock({ id, midbin }).then(() => {
            this.dialogVisible = false;
            this.$modal.msgSuccess("操作成功!");
            this.$emit("toLossSuccess");
          });
        })
        .catch(() => {
          this.$modal.msg("您已取消操作");
        });
    },
    // 二次拆分
    handleAccurate(row) {
      this.$refs.accurateDemergeVue.queryParams = row;
      this.$refs.accurateDemergeVue.open = true;
    },
    // 选中的数据
    handleSelectionChange(val) {
      this.currentRows = val;
    },
    // 删除
    async handleDelete() {
      if (this.currentRows.length === 0) {
        this.$modal.msgWarning("请选择您要删除的数据!");
        return;
      }
      await this.$modal.confirm("是否确认删除当前选中的数据项?");
      try {
        const ids = this.currentRows.map((item) => item.id);
        const params = {
          ids: ids?.join(","),
        };
        await deleteSplitOrderDetail(params);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch {
        //
      }
    },
    // 查看日志
    handleViewLogs(row) {
      const { id } = row;
      this.$refs.viewLogs.queryParams.businessId = id;
      this.$refs.viewLogs.open = true;
    },
  },
};
</script>

<style scoped>
.card-title {
  font-size: 14px;
}
</style>
