<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef"
      v-model:search="search"
      v-model="form"
      :data="data"
      :option="option"
      v-model:page="page"
      @row-save="createRow"
      @row-update="updateRow"
      @row-del="deleteRow"
      @search-change="searchChange"
      @search-reset="resetChange"
      @size-change="dataList"
      @current-change="dataList"
      @selection-change="selectionChange"
    >
      <template #menu="{ row, index, type }">
        <el-button
          @click="showTable(row)"
          link
          type="primary"
          size="small"
          >查看</el-button
        >
        <el-button
            type="primary"
            size="small"
            link
            @click="showSeq(row.seqList)"
        >查看管号</el-button
        >
        <el-button
            link
          @click="doEdit(row, index)"
          type="primary"
          v-if="row.state === 0 || row.state === 1"
            size="small"
          >编辑</el-button
        >
        <el-button
          @click="operation(row)"
          link
          size="small"
          type="primary"
          >返工工序</el-button
        >
        <el-button
            link
          @click="handle(row, index, 2)"
          type="primary"
          v-if="row.state === 0"
            size="small"
          >驳回</el-button
        >
        <el-button
            link
          @click="handle(row, index, 1)"
          type="primary"
          v-if="row.state === 0"
            size="small"
          >确认返工</el-button
        >
<!--        <el-button
            link
          v-if="row.state === 1"
          @click="handle(row, index, 3)"
          type="primary"
            size="small"
          >返工完成</el-button
        >-->
        <el-button
            link
          v-if="row.state === 1"
          @click="handle(row, index, 3)"
          type="primary"
            size="small"
        >返工完成</el-button
        >
        <el-button
          @click="document(row)"
          link
          size="small"
          v-if="row.state > 0 && row.state!==2 "
          type="primary"
        >领料单</el-button
        >
      </template>

      <template #menu-right="{}">
        <el-button
          class="ml-3"
          @click="exportData('/api/v1/rework/record/export')"
        >
          <template #icon> <i-ep-download /> </template>导出
        </el-button>
      </template>
    </avue-crud>
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="900px"
      @close="closeOperation"
      :destroy-on-close="true"
    >
      <ReworkItem
        :recordId="record.id"
        :dialog="dialog"
        :status="record.state"
      />
    </el-dialog>
    <el-dialog
      v-model="dialog1.visible"
      :title="dialog1.title"
      width="900px"
      @close="dialog1.visible=false"
      :destroy-on-close="true"
    >
      <ReworkDocument
        :recordId="record.id"
        :dialog="dialog1"
        :orderCode="record.orderCode"
        :status="record.state"
      />
    </el-dialog>
    <el-dialog
        v-model="dialog2.visible"
        :title="dialog2.title"
        width="950px"
        @close="dialog2.visible = false"
    >
      <el-card
          style="cursor: pointer; font-size: 20px"
          shadow="always"
          :key="item"
          v-for="item in showSeqList"
      >{{ item }}</el-card
      >
    </el-dialog>
    <ReworkShow v-model="showState" :tableData="tableData" />
  </div>
</template>
<script setup>
import { useCrud } from "@/hooks/userCrud";
import dictDataUtil from "@/common/configs/dictDataUtil";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import { useCommonStoreHook, useDictionaryStore } from "@/store";
import ReworkItem from "@/views/quality/rework/components/rework-detail.vue";
import { ref } from "vue";
import { handleReworkRecord, getReworkItem } from "@/api/fault/index";
import ReworkShow from "@/views/quality/rework/components/rework-show.vue";

import ReworkDocument from "@/views/quality/rework/components/rework-document.vue";

// 数据字典相关
const { dicts } = useDictionaryStore();
const showSeqList = ref([]);
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/rework/record",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const tableData = ref({});
const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const dialog = ref({
  title: "关联工序",
  visible: false,
});
const dialog1 = ref({
  title: "领料单",
  visible: false,
});
const dialog2 = reactive({
  title: "管号列表",
  visible: false,
});
const showSeq = (seqs) => {
  showSeqList.value = seqs;
  dialog2.visible = true;
};
const doEdit = (row, index) => {
  crudRef.value && crudRef.value.rowEdit(row, index);
};
onMounted(() => {
  dataList();
});
const record = ref({});
const operation = (row) => {
  record.value = row;
  dialog.value.visible = true;
};

const document = (row) => {
  record.value = row;
  console.log(record.value);
  dialog1.value.visible = true;
};
const showState = ref(false);
const showTable = (row) => {
  reworkItem.value.recordId = row.id;
  getReworkItem(reworkItem.value).then((data) => {
    if (data.code === "200") {
      tableData.value = row;
      tableData.value.itemList = data.data;
      showState.value = true;
    } else {
      ElMessage({
        message: data.msg,
        type: "error",
      });
    }
  });
};
const closeOperation = () => {
  dialog.value.visible = false;
};
const handleData = ref({});
const reworkItem = ref({});
const handle = (row, index, state) => {
  handleData.value.id = row.id;
  handleData.value.state = state;
  handleData.value.seqNos = row.seqNos;
  handleReworkRecord(handleData.value).then((data) => {
    if (data.code === "200") {
      ElMessage({
        message: data.msg,
        type: "success",
      });
      dataList();
    } else {
      ElMessage({
        message: data.msg,
        type: "error",
      });
    }
  });
};
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  searchEnter: true,
  selection: false,
  addBtn: false,
  delBtn: false,
  editBtn: false,
  viewBtn: false,
  column: [
    {
      label: "生产批号",
      prop: "workOrderCode",
      editDisabled: true,
      overHidden: true,
      width: 120,
      search: true,
    },
    {
      label: "产品名称",
      prop: "materialName",
      editDisabled: true,
      search: true,
    },
    {
      label: "数量",
      editDisabled: true,
      prop: "num",
    },
    {
      label: "管号",
      prop: "seqNo",
      search: true,
      editDisabled: true,
      hide: true,
      display: false,
    },
    {
      label: "返工原因",
      prop: "reason",
      width: 160,
      search: true,
      overHidden: true,
      type: "textarea",
    },
    {
      label: "产品规格",
      prop: "materialModel",
      width: 160,
      search: true,
      overHidden: true,
    },
    {
      label: "返工内容",
      prop: "remark",
      width: 160,
      overHidden: true,
      type: "textarea",
    },
    {
      label: "状态",
      prop: "state",
      type: "select",
      display: false,
      dicData: [
        {
          label: "待处理",
          value: 0,
        },
        {
          label: "返工中",
          value: 1,
        },
        {
          label: "驳回",
          value: 2,
        },
        {
          label: "返工完成",
          value: 3,
        },
      ],
    },

    {
      label: "创建人",
      prop: "creator",
      editDisplay: false,
    },
    {
      label: "创建日期",
      prop: "created",
      type: "date",
      width: 160,
      editDisplay: false,
      overHidden: true,
    },
    {
      label: "日期",
      prop: "createdSearch",
      width: 160,
      type: "daterange",
      searchRange: true,
      display: false,
      hide: true,
      startPlaceholder: "开始范围",
      endPlaceholder: "结束范围",
      format: "YYYY-MM-DD",
      valueFormat: "YYYY-MM-DD",
      overHidden: true,
      search: true,
    },
  ],
});
</script>
