<template>
  <div class="collection">
    <p-spin size="small" :spinning="spinning">
      <p-html
        ref="phtml"
        :items="items"
        :formTable="formTable"
        :pagination="pagination"
        :columns="columns"
        :keys="keys"
        :remoteDatas="remoteDatas"
        :dataSource="dataSource"
        :resetData="resetData"
        @selectChange="selectChange"
        @changePagination="changePagination"
        @changeClick="changeClick"
        @delClick="delClick"
        @hanlderSubmit="hanlderSubmit"
        @delAll="delAll"
        @addName="addItem"
      ></p-html>
    </p-spin>
    <p-modal
      :title="title"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      width="80%"
      :enlargeable="true"
      :draggable="true"
      :destroyOnClose="true"
      :maskClosable="false"
    >
      <div>
        <h3>主要信息</h3>
        <p-grid-form  ref="forms1"  :items="addItems">
          <template slot="diy" slot-scope="value, form">
            <p-cascader-zone-wms
              :paramValue="fields"
              @change="
                (...args) => {
                  handerChange(...args, form)
                }
              "
            />
          </template>
        </p-grid-form>
        <a-divider />
        <h3>扩展字段</h3>
        <p-grid-form :span="8" ref="forms" :label-col="{ span: 6 }" :items="addItems1"> </p-grid-form>
      </div>
    </p-modal>
  </div>
</template>

<script>
// import pick from 'lodash.pick'
import { setwarehouse } from './config/warehousemixin.js'
import pHtml from './components/commonHtml.vue'
import API from './api/default.js'
import { columns } from './config/extrepot.js'
import { warehousePage, warehouseAdd, delWarehouse, warehouseUpdate, warehouseId, remoteData } from './api/api.js'
import { genWarehouseSearchModal,
		  genContainerSearchModal,
		} from "@/views/kzyWMS/SearchModal";
