<template>
  <el-card class="mt20">
    <div slot="header">
      <span class="card-title">
        分切后规格
      </span>
    </div>
    <!-- 列表 -->
    <el-table
      v-loading="loading"
      :data="list"
      border
      max-height="400px"
      show-summary
      :summary-method="getSummaries"
    >
      <el-table-column
        label="批次号"
        align="center"
        fixed="left"
        prop="batchNumber"
        min-width="130"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料规格"
        align="center"
        prop="size"
        min-width="80"
        show-overflow-tooltip
      />
      <el-table-column
        label="单位"
        align="center"
        prop="unitCode"
        min-width="60"
        show-overflow-tooltip
      />
      <el-table-column
        label="长度(m)"
        align="center"
        prop="length"
        min-width="60"
      >
        <template slot-scope="scope">
          <span v-if="paramsQuery.splitType === '0'">{{ scope.row.length }}</span>
          <el-button v-else type="text" @click="handleAccurate(scope.row)">{{ scope.row.length }}</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="宽度(mm)"
        align="center"
        prop="width"
        min-width="60"
        show-overflow-tooltip
      />
      <el-table-column
        label="厚度(mm)"
        align="center"
        prop="thickness"
        min-width="60"
        show-overflow-tooltip
      />
      <el-table-column
        label="批次宽度(mm)"
        align="center"
        prop="widthLots"
        min-width="80"
        show-overflow-tooltip
      />
      <el-table-column
        label="分切面积(㎡)"
        align="center"
        prop="qty"
        min-width="80"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料批次"
        align="center"
        prop="materialLots"
        min-width="108"
        show-overflow-tooltip
      />
      <el-table-column
        label="生产日期"
        align="center"
        prop="produceDate"
        min-width="100"
        show-overflow-tooltip
      />
      <el-table-column
        label="可用状态"
        align="center"
        prop="status"
        min-width="80"
      >
        <template slot-scope="scope">
          <!-- <dict-tag :type="DICT_TYPE.WMS_SLITTING_MATERIAL_STATUS" :value="scope.row.status" /> -->
          <el-button
            v-if="scope.row.status === '1'"
            v-hasPermi="['wms:label-master-slit-plan:update']"
            type="success"
            size="mini"
            plain
            @click="handleToLoss(scope.row)"
          >可用</el-button>
          <!-- <el-button
            v-else
            type="danger"
            size="mini"
            plain
          >损耗</el-button> -->
          <el-tag v-else type="danger" style="padding: 0 15px;">损耗</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="100"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['wms:label-master-slit-plan:delete']"
            type="text"
            class="text-danger"
            size="mini"
            icon="el-icon-delete"
            :disabled="queryParams.slitStatus === '2'"
            @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"
    /> -->
    <!-- 精分 -->
    <AccurateSplit ref="accurateSplit" @saveAccurate="getList" />
  </el-card>
</template>

<script>
import { getSlittedLotsPage, slitPlanToLoss } from '@/api/wms/inLibrary/slittingOrder'
import AccurateSplit from './AccurateSplit.vue'

export default {
  name: 'SlittingOrder',
  components: {
    AccurateSplit
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分切任务单列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        eBillNo: null,
        alitStatus: null,
        createTime: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.queryParams.eBillNo = this.$route.query.ebillNo
    this.queryParams.slitStatus = this.$route.query.slitStatus
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getSlittedLotsPage(this.queryParams).then(response => {
        this.list = response.data
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        eBillNo: undefined,
        alitStatus: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 小数问题处理
    getHandleNumber(val) {
      return Math.round((parseFloat(val) * 10000000)) / 10000000
    },
    // 总计
    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 === 'width') {
          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
    },
    // 删除
    handleDelete(row) {
      this.$emit('deleteRow', row)
    },
    // 精分
    handleAccurate(row) {
      this.$refs.accurateSplit.queryParams = row
      this.$refs.accurateSplit.open = true
    },
    // 设为损耗
    handleToLoss(row) {
      this.$modal.confirm('确认是否要将该物料设为损耗吗?').then(() => {
        const { id } = row
        const params = {
          id
        }
        slitPlanToLoss(params).then(() => {
          this.$modal.msgSuccess('操作成功!')
          this.getList()
        })
      }).catch(() => {
        this.$modal.msg('您已取消操作')
      })
    }
  }
}
</script>

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