<template>
  <div>
    <el-table :data="record" style="width: 100%" border>
      <el-table-column type="index" label="序号"  :width="70"></el-table-column>
      <el-table-column prop="date" label="修改时间"></el-table-column>
      <el-table-column prop="createUserName" label="修改人"></el-table-column>
      <el-table-column label="修改概述">
        <template #default="{ row, $index }">
          <span>{{ getDetails(row, $index === 0 ? "创建" : "变更：") }} </span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getCompanyList } from "@/api/manage/companyRecord";
import lodash from "lodash";
import dictVertical from "../../util/demo/dict-vertical.vue";

const ignoreKey = {
  id: true,
  companyId: true,
  createUser: true,
  createUserName: true,
  createTime: true,
  reviewTime: true,
  tenantId: true,
  authType: true,
  manager: true,
  telphone: true,
};
const labelDic = {
  stationId: "所属市场",
  companyName: "用工单位",
  tradeId: "所属行业",
  masterName: "法人姓名",
  masterIdentity: "法人身份证号",
  companyTid: "统一信用代码",
  companyAddress: "详细地址",
  cityId: "所在地区",
  authUrlId: "营业执照照片",
  identityUrl4Id: "法人身份证（人像）",
  identityUrl5Id: "法人身份证（国徽）",
  remarks: "驳回原因",
  status: "企业状态",
};

function First(d) {
  const data = lodash.cloneDeep(d);

  this.change = (newData) => {
    const result = {
      date: newData.createTime,
      row: [],
      createUserName: newData.createUserName,
    };
    for (const key in newData) {
      if (Object.hasOwnProperty.call(newData, key)) {
        const element = newData[key];
        if (
          !ignoreKey.hasOwnProperty(key) &&
          element !== null &&
          element !== "" &&
          element !== -1
        ) {
          result.row.push({
            key,
            label: labelDic[key],
            newValue: element,
            oldValue: data[key],
          });
        }
      }
    }

    return result;
  };

  this.create = {
    date: data.createTime,
    row: [],
    createUserName: data.createUserName,
  };

  return this;
}

function record(data) {
  const arr = [];
  const first = new First(data[0]);
  arr[0] = first.create;
  for (let index = 1; index < data.length; index++) {
    const element = data[index];
    arr.push(first.change(element));
  }

  return arr;
}

export default {
  components: { dictVertical },
  props: {
    data: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      list: [],
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      getCompanyList(this.data.id).then((rep) => {
        this.list = rep.data.data;
      });
    },
    getDetails(row, start) {
      if (!row.row) {
        return "";
      }
      let rel = start;
      Array.prototype.forEach.call(row.row, (item) => {
        rel += "、" + item.label;
      });
      rel = rel.replace("、", "");
      return rel;
    },
  },
  computed: {
    record() {
      let result = [];
      if (this.list.length > 0) {
        result = record(this.list);
      }
      return result;
    },
  },
  watch: {},
};
</script>

<style>
</style>