<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)"
          icon="el-icon-view"
          text
          type="primary"
          >查看</el-button
        >
        <el-button
          icon="el-icon-edit"
          text
          @click="doEdit(row, index)"
          type="primary"
          v-if="row.state === 0 || row.state === 1"
          :size="size"
          >编辑</el-button
        >
        <el-button
          @click="operation(row)"
          icon="el-icon-link"
          text
          type="primary"
          >返工工序</el-button
        >
        <el-button
          icon="el-icon-edit"
          text
          @click="handle(row, index, 2)"
          type="primary"
          v-if="row.state === 0"
          >驳回</el-button
        >
        <el-button
          icon="el-icon-edit"
          text
          @click="handle(row, index, 1)"
          type="primary"
          v-if="row.state === 0"
          >确认返工</el-button
        >
        <el-button
          icon="el-icon-edit"
          text
          v-if="row.state === 1"
          @click="handle(row, index, 3)"
          type="primary"
          :size="size"
          >返工完成</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>
    <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";

// 数据字典相关
const { dicts } = useDictionaryStore();

// 传入一个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 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 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, {
  selection: false,
  addBtn: false,
  delBtn: false,
  editBtn: false,
  viewBtn: false,
  column: [
    {
      label: "生产批号",
      prop: "workOrderCode",
      editDisabled: true,
      width: 160,
      overHidden: true,
      search: true,
    },
    {
      label: "产品名称",
      prop: "materialName",
      editDisabled: true,
      search: true,
    },
    {
      label: "数量",
      editDisabled: true,
      prop: "num",
    },
    {
      label: "管号",
      prop: "seqNos",
      editDisabled: true,
    },
    {
      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: "返工提出者",
      prop: "creator",
      editDisplay: false,
    },
    {
      label: "日期",
      prop: "created",
      type: "date",
      width: 160,
      format: "YYYY-MM-DD",
      valueFormat: "YYYY-MM-DD",
      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>
