
/**
 * 获取主表初始化数据
 *
 * @returns 表字段
 */
export function getMasterTableInitialData() {
  return [
    {
      dbFieldName: "id",
      dbFieldTxt: "主键",
      dbLength: 36,
      dbPointLength: 0,
      dbDefaultVal: "",
      dbType: "string",
      fieldBelong: "common",
      dbIsKey: "1",
      dbIsNull: "0",
      isShowForm: "0",
      isShowList: "0",
      isReadOnly: "1",
      fieldShowType: "text",
      fieldLength: "200",
      queryMode: "single",
      orderNum: 1
    },
    {
      dbFieldName: "start_time",
      dbFieldTxt: "任务开始时间",
      dbLength: 20,
      dbPointLength: 0,
      dbDefaultVal: "",
      dbType: "Datetime",
      fieldBelong: "task",
      dbIsKey: "0",
      dbIsNull: "1",
      isShowForm: "0",
      isShowList: "0",
      fieldShowType: "datetime",
      fieldLength: "200",
      queryMode: "single",
      orderNum: 2
    },
    {
      dbFieldName: "end_time",
      dbFieldTxt: "任务结束时间",
      dbLength: 20,
      dbPointLength: 0,
      dbDefaultVal: "",
      dbType: "Datetime",
      fieldBelong: "task",
      dbIsKey: "0",
      dbIsNull: "1",
      isShowForm: "0",
      isShowList: "0",
      fieldShowType: "datetime",
      fieldLength: "200",
      queryMode: "single",
      orderNum: 3
    },
    {
      dbFieldName: "create_by",
      dbFieldTxt: "创建人",
      dbLength: 50,
      dbPointLength: 0,
      dbDefaultVal: "",
      dbType: "string",
      dbIsKey: "0",
      dbIsNull: "1",
      isShowForm: "0",
      isShowList: "0",
      fieldShowType: "text",
      fieldLength: "200",
      queryMode: "single",
      orderNum: 4
    },
    {
      dbFieldName: "create_time",
      dbFieldTxt: "创建日期",
      dbLength: 20,
      dbPointLength: 0,
      dbDefaultVal: "",
      dbType: "Datetime",
      dbIsKey: "0",
      dbIsNull: "1",
      isShowForm: "0",
      isShowList: "0",
      fieldShowType: "datetime",
      fieldLength: "200",
      queryMode: "single",
      orderNum: 5
    },
    {
      dbFieldName: "update_by",
      dbFieldTxt: "更新人",
      dbLength: 50,
      dbPointLength: 0,
      dbDefaultVal: "",
      dbType: "string",
      dbIsKey: "0",
      dbIsNull: "1",
      isShowForm: "0",
      isShowList: "0",
      fieldShowType: "text",
      fieldLength: "200",
      queryMode: "single",
      orderNum: 6
    },
    {
      dbFieldName: "update_time",
      dbFieldTxt: "更新日期",
      dbLength: 20,
      dbPointLength: 0,
      dbDefaultVal: "",
      dbType: "Datetime",
      dbIsKey: "0",
      dbIsNull: "1",
      isShowForm: "0",
      isShowList: "0",
      fieldShowType: "datetime",
      fieldLength: "200",
      queryMode: "single",
      orderNum: 7
    },
    {
      dbFieldName: "sys_org_code",
      dbFieldTxt: "所属部门",
      dbLength: 64,
      dbPointLength: 0,
      dbDefaultVal: "",
      dbType: "string",
      dbIsKey: "0",
      dbIsNull: "1",
      isShowForm: "0",
      isShowList: "0",
      fieldShowType: "text",
      fieldLength: "200",
      queryMode: "single",
      orderNum: 8
    }
  ]
}

/**
 * 设置数据源
 *
 * @param tableObj 表对象
 * @param dataList 数据源
 */
export function setDataSource(tableObj, dataList) {
  let result = [];
  dataList.forEach(data => {
    let item = {
      id: data["id"]
    };
    tableObj.columns.forEach(column => {
      let key = column.key;
      if (key) {
        item[key] = data[key];
      }
    })
    result.push(item)
  })
  tableObj.dataSource = result
}


/**
 * 同步所有表格数据
 *
 * @param targetTable 当前表格
 * @param dbTable 目标表
 * @returns {Promise<void>}
 */
export async function syncAllTable(targetTable, dbTable) {
  let dbValues;
  // 重置滚动位置
  targetTable.$refs.editableTable.resetScrollTop();
  // 获取被删除项的id
  let deleteIds = dbTable.$refs.editableTable.getDeleteIds();
  // 获取db中的值
  dbTable.$refs.editableTable.getValuesPromise(false)
    .then((dbValuesArr) => {
      dbValues = dbValuesArr;
      // 获取要同步的值
      return targetTable.$refs.editableTable.getValuesPromise(false);
    })
    .then((targetValues) => {
      dbValues.forEach((dbValue) => {
        let isSync = false;
        targetValues.forEach((targetValue) => {
          if (dbValue.id === targetValue.id) {
            // 字段名称和备注
            let dbFieldName = targetValue['dbFieldName'];
            let dbFieldTxt = targetValue['dbFieldTxt'];
            // 备注和名称不相同时
            if (dbValue.dbFieldName !== dbFieldName || dbValue.dbFieldTxt !== dbFieldTxt) {
              // 同步到目标表
              targetTable.$refs.editableTable.setValues([{
                rowKey: targetValue.id,
                values: {
                  dbFieldName: dbValue.dbFieldName,
                  dbFieldTxt: dbValue.dbFieldTxt
                }
              }])
            }
            isSync = true;
          } else {
            deleteIds.forEach((deleteId) =>{
              // 存在被删除的的字段时
              if (deleteId === targetValue.id) {
                // 同步到目标表
                targetTable.$refs.editableTable.removeRows(targetTable.$refs.editableTable.caseId + deleteId)
                isSync = true
              }
            })
          }
        })
        // 非同步时
        if (!isSync) {
          let record = Object.assign({}, dbValue);
          targetTable.columns.forEach((column)=> {
            if (column.dataIndex !== "dbFieldName" && column.dataIndex !== "dbFieldTxt") {
              record[column.dataIndex] = column.defaultValue
            }
          })
          // 添加数据
          targetTable.$refs.editableTable.push(record);
        }
      })
    }
  );



}