import { remote } from './config/remote'
import pCascaderZoneWms from '@/blocks/CascaderZone/wms.index.js'
import { sorter } from '../../utils/sort'
let grid = {
  labelCol: { span: 6 },
  span: 8
}
const addItems = function(that) {
  return [
    {
      type: 'input',
      name: 'warehouseName',
      label: '仓库名称',
      span:8,
      labelCol: {span: 6},
      required: true,
      value: ''
    },
    {
      type: 'input',
      name: 'warehouseCode',
      label: '仓库编码',
	    span:8,
	    labelCol: { span: 6 },
      required: true
    },
    {
      type: 'select',
      name: 'warehouseType',
      label: '仓库类型',
	  span:8,
	  labelCol: { span: 6 },
      required: true,
      ...remote('WH_TYPE')
    },
    {
      type: 'search',
      name: 'dcName',
      label: '仓配中心',
      title: '仓配中心',
      required: true,
      span:8,
      labelCol: { span: 6 },
      value: '',
      toolBar: {
        items: [
          {
            type: 'input',
            name: 'dcName',
            label: '仓配中心名称',
            span: 8,
            value: ''
          },
          {
            type: 'input',
            name: 'dcCode',
            label: '仓配中心编码',
            span: 8,
            value: ''
          }
        ]
      },
      tableConfig: {
        rowKey: 'dcName',
        rowSelectType: 1,
        hasRowSelectCheck: true,
        columns: [
          { dataIndex: 'dcName', title: '仓配中心名称', key: 'dcName' },
          { dataIndex: 'dcCode', title: '仓配中心编码', key: 'dcCode' },
          { dataIndex: 'dcId', title: '仓配中心ID', key: 'dcId' }
        ]
      },
      value: '',
      remoteUrl: API.wmsdcPage,
      on: {
        ok(event, value, rowData, form) {
          that.dcRecord = rowData[0]
        }
      }
    },
    {
      type: 'number',
      name: 'sortIndex',
      value: 1,
      min: 1,
	  span:8,
	  labelCol: { span: 6 },
      label: '排序索引'
    },
    
    {
      type: 'select',name: 'warehouseLayoutType',label: '仓库布局类型',span:8,labelCol: { span: 6 },
      options: [
        {value: '平面',label: '平面'},
        {value: '立体',label: '立体'},
        {value: '自动化',label: '自动化'}
      ]
    },
    {type: 'input',name: 'warehouseManagerName',span:8,labelCol: { span: 6 },label: '仓库联系人'},
    {type: 'input',name: 'phone',span:8,labelCol: { span: 6 },label: '电话'},
	{type: 'switch',name: 'isEnable',value: true,span:8,labelCol: { span: 6 },required: true,label: '是否启用'},
    {span:8,labelCol: { span: 6 },type: 'select',name: 'province1',label: '省/市/区',scopedSlots: { customRender: 'diy' }},
	{ type:'select',name:'scanSupervisionCodeStage',label:'扫描监管码节点',span:8,labelCol:{span:6},
		options:[
			{ label:'整件拣货、零货拣货、功能库房拣货',value:0 },
			{ label:'整件外复核、零货拣货、功能库房拣货',value:1 },
			{ label:'整件拣货、零货内复核',value:2 },
			{ label:'整件外复核、零货内复核',value:3 },
		]
	},
    {span: 16,labelCol: {span: 3},wrapperCol:{ span:19}, type: 'textarea',name: 'address',label: '地址',rows:1 },
  ]
}
const addItems1 = function(that) {
  return [
    {
      type: 'input',
      name: 'attribute11',
      label: '预留字段1',
	  span:8,
	  labelCol: { span: 6 }
    },
    {
      type: 'input',
      name: 'attribute12',
      label: '预留字段2',
	  span:8,
	  labelCol: { span: 6 }
    },
    {
      type: 'input',
      name: 'attribute13',
      label: '预留字段3',
	  span:8,
	  labelCol: { span: 6 }
    },
    {
      type: 'input',
      name: 'attribute14',
      label: '预留字段4',
	  span:8,
	  labelCol: { span: 6 }
    },
    {
      type: 'input',
      name: 'attribute15',
      label: '预留字段5',
	  span:8,
	  labelCol: { span: 6 }
    },
    {
      type: 'input',
      name: 'segment11',
      label: '预留字段6',
	  span:8,
	  labelCol: { span: 6 }
    },
    {
      type: 'input',
      name: 'segment12',
      label: '预留字段7',
	  span:8,
	  labelCol: { span: 6 }
    },
    {
      type: 'input',
      name: 'segment13',
      label: '预留字段8',
	  span:8,
	  labelCol: { span: 6 }
    },
    {
      type: 'input',
      name: 'segment14',
      label: '预留字段9',
	  span:8,
	  labelCol: { span: 6 }
    },
    {
      type: 'input',
      name: 'segment15',
      label: '预留字段10',
	  span:8,
	  labelCol: { span: 6 }
    }
  ]
}
export default {
  name: 'Collection',
  mixins: [setwarehouse],
  data() {
    return {
      columns:null,
      addItems: addItems(this),
      addItems1: addItems1(this),
      confirmLoading: false,
      visible: false,
      fields: [],
      keys: 'warehouseId',
      title: '新增仓库',
      warehouseCode: '', // 编码
      warehouseName: '', // 名称
      remoteDatas: [],
      resetData: {},
      dcRecord: {}, // 搜索框数据
      spinning: false,
      selectedRowKeys: [],
      dataSource: [], // 表格数据
	  // 搜索字段, 传递给p-html子组件展示
      items: [
        // form 表单
        //         {
        //     labelCol: { span: 8 },
        //     span: 6
        //   }

        // ...genwarehouse({
        //   labelCol: { span: 5 },
        //   span: 8,
        //   wrapperCol: { span: 19 }
        // }),

        // {
        //   type: 'input',
        //   name: 'warehouseName',
        //   label: '仓库名称',
		    //   span:8,
        //   labelCol: {span: 6},
        //   value: ''
        // },
        {
          type: 'input',
          name: 'warehouseName',
          label: '仓库名称',
          span:8,
          labelCol: {span: 6},
          value: ''
        },
        {
          type: 'input',
          name: 'warehouseCode',
          label: '仓库编码',
          span:8,
          labelCol: {span: 6},
          value: ''
        },
        {
          type: 'input',
          name: 'xxxx1',
          label: '配送中心编码',
          span:8,
          labelCol: {span: 6},
          value: ''
        },
        {
          type: 'select',
          name: 'isEnable',
          label: '是否启用',
          span:8,
          labelCol: {span: 6},
          value: '',
          options: [
            { title: '全部', value: '' },
            { title: '启用', value: '1' },
            { title: '停用', value: '0' }
          ]
        }
      ],
      formTable: {
        // 表格头部按钮
        title: '仓库资料定义',
        options: [
          // button
          {
            name: '新增',
            icon: 'plus',
            action: 'addName'
          },
          {
            name: '批量删除',
            icon: 'delete',
            action: 'delAll'
          }
        ]
      },
      pagination: {
        total: 0,
        pageSize: 10, //每页中显示10条数据
        current: 1,
        showSizeChanger: true,
        pageSizeOptions: ['10', '15', '20'], //每页中显示的数据
        showTotal: total => `共有 ${total} 条数据` //分页中显示总的数据
      }
    }
  },
  components: {
    pHtml,
    pCascaderZoneWms
  },
  created() {
    this.getQueryData()
    this.remoteData()
    this.columns = sorter(columns)
  },
  mounted() {
    // this.SetWarehouse()
  },
  methods: {
    // SetWarehouse() {
    //   let warehouse = this.$store.state.user.warehouse
    //   if (Object.keys(warehouse).length > 0) {
    //     this.$refs.phtml.$refs.gridform.form.setFieldsValue(
    //       pick(warehouse, 'warehouseId', 'warehouseCode', 'warehouseName')
    //     )
    //   }
    // },
    handerChange(...args) {
      this.fields = args[0][0]
    },
    remoteData() {
      // 字典
      let params = {
        type: 'WH_TYPE'
      }
      remoteData(params).then(res => {
        if (res.success) {
          this.remoteDatas = res.data
        } else {
          this.$exception(res)
        }
      })
    },
    getQueryData() {
      // 查询
      let params = {
        warehouseName: this.warehouseName, // 名称
        warehouseCode: this.warehouseCode, // 编码
        pageNum: this.pagination.current, // 当前页码
        pageSize: this.pagination.pageSize // 每页数量
      }
      this.spinning = true
      warehousePage(params).then(res => {
        this.spinning = false
        if (res.success) {
          this.dataSource = res.data.list
          this.pagination.total = Number(res.data.totalCount)
        } else {
          this.$exception(res)
        }
      })
    },
    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      this.selectedRowKeys = selectedRowKeys
    },
    getwarehouseId() {
      // 根据id查询数据
      let params = {
        id: this.resetData.warehouseId // 名称
      }
      warehouseId(params).then(res => {
        if (res.success) {
          let values = res.data
          this.dcRecord.dcId = values.dcId
          this.fields = [
            values.baseWmsWarehouseProfiles.province ? values.baseWmsWarehouseProfiles.province : '',
            values.baseWmsWarehouseProfiles.city ? values.baseWmsWarehouseProfiles.city : '',
            values.baseWmsWarehouseProfiles.area ? values.baseWmsWarehouseProfiles.area : ''
          ]
          this.$nextTick(() => {
            this.$refs.forms1.form.resetFields()
            this.$refs.forms1.form.setFieldsValue({
			  scanSupervisionCodeStage: values.scanSupervisionCodeStage,	//监管码节点
			  
              warehouseCode: values.warehouseCode,
              warehouseName: values.warehouseName,
              warehouseType: values.warehouseType ? values.warehouseType : '',
              dcName: this.resetData.dcName ? this.resetData.dcName : '', // 这个数据后台说从page拿，id查询字段没取值
              costingMethod: values.costingMethod ? values.costingMethod : '',
              warehouseIdPhy: values.warehouseIdPhy ? values.warehouseIdPhy : '',
              warehouseIdGap: values.warehouseIdGap ? values.warehouseIdGap : '',
              sortIndex: values.sortIndex ? values.sortIndex : '',
              isEnable: values.isEnable == '1' ? true : false,
              warehouseLayoutType: values.baseWmsWarehouseProfiles.warehouseLayoutType
                ? values.baseWmsWarehouseProfiles.warehouseLayoutType
                : '',
              warehouseWorkingMethod: values.baseWmsWarehouseProfiles.warehouseWorkingMethod
                ? values.baseWmsWarehouseProfiles.warehouseWorkingMethod
                : '',
              province1: [
                values.baseWmsWarehouseProfiles.province ? values.baseWmsWarehouseProfiles.province : '',
                values.baseWmsWarehouseProfiles.city ? values.baseWmsWarehouseProfiles.city : '',
                values.baseWmsWarehouseProfiles.area ? values.baseWmsWarehouseProfiles.area : ''
              ],
              address: values.baseWmsWarehouseProfiles.address ? values.baseWmsWarehouseProfiles.address : '',
              warehouseManagerName: values.baseWmsWarehouseProfiles.warehouseManagerName
                ? values.baseWmsWarehouseProfiles.warehouseManagerName
                : '',
              phone: values.baseWmsWarehouseProfiles.phone ? values.baseWmsWarehouseProfiles.phone : ''
            })
            this.$refs.forms.form.resetFields()
            this.$refs.forms.form.setFieldsValue({
              attribute11: values.baseWmsWarehouseProfiles.attribute11
                ? values.baseWmsWarehouseProfiles.attribute11
                : '',
              attribute12: values.baseWmsWarehouseProfiles.attribute12
                ? values.baseWmsWarehouseProfiles.attribute12
                : '',
              attribute13: values.baseWmsWarehouseProfiles.attribute13
                ? values.baseWmsWarehouseProfiles.attribute13
                : '',
              attribute14: values.baseWmsWarehouseProfiles.attribute14
                ? values.baseWmsWarehouseProfiles.attribute14
                : '',
              attribute15: values.baseWmsWarehouseProfiles.attribute15
                ? values.baseWmsWarehouseProfiles.attribute15
                : '',
              segment11: values.baseWmsWarehouseProfiles.segment11 ? values.baseWmsWarehouseProfiles.segment11 : '',
              segment12: values.baseWmsWarehouseProfiles.segment12 ? values.baseWmsWarehouseProfiles.segment12 : '',
              segment13: values.baseWmsWarehouseProfiles.segment13 ? values.baseWmsWarehouseProfiles.segment13 : '',
              segment14: values.baseWmsWarehouseProfiles.segment14 ? values.baseWmsWarehouseProfiles.segment14 : '',
              segment15: values.baseWmsWarehouseProfiles.segment15 ? values.baseWmsWarehouseProfiles.segment15 : ''
            })
          })
        } else {
          this.$exception(res)
        }
      })
    },
    changeClick(values) {
      // 修改
      this.visible = true
      this.title = '修改仓库'
      this.resetData = values
      this.getwarehouseId()
    },
    delAll() {
      // 批量删除
      let that = this
      if (this.selectedRowKeys.length < 1) return this.$message.warn('请选择仓库！')
      this.$confirm({
        title: '是否删除?',
        okText: '确认',
        cancelText: '取消',
        onOk() {
          that.delAllAjax(that.selectedRowKeys.join(','))
        }
      })
    },
    delClick(record) {
      let that = this
      this.$confirm({
        title: '是否删除?',
        okText: '确认',
        cancelText: '取消',
        onOk() {
          that.delAllAjax(record.warehouseId)
        }
      })
    },
    delAllAjax(val) {
      // 批量删除
      let params = {
        ids: val // 每页数量
      }
      delWarehouse(params).then(res => {
        if (res.success) {
          this.$message.success('删除成功！')
          this.getQueryData()
        } else {
          this.$exception(res)
        }
      })
    },
    hanlderSubmit(event, form) {
      // 查询按钮
      form.validateFields((error, values) => {
        if (!error) {
          this.warehouseName = values.warehouseName
          this.warehouseCode = values.warehouseCode
          this.getQueryData()
        }
      })
    },
    handleOk(e) {
      this.$refs.forms1.form.validateFields((error1, values1) => {
        this.$refs.forms.form.validateFields((error, values) => {
          let val = Object.assign(values, values1)
          if (!error) {
            if (this.title == '修改仓库') {
              // 修改
              this.changeAjax(val)
            } else {
              // 新增
              this.addAjax(val)
            }
          }
        })
      })

      // Object.assign(target, source)
    },
    changeAjax(values) {
      // 修改
      let params = {
		scanSupervisionCodeStage: values.scanSupervisionCodeStage,
		
        warehouseId: this.resetData.warehouseId,
        warehouseCode: values.warehouseCode,
        warehouseName: values.warehouseName,
        warehouseType: values.warehouseType,
        dcName: values.dcName,
        dcId: this.dcRecord.dcId,
        costingMethod: values.costingMethod,
        warehouseIdPhy: values.warehouseIdPhy,
        warehouseIdGap: values.warehouseIdGap,
        sortIndex: values.sortIndex,
        isEnable: values.isEnable ? '1' : '0',
        baseWmsWarehouseProfiles: {
          warehouseLayoutType: values.warehouseLayoutType,
          warehouseWorkingMethod: values.warehouseWorkingMethod,
          province: this.fields[0],
          city: this.fields[1],
          area: this.fields[2],
          address: values.address,
          warehouseManagerName: values.warehouseManagerName,
          phone: values.phone,
          attribute11: values.attribute11,
          attribute12: values.attribute12,
          attribute13: values.attribute13,
          attribute14: values.attribute14,
          attribute15: values.attribute15,
          segment11: values.segment11,
          segment12: values.segment12,
          segment13: values.segment13,
          segment14: values.segment14,
          segment15: values.segment15
        }
      }
      this.confirmLoading = true
      warehouseUpdate(params).then(res => {
        this.confirmLoading = false
        if (res.success) {
          this.visible = false
          this.getQueryData()
        } else {
          this.$exception(res)
        }
      })
    },
    addAjax(values) {
      // 新增
      let params = {
		 scanSupervisionCodeStage: values.scanSupervisionCodeStage,
		  
        warehouseCode: values.warehouseCode,
        warehouseName: values.warehouseName,
        warehouseType: values.warehouseType,
        dcName: values.dcName,
        dcId: this.dcRecord.dcId,
        costingMethod: values.costingMethod,
        warehouseIdPhy: values.warehouseIdPhy,
        warehouseIdGap: values.warehouseIdGap,
        sortIndex: values.sortIndex,
        isEnable: values.isEnable ? '1' : '0',
        baseWmsWarehouseProfiles: {
          warehouseLayoutType: values.warehouseLayoutType,
          warehouseWorkingMethod: values.warehouseWorkingMethod,
          province: this.fields[0],
          city: this.fields[1],
          area: this.fields[2],
          address: values.address,
          warehouseManagerName: values.warehouseManagerName,
          phone: values.phone,
          attribute11: values.attribute11,
          attribute12: values.attribute12,
          attribute13: values.attribute13,
          attribute14: values.attribute14,
          attribute15: values.attribute15,
          segment11: values.segment11,
          segment12: values.segment12,
          segment13: values.segment13,
          segment14: values.segment14,
          segment15: values.segment15
        }
      }
      this.confirmLoading = true
      warehouseAdd(params).then(res => {
        this.confirmLoading = false
        if (res.success) {
          this.visible = false
          this.getQueryData()
        } else {
          this.$exception(res)
        }
      })
    },
    handleCancel(e) {
      this.visible = false
    },
    addItem() {
      // 新增
      this.visible = true
      this.title = '新增仓库'
    },
    changePagination(pagination, filters, sorter) {
      // 分页变化
      this.pagination = pagination
      this.getQueryData()
    }
  }
}
</script>

<style lang="less" scoped>
.detialModal {
  border-bottom: 1px solid #e8e8e8;
  margin-bottom: 20px;
  color: rgba(0, 0, 0, 0.85);
  font-size: 14px;
  line-height: 22px;
  padding: 18px 12px;
}
</style>
