<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="state">
            <template #default="scope">
              <span v-if="scope.row.state == '0'">待处理</span>
              <span v-else-if="scope.row.state == '1'">已完成</span>
              <span v-else-if="scope.row.state == '-1'">审核驳回</span>
              <span v-else-if="scope.row.state == '3'">审核中</span>
              <span v-else-if="scope.row.state == '2'">撤销</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                v-if="row.state != '1' && row.state != 3"
                link
                type="danger"
                @click="del(row.id)"
                style="font-size: 20px"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <Pagination
          v-model:limit="limit"
          v-model:page="page"
          :position="'right'"
          :total="total"
          @pagination="getPagination1"
        />
      </template>
      <template v-else>
        <el-scrollbar class="form">
          <el-form ref="ruleFormRef1" :model="ruleForm1" :rules="rules1" label-width="100">
            <el-form-item
              label="流转卡号"
              prop="seqs"
              :rules="{
                required: true,
                trigger: 'change',
              }"
            >
              <el-select v-model="ruleForm1.seqs" multiple placeholder="请选择" value-key="value">
                <el-option v-for="item in processList" :key="item" :label="item" :value="item" />
              </el-select>
            </el-form-item>
            <el-form-item
              label="发生地点"
              prop="address"
              :rules="{
                required: true,
                trigger: 'change',
              }"
            >
              <el-input v-model="ruleForm1.address" />
            </el-form-item>
            <el-form-item
              label="数量"
              prop="remark1[num]"
              :rules="{
                required: true,
                trigger: 'change',
              }"
            >
              <el-input v-model="ruleForm1.remark1.num" />
            </el-form-item>
            <el-form-item
              label="问题描述"
              prop="remark1[content]"
              :rules="{
                required: true,
                trigger: 'change',
              }"
            >
              <el-input type="textarea" v-model="ruleForm1.remark1.content" />
            </el-form-item>
            <el-form-item
              label="审理级别："
              prop="remark1[lv]"
              :rules="{
                required: true,
                trigger: 'change',
              }"
            >
              <el-radio-group v-model="ruleForm1.remark1.lv">
                <el-radio value="1">一级</el-radio>
                <el-radio value="2">二级</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item
              label="填表人"
              prop="remark1[user1]"
              :rules="{
                required: true,
                trigger: 'change',
              }"
            >
              <el-tree-select
                v-model="ruleForm1.remark1.user1"
                :data="userList"
                placeholder="请选择人员"
                :default-expand-all="false"
                filterable
                clearable
              />
            </el-form-item>
            <el-form-item
              label="发生日期"
              prop="remark1[time]"
              :rules="{
                required: true,
                trigger: 'change',
              }"
            >
              <el-date-picker
                v-model="ruleForm1.remark1.time"
                type="date"
                format="YYYY-MM-DD"
                value-format="YYYY-MM-DD"
              />
            </el-form-item>
            <!--            <el-form-item
              label="指定检验人"
              prop="remark2[user2]"
              :rules="{
                required: true,
                trigger: 'change',
              }"
            >
              <el-tree-select
                v-model="ruleForm1.remark2.user2"
                :data="userList"
                placeholder="请选择人员"
                :default-expand-all="false"
                filterable
                clearable
              />
            </el-form-item>-->
          </el-form>
        </el-scrollbar>
        <div class="btns">
          <el-button type="primary" @click="submit1">提交</el-button>
        </div>
      </template>
    </el-scrollbar>
  </div>
</template>

<script setup>
import { breakReportInfoById, operationListByIds } from "@/api/process/reportBreak";
import { addRework, addRework1, getList, getList1, delError } from "@/api/rework";
import { useProcessStore } from "@/store/modules/processView";
import { useDictionaryStore } from "@/store/modules/dictionary";
import { getUserTree } from "@/api/user/index";
import { getunProcessedList } from "@/api/prosteps/index.js";
const dictStroe = useDictionaryStore();
const processStore = useProcessStore();
const ruleForm = ref({
  seqList: [],
});
const ruleForm1 = ref({
  seqs: [],
  remark1: {
    content: "",
    user1: "",
  },
  remark2: {
    user2: "",
  },
});
const tableData = ref([]);

const processList = ref([]);
const toAdd = () => {
  formStatus.value = false;
  /*breakReportInfoById(processStore.scanInfo.id).then((res) => {
    infoData.value = res.data;
  });*/
  getunProcessedList(processStore.scanInfo.id).then((res) => {
    processList.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 del = async (id) => {
  const { code } = await delError({
    id,
  });
  if (code == "200") {
    ElMessage.success("删除成功");
    getPagination1();
  }
};
const getPagination1 = async () => {
  const { data } = await getList1({
    type: "eliminate",
    pageNo: page.value,
    pageSize: limit.value,
    workOrderCode: processStore.odersData.workOrderCode,
    operationId: processStore.scanInfo.operationId,
  });
  tableData.value = data.records;
  total.value = data.totalCount;
};
const formStatus = ref(true);
const ruleFormRef1 = ref(null);
const submit1 = async () => {
  ruleFormRef1.value.validate((valid) => {
    if (valid) {
      addTask1();
    } else {
      ElMessage.error("请检查是否有未填项");
    }
  });
};

const addTask1 = async () => {
  const { code } = await addRework1({
    ...ruleForm1.value,
    materialModel: processStore.processInfo.materialModel,
    materialCode: processStore.scanInfo.materialCode,
    materialName: processStore.scanInfo.materialName,
    operationId: processStore.scanInfo.operationId,
    operationName: processStore.scanInfo.operationName,
    pid: processStore.scanInfo.id,
    workOrderCode: processStore.odersData.workOrderCode,
    orderNo: processStore.scanInfo.orderCode,
    stage: processStore.scanInfo.operationName,
    type: "eliminate",
    seqs: ruleForm1.value.seqs.join(","),
  });
  if (code == "200") {
    resetForm();
    formStatus.value = true;
    getPagination1();
    ElMessage.success("操作成功！");
  }
};

const total = ref(0);
const page = ref(1);
const limit = ref(10);
const operationList = ref([]);
const infoData = ref({});

const rules1 = reactive({
  location: [
    {
      required: true,
      message: "Please select a location",
      trigger: "change",
    },
  ],
});

// ================= 人员树结构
const userList = ref([]);
const queryUserList = () => {
  getUserTree().then((data) => {
    userList.value = data.data;
  });
};
onMounted(() => {
  getPagination1();
  queryUserList();
});
</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>
