<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="12">
        <!-- 入库 -->
        <h3 style="text-align: center; margin-bottom: 10px;">新增入库</h3>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="入场钢卷号" prop="enterCoilNo">
                <el-input v-model="form.enterCoilNo" placeholder="请输入入场钢卷号" :disabled="form.coilId" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="当前钢卷号" prop="currentCoilNo">
                <el-input v-model="form.currentCoilNo" placeholder="请输入当前钢卷号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item v-if="!form.coilId" label="厂家原料卷号" prop="supplierCoilNo">
                <el-input v-model="form.supplierCoilNo" placeholder="请输入厂家原料卷号" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="所在库位" prop="warehouseId">
                <warehouse-select v-model="form.warehouseId" placeholder="请选择仓库/库区/库位" style="width: 100%;" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="实际库区" prop="actualWarehouseId">
                <actual-warehouse-select v-model="form.actualWarehouseId" placeholder="请选择实际库区" style="width: 100%;"
                  clearable />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="班组" prop="team">
                <el-input v-model="form.team" placeholder="请输入班组" />
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="材料类型" prop="materialType" v-if="!form.coilId">
                <el-select v-model="form.materialType" placeholder="请选择材料类型" @change="handleMaterialTypeChange">
                  <el-option label="成品" value="成品" />
                  <el-option label="原料" value="原料" />
                  <el-option label="废品" value="废品" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="getItemLabel" prop="itemId" v-if="!form.coilId">
                <product-select v-if="form.itemType == 'product'" v-model="form.itemId" placeholder="请选择成品"
                  style="width: 100%;" clearable />
                <raw-material-select v-else-if="form.itemType == 'raw_material'" v-model="form.itemId"
                  placeholder="请选择原料" style="width: 100%;" clearable />
                <div v-else>请先选择材料类型</div>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row v-if="form.materialType === '成品'">
             <el-col :span="12">
              <el-form-item v-if="form.materialType === '成品'" label="质量状态" prop="qualityStatus">
                <el-input v-model="form.qualityStatus" placeholder="请输入质量状态" :disabled="readonly">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item v-if="form.materialType === '成品'" label="切边要求" prop="trimmingRequirement">
                <el-input v-model="form.trimmingRequirement" placeholder="请输入切边要求" :disabled="readonly">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item v-if="form.materialType === '成品'" label="打包状态" prop="packingStatus">
                <el-input v-model="form.packingStatus" placeholder="请输入打包状态" :disabled="readonly">
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item v-if="form.materialType === '成品'" label="包装要求" prop="packagingRequirement">
                <el-input v-model="form.packagingRequirement" placeholder="请输入包装要求" :disabled="readonly">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="毛重" prop="grossWeight">
                <el-input v-model="form.grossWeight" placeholder="请输入毛重" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="净重" prop="netWeight">
                <el-input v-model="form.netWeight" placeholder="请输入净重" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item>
            <el-button :loading="buttonLoading" type="primary" @click="submitForm">入 库</el-button>
            <el-button @click="cancel">重 置</el-button>
          </el-form-item>
        </el-form>
      </el-col>

      <el-col :span="12">
        <!-- 入库记录 -->
        <h3 style="text-align: center;">入库记录</h3>
        <el-table v-loading="loading" height="400" :data="pendingActions" title="入库记录">
          <el-table-column label="钢卷号" align="center" prop="currentCoilNo" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-tag type="info" size="small">{{ scope.row.currentCoilNo }}</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="优先级" align="center" prop="priority" width="90">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.priority === 0" type="info" size="mini">普通</el-tag>
              <el-tag v-else-if="scope.row.priority === 1" type="warning" size="mini">重要</el-tag>
              <el-tag v-else-if="scope.row.priority === 2" type="danger" size="mini">紧急</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="创建人" align="center" prop="createBy" width="100" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="155" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
            </template>
          </el-table-column>

          <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
        </el-table>

        <Pagination v-show="total > 0" :total="total" :page.sync="pagination.currentPage"
          :limit.sync="pagination.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { addMaterialCoil } from '@/api/wms/coil'
import { listPendingAction, addPendingAction } from '@/api/wms/pendingAction'
import MaterialSelect from "@/components/KLPService/MaterialSelect";
import ActualWarehouseSelect from "@/components/KLPService/ActualWarehouseSelect";
import WarehouseSelect from "@/components/KLPService/WarehouseSelect";
import ProductSelect from "@/components/KLPService/ProductSelect";
import RawMaterialSelect from "@/components/KLPService/RawMaterialSelect";
import { Pagination } from 'element-ui';

// 键值为[400, 500), 不包含在字典中，但后续可以加入的特殊操作，这类操作录入后会立刻完成，例如入库（401）和发货（402）

export default {
  components: {
    MaterialSelect,
    ActualWarehouseSelect,
    WarehouseSelect,
    ProductSelect,
    RawMaterialSelect,
  },
  data() {
    return {
      pendingActions: [],
      // 表单参数
      form: {},
      loading: false,
      buttonLoading: false,
      // 分页参数
      pagination: {
        currentPage: 1,
        pageSize: 10,
      },
      total: 0,
      // 表单校验
      rules: {
        enterCoilNo: [
          { required: true, message: "入场钢卷号不能为空", trigger: "blur" }
        ],
        currentCoilNo: [
          { required: true, message: "当前钢卷号不能为空", trigger: "blur" }
        ],
        itemId: [
          { required: true, message: "物品ID不能为空", trigger: "blur" }
        ],
        itemType: [
          { required: true, message: "物品类型不能为空", trigger: "change" }
        ],
        // 净重和毛重
        netWeight: [
          { required: true, message: "净重不能为空", trigger: "blur" }
        ],
        grossWeight: [
          { required: true, message: "毛重不能为空", trigger: "blur" }
        ],
      },
    }
  },
  mounted() {
    this.getList()
  },
  computed: {
    // 动态显示标签
    getItemLabel() {
      if (this.form.materialType === '成品') {
        return '产品类型';
      } else if (this.form.materialType === '原料' || this.form.materialType === '废品') {
        return '原料类型';
      }
      return '物品类型';
    },
  },
  methods: {
    // 处理材料类型变化
    handleMaterialTypeChange(value) {
      // 清空物品选择
      this.form.itemId = null;
      // 根据材料类型设置物品类型
      if (value === '成品') {
        this.form.itemType = 'product';
      } else if (value === '原料' || value === '废品') {
        this.form.itemType = 'raw_material';
      }
    },
    getList() {
      // 获取入库历史
      this.loading = true
      listPendingAction({ actionType: 401, ...this.pagination }).then(res => {
        this.pendingActions = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          addMaterialCoil(this.form).then(response => {
            addPendingAction({
              actionType: 401,
              currentCoilNo: this.form.currentCoilNo,
              coilId: response.data.coilId,
              priority: 0,
              remark: this.form.remark,
              actionStatus: 2,
            }).then(res => {
              this.$modal.msgSuccess("入库成功");
              this.getList()
            })
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    // 取消操作
    cancel() {
      this.form = {};
      this.$refs.form.resetFields();
      this.buttonLoading = false;
    }
  }
}
</script>