import { message } from 'ant-design-vue'
import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal, genItemRelatedSearchModal } from '../../../SearchModal'
import { changecargoOwnerfun } from '@/views/kzyWMS/utils/setCargoOwner'
import {upcloud_base_common, upcloud_base_wh_inbound} from "@/config/serviceSettings";
const extendConfig = {
  mapper: { warehouseAreaCode: 'workarea' }
}

let grid = {
  span: 8,
  labelCol: { span: 6 }
}

export const searchItems = function(self) {
  return [
    ...genWarehouseAuthSearchModal(1, true, false, null, changecargoOwnerfun, grid, null),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, grid, null),
    ...genItemRelatedSearchModal(1, false, false, function(res, p) {
      let vals = self.$refs.searchTable.$refs.form.form.getFieldsValue()
      if (!vals.warehouseId) {
        message.warning('请先选择仓库')
        return false
      }
      if (!vals.cargoOwnerId) {
        message.warning('请先选择货主')
        return false
      }
      p.data.itemCategoryCode = vals.itemCategoryCode;
      p.data.categoryCode = vals.categoryCode;
      p.data.warehouseId = vals.warehouseId;
      p.data.cargoOwnerId = vals.cargoOwnerId;
      return p
    }, null, grid, null),
    {
      type: 'date',
      name: 'startTime',
      label: '起始时间',
      format: 'YYYY-MM-DD',
      ...grid
    },
    {
      type: 'date',
      name: 'endTime',
      label: '结束时间',
      format: 'YYYY-MM-DD',
      ...grid
    },
    {
      type: 'input',
      name: 'refBillId',
      label: '单据编号',
      ...grid
    },
    {
      type: 'input',
      name: 'supplierOrShipperName',
      label: '单位名称',
      ...grid
    },
    // {
    //   type: 'input',
    //   name: 'departmentName',
    //   label: '部门名称',
    //   ...grid
    // },
    {
      type: 'input',
      name: 'scanName',
      label: '操作员',
      ...grid
    },
    {
      type: 'input',
      name: 'supervisionCode',
      label: '监管码',
      ...grid
    },
    // {
    //   type: 'select',
    //   name: 'supervisionType',
    //   label: '监管类型',
    //   required: true,
    //   value: undefined,
    //   remoteUrl: { url: upcloud_base_common+'/dict/listbytype?dicttypeCodes=supervision_type', method: 'post' },
    //   replaceFields: {
    //     value: 'dictCode',
    //     label: 'dictName'
    //   },
    //   ...grid
    // },
    {
      type: 'select',
      name: 'supervisionType',
      label: '监管类型',
      required: true,
      value:'',
      options: [
        {label: "采购入库", value: "0"},
        {label: "销售退货", value: "1"},
      ],
      ...grid
    },
    {
      type: 'select',
      name: 'status',
      label: '导出类型',
      options: [
        {
          label: '未导出',
          value: '10'
        },
        {
          label: '已导出',
          value: '20'
        }
      ],
      value: '10',
      ...grid
    },
    {
      type: 'input',
      name: 'containerCode',
      label: '容器号',
      ...grid
    },
    {
      type: 'select',
      name: 'UPLOAD_FLAG',
      label: '上传状态',
      value: undefined,
      options: [
        {label:'已上传',value:1},
        {label:'未上传',value:0},
        {label:'上传失败',value:2},
      ],
      ...grid
    },
  ]
}

export function download(data ,url, param) {
  let params = param
  let xhr = new XMLHttpRequest();
  console.log(xhr)
  const supervisionType = data.supervisionType
  const ids = data.ids
  const token = sessionStorage.getItem("token")

  xhr.open('get',  upcloud_base_wh_inbound + '/supervisionCodeXml/exportXml?supervisionType='+supervisionType+'&ids='+ids+'&access_token+'+token, true);
  xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  xhr.setRequestHeader("Authorization", 'Bearer ' + token);
  // xhr.send("a=3&b=4");

  xhr.responseType = 'blob';
  xhr.onload = function (e) {
    if (this.status == 200) {
      if (navigator.msSaveBlob) {
        return navigator.msSaveBlob(this.response, params.originalName ? params.originalName : '错误提示文档.xlsx')
      }
      let blob = this.response;
      let a = document.createElement('a');
      let url = window.URL.createObjectURL(blob);
      a.href = url;
      //获取后端文件名称
      let fileName = decodeURI(xhr.getResponseHeader('content-disposition'));
      console.log(fileName)

      //截取=字符串后面的内容
      let str = fileName.substring(21, fileName.length)
      // let utfStr = decodeURI(escape(str))     //是ISO_8859_1格式->改成utf-8
      a.download = fileName;
      a.click();
      window.URL.revokeObjectURL(url);
    }
  };
  //参数是json格式
  xhr.send(JSON.stringify(params));
}


export function downloadALL(data ,callBack, param) {
  let params = param
  let xhr = new XMLHttpRequest();
  console.log(xhr)
  const ids = data.ids
  const token = sessionStorage.getItem("token")

  xhr.open('get',  upcloud_base_wh_inbound + '/supervisionCodeXml/exportXmlAll?supervisionType='+data.supervisionType+'&warehouseId='+data.warehouseId+'&cargoOwnerId='+data.cargoOwnerId+'&access_token+'+token, true);
  xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
  xhr.setRequestHeader("Authorization", 'Bearer ' + token);
  // xhr.send("a=3&b=4");

  xhr.responseType = 'blob';
  xhr.onload = function (e) {
    if (this.status == 200) {
      if (navigator.msSaveBlob) {
        return navigator.msSaveBlob(this.response, params.originalName ? params.originalName : '错误提示文档.xlsx')
      }
      let blob = this.response;
      let a = document.createElement('a');
      let url = window.URL.createObjectURL(blob);
      a.href = url;
      //获取后端文件名称
      let fileName = decodeURI(xhr.getResponseHeader('content-disposition'));
        console.log(fileName)
      if (fileName == 'noXmlData.zip') {
        callBack(false)
      } else {
        //截取=字符串后面的内容
        let str = fileName.substring(21, fileName.length)
        // let utfStr = decodeURI(escape(str))     //是ISO_8859_1格式->改成utf-8
        a.download = fileName;
        a.click();
        window.URL.revokeObjectURL(url);
        downloadALL(data ,callBack, param)
        
      }
    }
  };
  //参数是json格式
  xhr.send(JSON.stringify(params));
}