import request from '@/utils/request'
import qs from 'qs'
import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE,UPCLOUD_RDC_TMS_BUSINESS_SERVICE } from '@/config/serviceSettings.js'
const serviceName = UPCLOUD_RDC_TMS_BUSINESS_SERVICE

export const getQueryConfiguration = (self, data) => {
  return request({
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+'/sysQueryConfiguration/getQueryConfiguration/v1.0',
    method: 'POST',
    data:qs.stringify(data),
  }).then((response) => {
    if (response.success) {

      return response.data
    } else {
      self.$exception(response)
      return false
    }
  })
}

//配置控制层-保存
export const saveConfiguration = (_this, data) => {

  let confirmLoading = true
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否保存分组' + '?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        return request({
          url: serviceName + '/sys_group/save',
          method: 'post',
          data,
        })
          .then((response) => {

            if (response.success) {
              clear()
              // _this.$refs.mainTable.reload()
              _this.$message.success(response.message)
            } else {
              _this.$exception(response)
            }
            setTimeout(() => {
              confirmLoading = false
            }, 1000)
            return resolve(response)
          })
          .catch((response) => {
            confirmLoading = false
          })
      },
    })
  })
}

export const deleteConfiguration = (_this, id) => {

  let confirmLoading = true
  let data = {id}
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是删除分组?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        if (id.indexOf('new') > -1) {
          _this.conditionNameList = _this.conditionNameList.filter((item) => item.id != id);
          delete _this.tableDataform[id];
          _this.selectedKeys = [];
          return clear()

        }
        return request({
          url: serviceName + '/sys_group/delete',
          method: 'post',
          data,
        })
          .then((response) => {
            if (response.success) {
              clear()
              _this.$message.success(response.message)
              _this.getData()

            } else {
              _this.$exception(response)
            }
            setTimeout(() => {
              confirmLoading = false
            }, 1000)
            return resolve(response)
          })
          .catch((response) => {
            confirmLoading = false
          })
      },
    })
  })
}

//设为默认
export const setDefault = (_this, id) => {

  let confirmLoading = true
  let url = serviceName + '/sys_group/default'
  let data={id}
  return new Promise((resolve) => {
    _this.$confirm({
      title: '是否设置默认' + '?',
      confirmLoading,
      onOk(clear) {
        confirmLoading = true
        let is=false
        if (id.indexOf('new') > -1) {
          _this.conditionNameList.forEach((item) => {
            if (id === item.id) {
              item.defaultFlag = 'Y';
            } else {
              item.defaultFlag = "N";
            }
          }, _this);

          url = serviceName + '/sys_group/save';
          data = _this.resultSavaData()
          is=true
        }
      return request({
          url,
          method: 'post',
          data
        }).then((response) => {
            if (response.success) {
              clear()
              _this.$message.success(response.message)
              _this.getData().then(() => {

              })




            } else {
              _this.$exception(response)

            }
            setTimeout(() => {
              confirmLoading = false
            }, 1000)
            return resolve(response)
          })
          .catch((response) => {
            confirmLoading = false
          })
      },
    })
  })
}

// .then((res) => {
//   if (Array.isArray(res)) {
//     res.find((item) => {
//       if (item.defaultStatus === 1) {
//         this.activeKey = item.lineid
//       }
//     }, this)
//     this.tableData = res
//   }
// })

//分组数据查询
export const getQueryGroupAll = (self) => {
  return request({
    url:serviceName+ '/sys_group/query',
    method: 'POST',
    data:{}
  }).then((response) => {
    if (response.success) {
      return response.data
    } else {
      self.$exception(response)
      return false
    }
  })
}
