function setShowElement(name, row) {
  if (typeof name == "function") {
    return name(row || {});
  } else {
    return !!name;
  }
}

export default {
  data() {
    return {
      isEditTableIndex: -1,
    };
  },
  methods: {
    convertTableJson(
      tableData,
      list,
      attributes = {
        isAdd: true,
        isDel: true,
        isUpdate: true,
        isConfirm: true,
        isEdit: true,
        isTemElm: true,
        confirmText: "",
        editText: "",
        delText: "",
      },
      funs = {
        add: function () {},
        confirm: function () {},
        delete: function () {},
        update: function () {},
        verify: function () {},
        btnSlot: function () {},
      },
      qianyu = ""
    ) {
      var newList = list.map((item) => {
        if (item.editRender && item.viewRender) {
          item.render = (h, { row, column, index }) => {
            if (
              attributes?.isTemElm !== undefined
                ? setShowElement(attributes?.isTemElm, { row, column, index })
                : index == this.isEditTableIndex
            ) {
              return item.editRender(h, { row, column, index });
            } else {
              return item.viewRender(h, { row, column, index });
            }
          };
        }
        return item;
      });
      if (setShowElement(attributes?.isEdit)) {
        newList.push({
          columnAttributes: {
            label: "操作",
            width: 100,
          },
          render: (h, { row, column, index }) => {
            return (
              <div style="padding-left:10px">
                {(attributes?.isConfirm !== undefined
                  ? setShowElement(attributes?.isConfirm, {
                      row,
                      column,
                      index,
                    })
                  : this.isEditTableIndex == index) && (
                  <el-button
                    type="text"
                    onClick={async () => {
                      if (
                        funs.verify &&
                        !(await funs.verify(
                          { row, column, index },
                          this.isEditTableIndex
                        ))
                      ) {
                        return;
                      }
                      if (funs.confirm) {
                        funs.confirm({ row, column, index });
                      } else {
                        this.isEditTableIndex = -1;
                      }
                    }}
                  >
                    {attributes?.confirmText || "确定"}
                  </el-button>
                )}
                {(attributes?.isUpdate !== undefined
                  ? setShowElement(attributes?.isUpdate, { row, column, index })
                  : this.isEditTableIndex == -1) && (
                  <el-button
                    type="text"
                    onClick={async () => {
                      if (
                        funs.verify &&
                        !(await funs.verify(
                          { row, column, index },
                          this.isEditTableIndex
                        ))
                      ) {
                        return;
                      }
                      if (funs.update) {
                        funs.update({ row, column, index });
                      } else {
                        this.isEditTableIndex = index;
                      }
                    }}
                  >
                    {attributes?.editText || "编辑"}
                  </el-button>
                )}
                {setShowElement(attributes?.isDel, { row, column, index }) && (
                  <el-button
                    type="text"
                    onClick={async () => {
                      if (funs.delete) {
                        funs.delete({ row, column, index }, function () {
                          tableData.splice(index, 1);
                        });
                      } else {
                        tableData.splice(index, 1);
                      }
                    }}
                  >
                    {attributes?.delText || "删除"}
                  </el-button>
                )}
                {funs.btnSlot ? funs.btnSlot({ row, column, index }) : null}
              </div>
            );
          },
        });
      }
      return (
        <div>
          {setShowElement(attributes?.isAdd) ? (
            <div>
              {qianyu}
              <el-button
                size="mini"
                type="primary"
                onClick={async () => {
                  if (
                    funs.verify &&
                    !(await funs.verify({}, this.isEditTableIndex))
                  ) {
                    return;
                  }
                  if (funs.add) {
                    funs.add();
                  }
                }}
              >
                新增
              </el-button>
            </div>
          ) : null}
          <zj-table columns={newList} table-data={tableData} />
        </div>
      );
    },
  },
};
