<template>
  <div class="collection">
    <p-spin v-show="!visible" size="small" :spinning="spinning">
      <p-html
        :items="items"
        :formTable="formTable"
        :pagination="pagination"
        :columns="columns"
        :keys="keys"
        :dataSource="dataSource"
        :resetData="resetData"
        @selectChange="selectChange"
        @changePagination="changePagination"
        @changeClick="changeClick"
        @delClick="delClick"
        @hanlderSubmit="hanlderSubmit"
        @delAll="delAll"
        @addName="addItem"
      ></p-html>
    </p-spin>
    <addHtml
      v-if="visible"
      :warehouseRecord="warehouseId"
      :cargoOwnerRecord="cargoOwnerId"
      :title="title"
      @successRequest="successRequest"
      @requestUrl="requestUrl"
    />
  </div>
</template>

<script>
import pHtml from './components/commonHtml.vue'
import addHtml from './components/add.vue'
import API from './api/default.js'
import { columns } from './config/extrepot.js'
import { pageWarehouseitem, addWarehouseitem, delWarehouseitem, unpageWarehouseitem } from './api/api.js'
import { sorter } from '../../utils/sort'
import { genWarehouseAuthSearchModal, genCargoownerAuthSearchModal } from '@/views/kzyWMS/SearchModal/index'
import { changecargoOwnerfun } from '@/views/kzyWMS/utils/setCargoOwner'
const span8 = { span: 8, labelCol: { span: 6 } }
const items = function(that) {
  return [
    // form 表单
    {
      type: 'input',
      name: 'condition',
      label: '查询条件',
      placeholder: '请输入名称/编码/助记码',
      span: 8,

      labelCol: {
        span: 6
      },

      value: ''
    },
    ...genWarehouseAuthSearchModal(1, true, false, null, changecargoOwnerfun, span8),
    ...genCargoownerAuthSearchModal(1, true, false, null, null, span8)
  ]
}
export default {
  name: 'Collection',
  data() {
    return {
      columns: null,
      items: items(this),
      confirmLoading: false,
      visible: false,
      keys: 'drId',
      title: '新增仓商品',
      condition: '', // 名称
      warehouseId: {}, // 仓库id
      cargoOwnerId: {}, // 货主id
      resetData: {},
      spinning: false,
      selectedRowKeys: [],
      dataSource: [], // 表格数据
      formTable: {
        // 表格头部按钮
        title: '仓商品',
        options: [
          // button
          //   {
          //     name: '新增',
          //     icon: 'plus',
          //     action: 'addName'
          //   },
          //   {
          //     name: '批量删除',
          //     icon: 'delete',
          //     action: 'delAll'
          //   }
        ]
      },
      pagination: {
        total: 0,
        pageSize: 10, //每页中显示10条数据
        current: 1,
        showQuickJumper: true,
        showSizeChanger: true,
        pageSizeOptions: ['10', '15', '20'], //每页中显示的数据
        showTotal: total => `共有 ${total} 条数据` //分页中显示总的数据
      }
    }
  },
  components: {
    pHtml,
    addHtml
  },
  created() {
    this.getQueryData()
    this.columns = sorter(columns)
  },
  methods: {
    getQueryData() {
      // 查询
      let params = {
        condition: this.condition, // 名称
        warehouseId: this.warehouseId.warehouseId ? this.warehouseId.warehouseId : '', // 仓库ID
        cargoOwnerId: this.cargoOwnerId.cargoOwnerId ? this.cargoOwnerId.cargoOwnerId : '', // 货主ID
        pageNum: this.pagination.current, // 当前页码
        pageSize: this.pagination.pageSize // 每页数量
      }
      this.spinning = true
      pageWarehouseitem(params).then(res => {
        this.spinning = false
        if (res.success) {
          console.log(res.success)
          this.dataSource = res.data.list
          this.pagination.total = Number(res.data.totalCount)
        } else {
          this.$exception(res)
        }
      })
    },
    requestUrl(val) {
      this.visible = val
    },
    successRequest(val) {
      this.visible = val
      this.getQueryData()
    },
    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      console.log(selectedRowKeys, selectedRows)
      this.selectedRowKeys = selectedRowKeys
    },
    getwarehouseId() {
      // 根据id查询数据
      let params = {
        id: this.resetData.warehouseId // 名称
      }
    },
    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.drId)
        }
      })
    },
    delAllAjax(val) {
      // 批量删除
      let params = {
        ids: val // 每页数量
      }
      delWarehouseitem(params).then(res => {
        if (res.success) {
          console.log(res.success)
          this.$message.success('删除成功！')
          this.getQueryData()
        } else {
          this.$exception(res)
        }
      })
    },
    hanlderSubmit(event, form) {
      // 查询按钮
      console.log(event, form)
      form.validateFields((error, values) => {
        if (!error) {
          console.log(error, values, 'valuesvaluesvaluesvaluesvalues')
          this.condition = values.condition
          this.getQueryData()
        }
      })
    },
    handleOk(e) {
      // this.$refs.forms.form.validateFields((error, values) => {
      //   console.log(error, values, '555555555555555555555555')
      //   if (this.title == '修改仓库') {
      //     // 修改
      //     this.changeAjax(values)
      //   } else {
      //     // 新增
      //     this.addAjax(values)
      //   }
      // })
    },
    changeAjax(values) {
      // 修改
      let params = {
        warehouseId: this.resetData.warehouseId,
        warehouseType: values.warehouseType,
        warehouseLevel: values.warehouseLevel,
        warehouseAttr: values.warehouseAttr,
        warehousePath: values.warehousePath,
        dcName: values.dcName,
        costingMethod: values.costingMethod,
        isPickingEnabled: values.isPickingEnabled ? '1' : '0',
        isExecWms: values.isExecWms ? 'Y' : 'N',
        warehouseIdPhy: values.warehouseIdPhy,
        warehouseIdGap: values.warehouseIdGap,
        pickingRule: values.pickingRule,
        sortIndex: values.sortIndex,
        isEnable: values.isEnable ? '1' : '0',
        baseWmsWarehouseProfiles: {
          warehouseLayoutType: values.warehouseLayoutType,
          warehouseWorkingMethod: values.warehouseWorkingMethod,
          industry: values.industry,
          inMode: values.inMode,
          inWorkFlow: values.inWorkFlow,
          outWorkFlow: values.outWorkFlow,
          province: values.province,
          city: values.city,
          area: values.area,
          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
      unpageWarehouseitem(params).then(res => {
        this.confirmLoading = false
        if (res.success) {
          console.log(res, 'wmsdcAddwmsdcAdd')
          this.visible = false
          this.getQueryData()
        } else {
          this.$exception(res)
        }
      })
    },
    addAjax(values) {
      // 新增
      let params = {
        warehouseType: values.warehouseType,
        warehouseLevel: values.warehouseLevel,
        warehouseAttr: values.warehouseAttr,
        warehousePath: values.warehousePath,
        dcName: values.dcName,
        costingMethod: values.costingMethod,
        isPickingEnabled: values.isPickingEnabled ? '1' : '0',
        isExecWms: values.isExecWms ? 'Y' : 'N',
        warehouseIdPhy: values.warehouseIdPhy,
        warehouseIdGap: values.warehouseIdGap,
        pickingRule: values.pickingRule,
        sortIndex: values.sortIndex,
        isEnable: values.isEnable ? '1' : '0',
        baseWmsWarehouseProfiles: {
          warehouseLayoutType: values.warehouseLayoutType,
          warehouseWorkingMethod: values.warehouseWorkingMethod,
          industry: values.industry,
          inMode: values.inMode,
          inWorkFlow: values.inWorkFlow,
          outWorkFlow: values.outWorkFlow,
          province: values.province,
          city: values.city,
          area: values.area,
          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
      addWarehouseitem(params).then(res => {
        this.confirmLoading = false
        if (res.success) {
          console.log(res, 'wmsdcAddwmsdcAdd')
          this.visible = false
          this.getQueryData()
        } else {
          this.$exception(res)
        }
      })
    },
    addItem() {
      // 新增
      this.visible = true
      this.title = '新增仓商品'
    },
    changePagination(pagination, filters, sorter) {
      // 分页变化
      console.log(pagination, filters, sorter, '分页变化分页变化')
      this.pagination = pagination
      this.getQueryData()
    }
  }
}
</script>

<style lang="less" scoped></style>
