import {
  genItemSearchModal,
  genWarehouseAuthSearchModal,
  genBldgSearchModal,
  genCargoownerAuthSearchModal,
  genWarehouseLocatorSearchModal,
  genItemSearchModalcuring
} from '@/views/kzyWMS/SearchModal'
import moment from 'moment'
import qs from 'qs'
import { api } from '@/views/kzyWMS/api/common/dictionaries-type'
import { changecargoOwnerfun } from '@/views/kzyWMS/utils/setCargoOwner'
import { sorter } from '../../../utils/sort'
import { upcloud_base_gsp, upcloud_base_strategy, SERVICE_DATA_API_BASE } from '@/config/serviceSettings'

export let itemHealthCategory = {
  type: 'select',
  name: 'itemHealthCategory',
  label: '商品大类',
  value: '',
  autoLoad: true,
  remoteUrl: { url: api.queryDictList + '?dicttypeCodes=item_health_category', method: 'post' },
  replaceFields: {
    value: 'dictCode',
    label: 'dictName'
  }
}
let warehouseId
const baseUrl = upcloud_base_gsp
import request from '@/utils/request'
function gettype(parameter) {
  //  批量录入销毁信息 POST
  return request({
    method: 'get',
    url: upcloud_base_strategy + '/registry/by-code',
    params: parameter
  })
}
// let a = [
//   { label: '待审核', value: '10' },
//   { label: '通过', value: '20' },
//   { label: '驳回', value: '30' }
// ]
// let b = [
//   { label: '待审核', value: '10' },
//   { label: '通过', value: '20' }
// ]
const tablecolumns = function(that) {
  return {
    gridform: {
      ref: 'form',
      items: [
        //genWarehouseAuthSearchModal 仓库
        ...genWarehouseAuthSearchModal(
          1,
          true,
          false,
          null,
          function(rowData, form) {
            form.setFieldsValue({
              cargoOwnerCode: null,
              cargoOwnerId: null,
              cargoOwnerName: null
            })
            console.log(rowData)
            if (rowData == null) {
              warehouseId = null
            } else {
              warehouseId = rowData[0].companyId
            }
          },
          {
            span: 6,
            labelCol: { span: 6 },
            wrapperCol: { span: 14 }
          }
        ),
        //货主
        ...genCargoownerAuthSearchModal(
          1,
          false,
          false,
          null,
          function(rowData, form) {
            console.log(rowData)
            if (!rowData) {
              cargoOwnerId = null
            } else {
              cargoOwnerId = rowData[0].companyId
            }
          },
          {
            span: 6,
            labelCol: { span: 6 },
            wrapperCol: { span: 14 }
          }
        ),
        //商品大类
        { ...itemHealthCategory, span: 6 },
        {
          type: 'date',
          name: 'startTime',
          label: '开始日期',
          format: 'YYYY-MM-DD',
          span: 6,
          value: moment()
            .startOf('month')
            .format('YYYY-MM-DD')
        },

        {
          type: 'date',
          name: 'endTime',
          format: 'YYYY-MM-DD',
          label: '结束日期',
          span: 6,
          value: moment()
            .locale('zh-cn')
            .format('YYYY-MM-DD')
        },
        ...genBldgSearchModal(
          1,
          false,
          false,
          function(res, p) {
            // console.log(res, p)
            console.log(warehouseId)
            // console.log(that.$store.state.user.warehouse)
            p.params.warehouseId = warehouseId || that.$store.state.user.warehouse.warehouseId
            return p
          },
          function(value, form) {
            if (!value) {
              bldgId = null
            } else {
              bldgId = value[0].bldgId
            }
          },
          {
            span: 6,
            labelCol: { span: 6 },
            wrapperCol: { span: 14 }
          }
          //   {
          //     search(p1, p2, p3, p4, p5) {
          //       console.log(p5)
          //       warehouseId = p5.warehouseId
          //       cargoOwnerId = p5.mtlOwnerId
          //       itemId = p5.itemId
          //       itemCategoryCode = p5.categoryId
          //     }
          //   }
        ),

        //商品
        ...genItemSearchModalcuring(
          1,
          false,
          false,
          function(res, p) {
            p.data.cargoOwnerId = cargoOwnerId
            p.data.warehouseId = warehouseId
            console.log(p, '====')
            return p
          },
          null,
          { span: 6, labelCol: { span: 6 }, wrapperCol: { span: 14 } }
        ),

        {
          span: 6,
          type: 'buttonGroup',
          style: { textAlign: 'left' },
          options: [
            {
              value: '查询',
              icon: 'search',
              btnType: 'primary',
              style: 'margin-right:10px;margin-left:30px;',
              htmlType: 'submit'
              //   on: {
              //     click(event, form) {
              //       console.log('弹窗')
              //       //   that.aaa()
              //       //   that.showModal()
              //     }
              //   }
            },
            {
              value: '重置',
              name: 'ccz',
              icon: 'redo',
              on: {
                click(event, form) {
                  form.resetFields()
                }
              }
            }
          ]
        }
      ]
    },

    //主表
    MainTable: {
      edit: false,
      // edit: true,
      title: '明细列表',
      // /strategy/v1/page
      //   remoteBefore(vn, params) {
      //     let p = { ...params }

      //     p.data = {
      //       id: '592360607987142656',
      //       pageNum: params.data.pageNum,
      //       pageSize: params.data.pageSize,
      //       params: params.data
      //     }
      //     console.log(p, params, '====')
      //     return p
      //   },
      //   doneBefore(res) {
      //     console.log(res)
      //     res.data.forEach(item => {
      //       item.upkeepConclusion = '1'
      //       if (item.upkeepQty) {
      //         item.upkeepQty1 = item.upkeepQty
      //       }
      //     })
      //     return res
      //   },
      remoteUrl: {
        url: upcloud_base_gsp + '/gspUpkeepCheck/findPlan',
        headers: {
          'Content-Type': 'application/JSON'
        },
        method: 'get'
      },
      ref: 'MainTable',
      rowkey: 'id',
      columns: sorter([
        {
          title: '仓库',
          dataIndex: 'warehouseName',
          key: 'warehouseName'
        },
        {
          title: '货主',
          dataIndex: 'cargoOwnerName',
          key: 'cargoOwnerName'
        },
        {
          title: '建筑物',
          dataIndex: 'bldgName',
          key: 'bldgName'
        },

        {
          title: '单据编号',
          dataIndex: 'code',
          key: 'code'
        },
        {
          title: '日期',
          dataIndex: 'gmtCreate',
          key: 'gmtCreate'
        },
        {
          title: '养护类型',
          dataIndex: 'upkeepTypeDesc',
          key: 'upkeepTypeDesc'
          //   customRender(text, record, index) {
          //     text == 0 && (text = '一般')
          //     text == 1 && (text = '重点')
          //     return text
          //   }
        },
        {
          title: '类别',
          dataIndex: 'source',
          key: 'source',
          customRender(text, record, index) {
            text == '0' && (text = '手动')
            text == '1' && (text = '自动')
            return text
          }
        },
        {
          title: '打印次数',
          dataIndex: 'printCount',
          key: 'printCount'
        },
        {
          title: '单据ID',
          dataIndex: 'id',
          key: 'id',
          hidden: true
        }
      ])
    },
    //   详情表
    SubTable: {
      title: '明细列表',
      remoteUrl: {
        url: baseUrl + '/print/getGSPUpkeepPrintData',
        headers: {
          'Content-Type': 'application/JSON'
        },
        method: 'get'
      },
      ref: 'rule',
      doneBefore(res) {
        let head = res.data[0].head
        let table = res.data[0].table
        that.setSubTableData(table)
        console.log(res)

        // return res
      },
      rowkey: 'id',
      columns: sorter([
        {
          title: '养护计划日期',
          dataIndex: 'planUpkeepDate',
          type: 'txt',
          width: 180,
          customRender(text) {
            if (text) {
              return moment(text).format('YYYY-MM-DD')
            } else return
          },
          format: 'YYYY-MM-DD',
          key: 'planUpkeepDate',
          scopedSlots: { customRender: 'planUpkeepDate' }

          //   slots: { title: 'warehouseName' }
        },
        // ...genBuilding(
        //   1,
        //   true,
        //   false,
        //   function(res, p) {
        //     p.params.warehouseId = warehouseId
        //     return p
        //   },
        //   function(rowData, form) {
        //     bldgId = rowData[0].bldgId
        //   },
        //   {}
        // ),
        {
          dataIndex: 'bldgName',
          title: '建筑物名称',
          type: 'txt'
        },
        // {
        //   title: '建筑物',
        //   dataIndex: 'bldgId',
        //   type: 'edit',
        //   key: 'bldgId'
        // },
        // {
        //   title: '货主名称',
        //   dataIndex: 'cargoOwnerName',
        // //   type: 'searchModal',
        //   key: 'cargoOwnerName',
        //   ...genwarehouse()
        // },
        // ...genCargoowner(),
        {
          title: '货主名称',
          type: 'txt',
          name: 'cargoOwnerName',
          ellipsis: true,
          dataIndex: 'cargoOwnerName'
        },
        {
          title: '实际货位',
          type: 'txt',
          name: 'locatorName',
          dataIndex: 'locatorName'
        },

        {
          title: '商品编号',
          dataIndex: 'itemCode',
          type: 'txt',
          key: 'itemCode'
        },
        {
          title: '商品名称',
          dataIndex: 'itemName',
          ellipsis: true,
          type: 'txt',
          key: 'itemName'
        },
        // genItemSearchModal(
        //   2,
        //   false,
        //   false,
        //   function(res, p) {
        //     p.data.cargoOwnerId = cargoOwnerId
        //     p.data.warehouseId = warehouseId
        //     console.log(p, '====')
        //     return p
        //   },
        //   function(value, index) {
        //     console.log(value, 'value', arguments)
        //   },
        //   { span: 6, labelCol: { span: 6 }, wrapperCol: { span: 14 } }
        // ),
        {
          title: '批号',
          ellipsis: true,
          dataIndex: 'lotNum',
          align: 'center',
          type: 'txt',
          key: 'lotNum'
        },
        {
          title: '商品大类',
          dataIndex: 'itemHealthCategoryName',
          ellipsis: true,
          type: 'txt',
          key: 'itemHealthCategoryName'
        },
        {
          title: '批准文号',
          dataIndex: 'itemHealthApprovalNbr',
          ellipsis: true,
          type: 'txt',
          key: 'itemHealthApprovalNbr'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          ellipsis: true,
          type: 'edit',
          key: 'remark'
        },
        {
          title: '库存数量',
          dataIndex: 'stockQty',
          //   scopedSlots: { customRender: 'stockQty' },
          align: 'center',
          width: 80,
          type: 'txt',
          key: 'stockQty'
        },
        {
          title: '养护数量',
          ellipsis: true,
          dataIndex: 'upkeepQty',
          //   scopedSlots: { customRender: 'upkeepQty' },
          align: 'center',
          width: 100,
          type: 'number',
          key: 'upkeepQty',
          on: {
            change(data) {
              //   console.log(data)
              //   let num
              //   if (data.record.id == that.ids.creatRowid) {
              //     let ss = that.ids.data.filter(item => {
              //       return item.id.replace(/_copy_\d*/, '') == that.ids.id
              //     })
              //     console.log(ss)
              // let upkeepQty = ss.stockQty
              // if (ss) {
              //   num = upkeepQty - data.value
              //   console.log(num)
              //   that.setRowQTy(that.ids.id, num)
              // }
              //   }
            }
          }
        },
        {
          title: '计划数量',
          ellipsis: true,
          align: 'center',
          width: 100,
          dataIndex: 'planStockQty',
          type: 'number',
          key: 'planStockQty',
          on: {
            change(data) {}
          }
        },
        {
          title: '养护结论',
          dataIndex: 'upkeepConclusion',
          required: true,
          type: 'select',
          options: [
            { label: '待处理', value: 0 },
            { label: '合格', value: 1 },
            // { label: '不合格', value: 2 }
          ],
          customRender(text) {
            text == 0 && (text = '待处理')
            text == 1 && (text = '合格')
            text == 2 && (text = '不合格')
            return text
          },
          key: 'upkeepConclusion'
        },
        {
          title: '待处理原因',
          dataIndex: 'pendingReason',
          ellipsis: true,
          type: 'edit',
          key: 'pendingReason'
        },
        {
          title: '养护待处理数量',
          dataIndex: 'pendingQty',
          ellipsis: true,
          type: 'edit',
          key: 'pendingQty'
        },
        {
          title: '外观',
          dataIndex: 'checkWaiGuan',
          type: 'edit',
          width: 50,
          key: 'checkWaiGuan',
          slots: { title: 'checkWaiGuanTitle' },
          scopedSlots: { customRender: 'checkWaiGuan' }
        },
        {
          title: '包装',
          dataIndex: 'checkBaoZhuang',
          type: 'edit',
          width: 50,
          align: 'center',
          key: 'checkBaoZhuang',
          slots: { title: 'checkBaoZhuangTitle' },
          scopedSlots: { customRender: 'checkBaoZhuang' }
        },
        {
          title: '存储条件',
          dataIndex: 'checkChenChuTiaoJian',
          type: 'edit',
          width: 80,
          align: 'center',
          key: 'checkChenChuTiaoJian',
          slots: { title: 'checkChenChuTiaoJianTitle' },
          scopedSlots: { customRender: 'checkcheckChenChuTiaoJianFanDuo' }
        },
        {
          title: '翻跺',
          dataIndex: 'checkFanDuo',
          key: 'checkFanDuo',
          width: 50,
          align: 'center',
          slots: { title: 'checkFanDuoTitle' },
          scopedSlots: { customRender: 'checkFanDuo' }
        },
        {
          title: '除湿',
          dataIndex: 'checkChuShi',
          type: 'switch',
          width: 50,
          align: 'center',
          key: 'checkChuShi',
          slots: { title: 'checkChuShiTitle' },
          scopedSlots: { customRender: 'checkChuShi' }
        },
        {
          title: '加湿',
          dataIndex: 'checkJiaShi',
          key: 'checkJiaShi',
          width: 50,
          align: 'center',
          type: 'switch',
          slots: { title: 'checkJiaShiTitle' },
          scopedSlots: { customRender: 'checkJiaShi' }
        },
        {
          title: '升温',
          dataIndex: 'checkShenWen',
          key: 'checkShenWen',
          width: 50,
          align: 'center',
          type: 'switch',
          slots: { title: 'checkShenWenTitle' },
          scopedSlots: { customRender: 'checkShenWen' }
        },
        {
          title: '降温',
          dataIndex: 'checkJiangWen',
          key: 'checkJiangWen',
          type: 'switch',
          width: 50,
          align: 'center',
          slots: { title: 'checkJiangWenTitle' },
          scopedSlots: { customRender: 'checkJiangWen' }
        },
        {
          title: '通风',
          dataIndex: 'checkTongFeng',
          key: 'checkTongFeng',
          type: 'switch',
          width: 50,
          align: 'center',
          slots: { title: 'checkTongFengTitle' },
          scopedSlots: { customRender: 'checkTongFeng' }
        },
        {
          title: '除尘',
          dataIndex: 'checkChuChen',
          key: 'checkChuChen',
          type: 'switch',
          width: 50,
          align: 'center',
          slots: { title: 'checkChuChenTitle' },
          scopedSlots: { customRender: 'checkChuChen' }
        },
        {
          title: '上市许可持有人',
          dataIndex: 'itemHealthListedLicensor',
          ellipsis: true,
          type: 'txt',
          key: 'itemHealthListedLicensor'
        },
        {
          title: '生产厂家',
          dataIndex: 'manufacturerName',
          ellipsis: true,
          type: 'txt',
          key: 'manufacturerName'
        },
        {
          title: '生产日期',
          dataIndex: 'pdateFrom',
          type: 'txt',
          format: 'YYYY-MM-DD',
          customRender(text) {
            if (text) {
              return moment(text).format('YYYY-MM-DD')
            } else return
          },
          key: 'pdateFrom'
        },
        {
          title: '有效期至',
          dataIndex: 'validUntil',
          type: 'txt',
          format: 'YYYY-MM-DD',
          key: 'validUntil',
          customRender(text) {
            if (text) {
              return moment(text).format('YYYY-MM-DD')
            } else return
          }
        },
        {
          title: '质量状况',
          dataIndex: 'qualityStatus',
          type: 'txt',
          key: 'qualityStatus',
          customRender(text) {
            switch (text) {
              case '1':
                text = '合格'
                break
              case '2':
                text = '不合格'
                break
              case '3':
                text = '待检'
                break
              case '4':
                text = '退货'
                break
            }
            // console.log(text)
            return text
          }
        }
        //   {
        //     title: '操作',
        //     key: 'operation',
        //     scopedSlots: { customRender: 'operation' },
        //     align: 'center'
        //   }
      ])
    },
    modalItems: {
      rowkey: 'locatorId',
      remoteUrl: {
        url: SERVICE_DATA_API_BASE + '/search/page',
        method: 'post'
      },
      remoteBefore(vn, params) {
        let p = { ...params }
        p.data = {
          num: '589130005049643008',
          pageNum: params.data.pageNum,
          pageSize: params.data.pageSize,
          params: params.data
        }
        return p
      },
      title: '提取商品',
      gridformref: 'gridformModelFormType',
      ref: 'modelFormType',
      items: [
        //genWarehouseAuthSearchModal 仓库

        ...genWarehouseAuthSearchModal(1, true, false, null, changecargoOwnerfun, {
          span: 6,
          labelCol: { span: 6 },
          wrapperCol: { span: 14 }
        }),
        ...genCargoownerAuthSearchModal(1, true, false, null, null, {
          span: 6,
          labelCol: { span: 6 },
          wrapperCol: { span: 14 }
        }),

        {
          type: 'select',
          span: 6,
          name: 'refBillType',
          label: '不合格品来源',
          value: '', //初始化值
          options: [
            { label: '所有', value: '' },
            { label: '入库', value: '1' },
            { label: '移库', value: '2' }
          ]
        },
        {
          type: 'input',
          span: 6,
          name: 'itemId',
          label: '商品',
          placeholder: '请输入产品名称或助记码或产品编',
          value: '' //初始化值
        },
        {
          span: 24,
          type: 'buttonGroup',
          style: { textAlign: 'right' },
          options: [
            {
              value: '查询',
              icon: 'search',
              btnType: 'primary',
              style: 'margin-right:10px;margin-left:10px;',
              htmlType: 'submit'
            },
            {
              value: '重置',

              name: 'ccz',
              icon: 'redo',
              on: {
                click(event, form) {
                  form.resetFields()
                }
              }
            }
          ]
        }
      ],
      columns: sorter([
        {
          title: '日期',
          dataIndex: 'refBillDate',
          ellipsis: true,
          key: 'refBillDate'
        },
        {
          title: '原单据编号',
          dataIndex: 'refBillId',
          ellipsis: true,
          key: 'refBillId'
        },
        {
          title: '不合格品来源',
          dataIndex: 'refBillType',
          ellipsis: true,
          key: 'refBillType',
          width: 100,
          customRender(text, record, index) {
            text == 1 ? ((text = '入库') ? text == 2 : (text = '移库')) : (text = '暂无')
            return text
          }
        },
        {
          title: '不合格原因',
          dataIndex: 'unqualifiedReason',
          ellipsis: true,
          key: 'unqualifiedReason'
        },
        {
          title: '不合格数量',
          dataIndex: 'unqualifiedQty',
          key: 'unqualifiedQty',
          hidden: true
        },

        {
          title: '数量',
          key: 'lossQty',
          dataIndex: 'lossQty',
          width: 100,
          scopedSlots: { customRender: 'number' }
        },
        {
          title: '所在部门意见',
          key: 'opinion',
          ellipsis: true,
          dataIndex: 'opinion'
        },
        {
          title: '商品编码',
          key: 'itemCode',
          ellipsis: true,
          dataIndex: 'itemCode'
        },
        {
          title: '商品名称',
          key: 'itemName',
          ellipsis: true,
          dataIndex: 'itemName'
        },
        {
          title: '助记码',
          key: 'itemAssistantCode',
          ellipsis: true,
          dataIndex: 'itemAssistantCode'
        },
        {
          title: '生产厂家',
          key: 'manufacturerName',
          ellipsis: true,
          dataIndex: 'manufacturerName'
        },
        {
          title: '货位',
          key: 'locatorCode',
          ellipsis: true,
          dataIndex: 'locatorCode'
        },
        {
          title: '货位ID',
          key: 'locatorId',
          ellipsis: true,
          hidden: true,
          dataIndex: 'locatorId'
        },
        {
          title: '批号',
          key: 'lotNum',
          ellipsis: true,
          dataIndex: 'lotNum'
        },
        {
          title: '是否追溯',
          key: 'itemHealthSuperviseflag',
          ellipsis: true,
          dataIndex: 'itemHealthSuperviseflag',
          customRender(text, record, index) {
            text ? (text = '是') : (text = '否')
            return text
          }
        },
        {
          title: '商品规格',
          key: 'specification',
          ellipsis: true,
          dataIndex: 'specification'
        },
        {
          title: '产地',
          key: 'countryOriginName',
          ellipsis: true,
          dataIndex: 'countryOriginName'
        },

        {
          title: '上市许可持有人',
          key: 'itemHealthListedLicensor',
          ellipsis: true,
          width: 150,
          dataIndex: 'itemHealthListedLicensor'
        },
        {
          title: '包装数量',
          key: 'caseConversionRate',
          ellipsis: true,
          dataIndex: 'caseConversionRate'
        },
        {
          title: '包装单位',
          key: 'caseUom',
          ellipsis: true,
          dataIndex: 'caseUom'
        }
      ]),
      ModalItemsOpinion: {
        ref: 'Opinion',
        items: [
          {
            type: 'input',
            span: 12,
            name: 'opinion',
            label: '意见',
            value: '' //初始化值
          }
        ]
      }
    }
  }
}

export default tablecolumns
