<template>
  <div class="body">
    <!-- 返工 -->
    <el-scrollbar class="containerBox">
      <div
        style="margin-bottom: 10px; display: flex"
        :style="{ justifyContent: !formStatus ? 'center' : '' }"
      >
        <el-button
          type="primary"
          style="font-size: 16px"
          v-if="formStatus"
          @click="toAdd"
          >新增</el-button
        >
        <el-button type="primary" v-else @click="toList">返回</el-button>
      </div>

      <template v-if="formStatus">
        <el-table class="table" :data="tableData">
          <el-table-column label="物料名称" prop="materialName" />
          <el-table-column label="管号" prop="seqNos" />
          <el-table-column label="数量" prop="num" />
          <el-table-column label="返工原因" prop="reason" />
          <el-table-column label="返工内容" prop="remark" />
        </el-table>
        <Pagination
          v-model:limit="limit"
          v-model:page="page"
          :position="'right'"
          :total="total"
          @pagination="getPagination"
        />
      </template>
      <template v-else>
        <el-scrollbar class="form">
          <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules">
            <el-form-item
              :label="`流转卡号数量[${ruleForm.seqList.length}]`"
              prop="seqList"
              :rules="{
                required: true,
                trigger: 'change',
              }"
            >
              <el-select
                v-model="ruleForm.seqList"
                multiple
                placeholder="请选择"
                value-key="value"
                @change="selectProcessWorkSeqChange"
              >
                <el-option
                  v-for="item in infoData?.processWorkSeq"
                  :key="item"
                  :label="item"
                  :value="item"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              label="返工工序"
              prop="itemList"
              :rules="{
                required: true,
                trigger: 'burl',
              }"
            >
              <el-select
                v-model="ruleForm.itemList"
                multiple
                placeholder="请选择"
                value-key="operationId"
              >
                <el-option
                  v-for="item in operationList"
                  :key="item.operationId"
                  :label="item.operationName"
                  :value="item"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="选择返工信息(可选)">
              <el-select v-model="infoIndex" placeholder="请选择" filterable>
                <el-option
                  v-for="(item, index) in ReworkTempList"
                  :key="item.id"
                  :label="item.code"
                  :value="index"
                />
              </el-select>
            </el-form-item>
            <el-form-item
              label="返工原因"
              prop="reason"
              :rules="{
                required: true,
                trigger: 'change',
              }"
            >
              <el-input v-model="ruleForm.reason" />
            </el-form-item>
            <el-form-item
              label="返工内容"
              prop="remark"
              :rules="{
                required: true,
                trigger: 'change',
              }"
            >
              <el-input v-model="ruleForm.remark" />
            </el-form-item>
          </el-form>
        </el-scrollbar>
        <div class="btns">
          <el-button type="primary" @click="submit">提交</el-button>
        </div>
      </template>
    </el-scrollbar>
  </div>
</template>

<script setup>
import {
  breakReportInfoById,
  operationListByIds,
} from "@/api/process/reportBreak";
import {
  addRework,
  addRework1,
  getList,
  getList1,
  delError,
  getReworkList,
} from "@/api/rework";
import { useProcessStore } from "@/store/modules/processView";
import { useDictionaryStore } from "@/store/modules/dictionary";
const dictStroe = useDictionaryStore();
const processStore = useProcessStore();
const infoIndex = ref(null);
const ruleForm = ref({
  seqList: [],
});
const ruleForm1 = ref({
  seqs: [],
  remark1: {
    content: "",
    user1: "",
  },
  remark2: {
    user2: "",
  },
});
const ReworkTempList = ref([]);
//获取返工列表模版数据
const getReworkTempList = async () => {
  const { data } = await getReworkList({
    pageNo: 1,
    pageSize: 9999,
  });

  ReworkTempList.value = data;
  console.log(ReworkTempList.value);
};
const tableData = ref([]);
const toAdd = () => {
  formStatus.value = false;
  breakReportInfoById(processStore.scanInfo.id).then((res) => {
    infoData.value = res.data;
  });
};
const toList = () => {
  resetForm();
  formStatus.value = true;
};
const resetForm = () => {
  page.value = 1;
  limit.value = 10;
  ruleForm.value = { seqList: [] };
  ruleForm1.value = {
    seqs: [],
    remark1: {
      content: "",
      user1: "",
    },
    remark2: {
      user2: "",
    },
  };
};
const getPagination = async () => {
  const { data } = await getList({
    pageNo: page.value,
    pageSize: limit.value,
    processId: processStore.scanInfo.id,
  });
  tableData.value = data.records;
  total.value = data.totalCount;
};
const getPagination1 = async () => {
  const { data } = await getList1({
    pageNo: page.value,
    pageSize: limit.value,
  });
  tableData.value = data.records;
};
const formStatus = ref(true);
const submit = async () => {
  ruleFormRef.value.validate((valid) => {
    if (valid) {
      addTask();
    } else {
      ElMessage.error("请检查是否有未填项");
    }
  });
};

const addTask = async () => {
  const { code } = await addRework({
    ...ruleForm.value,
    materialCode: processStore.scanInfo.materialCode,
    materialName: processStore.scanInfo.materialName,
    operationId: processStore.scanInfo.operationId,
    operationName: processStore.scanInfo.operationName,
    processId: processStore.scanInfo.id,
    workOrderCode: processStore.odersData.workOrderCode,
    materialModel: processStore.processInfo.materialModel,
  });
  if (code == "200") {
    resetForm();
    formStatus.value = true;
    getPagination();
    ElMessage.success("操作成功！");
  }
};

const ruleFormRef = ref(null);

const total = ref(0);
const page = ref(1);
const limit = ref(10);
const operationList = ref([]);
const infoData = ref({});
const rules = reactive({
  location: [
    {
      required: true,
      message: "Please select a location",
      trigger: "change",
    },
  ],
});

const selectProcessWorkSeqChange = () => {
  operationListByIds(ruleForm.value.seqList).then((res) => {
    operationList.value = res.data;
  });
};
watch(infoIndex, (val) => {
  if (val || val == 0) {
    ruleForm.value.reason = ReworkTempList.value[val].name;
    ruleForm.value.remark = ReworkTempList.value[val].remark;
  }
});
onMounted(() => {
  getPagination();
  getReworkTempList();
});
</script>

<style lang="scss" scoped>
.table {
  height: calc(100vh - 240px);
  border-radius: 16px;
}
.body {
  width: 100%;
  height: calc(100vh - 60px);
  .opera {
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: white;
    padding-right: 8%;
  }
}
.containerBox {
  height: calc(100vh - 100px);
  padding: 20px;
}
:deep(.el-switch__label) {
  span {
    font-size: 20px !important;
    line-height: 25px !important;
  }
}
.form {
  width: 50%;
  max-width: 600px;
  height: calc(100vh - 240px);
  background-color: white;
  margin: 0 auto;
  padding: 0 20px;
  padding-top: 20px;
  border-radius: 16px;
}
.btns {
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 50%;
  max-width: 600px;

  margin: 0 auto;
}
</style>
