<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"
      v-if="!examineStatus"
    >
      <template #menu="{ row }">
        <el-button
          v-if="row.state != '2' && row.state != '3'"
          link
          @click="toExamine(row)"
        >
          处理
        </el-button>
        <el-button v-if="row.state == '3'" link @click="toShow(row)">
          查看
        </el-button>
      </template>
    </avue-crud>
    <div v-else>
      <Process :key="proccessKey" @get-list="getList" />
    </div>
    <ShowVue 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 SingleUpload from "@/components/Upload/SingleUpload.vue";
import Process from "./components/process.vue";
import ShowVue from "./components/show.vue";
const { dicts } = useDictionaryStore();
const showState = ref(false);
const toShow = (row) => {
  tableData.value = setJson(row);
  showState.value = true;
};
const tableData = ref();
const proccessKey = ref(false);
const examineStatus = ref(false);
const formData = ref({});
provide("formData", formData);
provide("examineStatus", examineStatus);
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/error",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const crudRef = ref(null); //crudRef.value 获取avue-crud对象
onMounted(() => {
  search.value.type = "eliminate";
  dataList();
});
const getList = () => {
  dataList();
};
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  selection: false,
  addBtn: false,
  delBtn: false,
  editBtn: false,
  viewBtn: false,
  column: [
    {
      label: "产品名称",
      prop: "materialName",
      search: true,
    },
    {
      label: "产品型号",
      prop: "materialModel",
      search: true,
    },
    {
      label: "阶段",
      prop: "stage",
    },
    {
      label: "创建时间",
      prop: "created",
    },
    {
      label: "备注",
      prop: "remark",
    },
    {
      label: "生产批号",
      prop: "workordercode",
    },
    {
      label: "状态",
      prop: "state",
      search: true,
      type: "select",
      dicData: [
        {
          label: "待处理",
          value: "0",
        },
        {
          label: "审核中",
          value: "1",
        },
        {
          label: "审核驳回",
          value: "2",
        },
        {
          label: "审核完成",
          value: "3",
        },
      ],
    },
  ],
});
const setJson = (row1) => {
  const row = JSON.parse(JSON.stringify(row1));
  //处理初始值
  let obj = {
    content: "",
    user1: "",
    user2: "",
    user3: "",
    user4: "",
    user5: "",
    user6: "",
    user7: "",
    time: "",
    state: "-1",
  };
  if (row.remark1 == "") {
    row.remark1 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark1)).length == 0) {
      row.remark1 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark1 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark1),
      };
    }
  }
  if (row.remark2 == "") {
    row.remark2 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark2)).length == 0) {
      row.remark2 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark2 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark2),
      };
    }
  }
  if (row.remark3 == "") {
    row.remark3 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark3)).length == 0) {
      row.remark3 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark3 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark3),
      };
    }
  }
  if (row.remark4 == "") {
    row.remark4 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark4)).length == 0) {
      row.remark4 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark4 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark4),
      };
    }
  }
  if (row.remark5 == "") {
    row.remark5 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark5)).length == 0) {
      row.remark5 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark5 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark5),
      };
    }
  }
  if (row.remark6 == "") {
    row.remark6 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark6)).length == 0) {
      row.remark6 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark6 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark6),
      };
    }
  }
  if (row.remark7 == "") {
    row.remark7 = JSON.parse(JSON.stringify(obj));
  } else {
    if (Object.keys(JSON.parse(row.remark7)).length == 0) {
      row.remark7 = JSON.parse(JSON.stringify(obj));
    } else {
      row.remark7 = {
        ...JSON.parse(JSON.stringify(obj)),
        ...JSON.parse(row.remark7),
      };
    }
  }
  return row;
};
const toExamine = (row) => {
  formData.value = setJson(row);
  proccessKey.value = !proccessKey.value;
  examineStatus.value = true;
};
</script>
