import { Query, newData, modifyData, moveData } from "@/api/webApi"
import { getAuthData } from "@/utils/validate";
export default {
  data () {
    return {
      AauthId: null,
      AqueryId: null,
      AauthTable: [],
      TauthId: null,
      TqueryId: null,
      AqueryParams: null
    }
  },
  created () {
    const { auth_id } = this.$route.meta
    if (auth_id) {
      const { arrs } = getAuthData(auth_id)
      const table = arrs.filter(item => item.auth_type == 4)
      const table2 = arrs.filter(item => item.auth_type == 5)
      if (table2 && table2.length) {
        const obj = table2[0]
        this.AqueryParams = obj
        this.TqueryId = obj.queryTemplateID
        this.TauthId = obj.auth_id
      }
      if (table && table.length) {
        if (table.length == 1) {
          const obj = table[0]
          this.AqueryParams = obj
          this.AqueryId = obj.queryTemplateID
          this.AauthId = obj.auth_id
        } else {
          this.AauthTable = table
        }
      } else {
        this.AauthId = auth_id
        this.AqueryId = auth_id
      }
    }
  },
  methods: {
    formatQuery (arrs, key = '') {
      const params = []
      if (key) {
        const flag = Array.isArray(arrs)
        if (flag) {
          for (let i = 0; i < arrs.length; i++) {
            const obj = {
              "left": "(",
              "column": `${key[i]}`,
              "comparator": "=",
              "value": `${arrs[i][key[i]]}`,
              "right": ")",
              "connector": "and"
            }
            params.push(obj)
          }
        } else {
          params.push({
            "left": "(",
            "column": `${key}`,
            "comparator": "=",
            "value": `${arrs[key]}`,
            "right": ")",
            "connector": "and"
          })
        }
      } else {
        params.push({
          "left": "(",
          "column": ``,
          "comparator": "=",
          "value": ``,
          "right": ")",
          "connector": "and"
        })
      }
      return params
    },
    formatChange (data, event, key) {
      const datas = []
      const flag = Array.isArray(data)
      if (flag) {
        for (let i = 0; i < data.length; i++) {
          let obj = {}
          if (event == 1) {
            obj.Value = data[i]
          } else if (event == 2) {
            obj.Value = data[i]
            obj.filter = {}
            if (Array.isArray(key)) {
              const keys = []
              const vals = []
              obj.filter = []
              for (let j = 0; j < key.length; j++) {
                const dep = key[i]
                keys.push(Object.keys(dep))
                vals.push(Object.values(dep))
              }
              const nobj = {
                "left": "(",
                "column": `${keys[i]}`,
                "comparator": "=",
                "value": `${vals[i]}`,
                "right": ")",
                "connector": "and"
              }
              obj.filter.push(nobj)
              if (obj.Value.hasOwnProperty(keys[i])) {
                delete obj.Value[keys[i]]
              }
            } else {
              obj.filter[key] = data[i][key]
              if (obj.Value[key]) {
                delete obj.Value[key]
              }
            }
          } else {
            obj = data[i]
          }
          datas.push(obj)
        }
      } else {
        let obj = {}
        if (event == 1) {
          obj.Value = data
        } else if (event == 2) {
          obj.Value = data
          obj.filter = [{
            "left": "(",
            "column": `${key}`,
            "comparator": "=",
            "value": `${data[key]}`,
            "right": ")",
            "connector": "and"
          }]
          if (obj.Value[key]) {
            delete obj.Value[key]
          }
        } else {
          obj = data
        }
        datas.push(obj)
      }
      return datas
    },
    //查询
    async getQueryList (id, dataContent = {}, pageIndex = 1, pageSize = 999) {
      try {
        const { code, rowcount, returnData } = await Query({
          serviceId: id,
          page: pageIndex,
          pageSize: pageSize,
          dataContent: dataContent,
          event: '0',
          // authId: this.AauthId
        })
        return { code, rowcount, returnData }
      } catch (error) {
        return []
      }
    },
    //权限查询
    async getQueryListAuth (id, dataContent = {}, pageIndex = 1, pageSize = 999, authId) {
      try {
        const { code, rowcount, returnData } = await Query({
          serviceId: id,
          page: pageIndex,
          pageSize: pageSize,
          dataContent: dataContent,
          event: '0',
          authId: authId ?? this.AauthId
        })
        return { code, rowcount, returnData }
      } catch (error) {
        return []
      }
    },
    //高级查询
    async getQueryGj (id, dataContent = {}, key, pageSize) {
      try {
        const { code, columnset, returnData } = await Query({
          serviceId: id,
          page: this.pageIndex,
          pageSize: pageSize ?? this.pageSize,
          dataContent: this.formatQuery(dataContent, key),
          event: '0',
          // authId: this.AauthId
        })
        if (code == 0) {
          return returnData
        } else {
          return []
        }
      } catch (error) {
        return []
      }
    },
    //增/删/改
    async getChangeList (id, data, event, key) {
      try {
        const params = {
          serviceId: id,
          dataContent: this.formatChange(data, event, key),
          event: `${event}`,
          // authId: this.AauthId
        };
        const { code, message } =
          event == 1
            ? await newData(params)
            : event == 2
              ? await modifyData(params)
              : await moveData(params);
        return { code, message }
      } catch (error) {
        console.log(error)
        return {
          code: -1,
          message: error
        }
      }
    },
    tipMsg (code, msg) {
      if (code == 0 || code != -1) {
        this.$message.success(msg ?? '操作成功')
      } else {
        this.$message.error(msg ?? '操作失败')
      }
    }
  }
}