<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>
      </template>
    </avue-crud>
    <div v-else>
      <Process :key="proccessKey" @get-list="getList" />
    </div>
  </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";
const { dicts } = useDictionaryStore();
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 = "sales";
  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",
    },
    {
      label: "阶段",
      prop: "stage",
    },
    {
      label: "创建时间",
      prop: "created",
    },
    {
      label: "备注",
      prop: "remark",
    },
    {
      label: "状态",
      prop: "state",
      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>
