<template>
  <div class="logicalArea">
    <searchTable
      ref="searchTable"
      :title="pageTitle"
      :columns="columns"
      :remoteUrl="{ url: upcloud_base_wh+'/baseWmsWarehouseLogic/page2', method: 'get' }"
      :rowKey="rowKey"
      :formItems="items"
      :tableBtnArray="tableEtraBtns"
      :btnEventArray="btnEventArray"
      :searchColNum="3"
      :tableAutoLoad="false"
      :isQuery="true"
      @select="tableSelect"
    />
    <formModel
      ref="formModel"
      :setFormItems="modalItems"
      :formData="formData"
      :title="actionType ? '逻辑区新增' : '逻辑区修改'"
      v-model="formModelVisible"
      @remoteBeforeFM="remoteBefore"
      @handleOk="handleOk"
      @handleCancel="handleCancel"
    />
  </div>
</template>

<script>
import searchTable from '@/views/kzyWMS/specialOutBound/components/searchTable.vue'
import formModel from '@/views/kzyWMS/specialOutBound/components/formModel/formModel.vue'
import { columns } from './cofing/columns.js'
import { formItems } from './cofing/modalConfig.js'
import { item } from './cofing/searchItems.js'
import { setwarehouse } from '../config/warehousemixin.js'
import { upcloud_base_wh } from "@/config/serviceSettings";

import add_logicalArea from './components/add_logicalArea.vue'
import {
  baseWmsWarehouseLogic_add,
  baseWmsWarehouseLogic_edit,
  baseWmsWarehouseLogic_delete,
} from '../api/api_warehouse.js'

export default {
  components: { add_logicalArea, searchTable, formModel },
  mixins: [setwarehouse],
  data() {
    return {
      upcloud_base_wh,
      form: this.$form.createForm(this, { name: 'horizontal_login' }),
      columns,
      items: item(this),
      modalItems: formItems(this),
      pageTitle:'逻辑区分组',
      rowKey: 'warehouseLogicId',
      tableEtraBtns: [
        { label: '修改', click: data => { this.showModal(false, data) } },
        { label: '删除',click: data => {this.delSingle(data)}}
      ],
      btnEventArray: [
        { label: '新增',icon: 'plus',type: 'primary',click: () => {this.showModal(true) }},
        { label: '批量删除',icon: 'delete',type: 'primary', click: () => {this.delAll()} },
      ],
      rows: {},
      addTypeName: '新增逻辑区',
      selectedRows: [],
      selectedRowKeys: [],
      formData: {},
      actionType: false,
      formModelVisible: false,
      data: [],
      visible: false,
      spinning: false,
      ids: '',
      warehouse_page: [], //仓库
      warehouse: this.$store.state.user.warehouse, //vuex库房共享资料
      baseWmsWarehouseRoomMst_page: [], //库房
      baseWmsWarehouseArea_page: [], //库区
      loading: false,
    }
  }, //data
  mounted() {},
  methods: {
    tableSelect(record, selected, selectedRows, nativeEvent) {
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRows.reduce((prew, item) => {
        if (item.warehouseLogicId) {
          prew.push(item.warehouseLogicId)
        }
        return prew
      }, [])
    },
    showModal(bool, record) {
      this.formModelVisible = true
      this.actionType = bool
      this.$nextTick(() => {
        if (bool) {
          this.formData = {}
        } else {
          this.formData = record
          this.formData.isEnable = this.formData.isEnable == 1 ? true : false
        }
      })
    },
    remoteBefore( event, remoteParams, name){
      if(name == 'bldgName') {
        remoteParams.params.warehouseId = this.$refs.formModel.$refs.formTarget.form.getFieldsValue().warehouseId
      }
    },
     async handleOk(error, values, title) {
      const { actionType } = this
      values.isEnable = values.isEnable ? 1 : 0
      const res = actionType
        ? await baseWmsWarehouseLogic_add(values)
        : await baseWmsWarehouseLogic_edit({
            ...values,
            warehouseLogicId: this.formData.warehouseLogicId
          })
      let msg = res.message
      if (res.success) {
        this.formModelVisible = false
        this.$refs.searchTable.tableReload()
        this.$message.success(msg || '操作成功')
      } else {
        this.$exception(res)
      }
    },
    handleCancel() {
      this.formModelVisible = false
    },
    delSingle(data) {
      let ids = data.warehouseLogicId
      this.del(ids)
    },
    delAll() {
      this.del(this.selectedRowKeys.join(','))
    },
    // 查询仓库时设置仓库代码
    setWarehouseCode(event, selectId, selectData) {
      this.$refs.search.form.setFieldsValue({
        warehouseId: selectData[0].warehouseId, //仓库代码
        warehouseCode: selectData[0].warehouseCode //仓库代码
      })
    },
    // 清除查询仓库时设置仓库代码
    clearWarehouseCode(event, selectId, selectData) {
      if (!selectId) {
        this.$refs.search.form.setFieldsValue({
          warehouseCode: '', //仓库代码
          warehouseId: '' //仓库id
        })
      }
    },
    // 选择时的变化, 必须点到 checkbox 上或者'hasRowSelectCheck=true'点击行
    checkRow(selectedRowKeys, selectedRows) {
      this.ids = this.ids = selectedRows.map(item => item.warehouseLogicId).join()
    },

    // 根据库存id删除一条或批量删除
    del(ids) {
      let _this = this
      if (ids) {
        this.$confirm({
          title: '是否删除?',
          okText: '确认',
          cancelText: '取消',
          onOk() {
            baseWmsWarehouseLogic_delete({ ids: ids }).then(res => {
              if (res.success) {
                _this.$refs.searchTable.tableReload()
                _this.$message.success(`删除成功！${res.message}`)
              } else {
                _this.$exception(res)
              }
            })
          }
        })
      } else {
        this.$message.warning('请选择要删除记录')
      }
    },
    // 过滤仓库id为名称
    returnWarehouseName(text) {
      let temp = this.warehouse_page.find(v => v.value == text)
      return temp ? temp.title : text
    },
    // 过滤库房id为名称
    returnWarehouseRoomId(text) {
      let temp = this.baseWmsWarehouseRoomMst_page.find(v => v.value == text)
      return temp ? temp.title : text
    }
  }, //methods
}
</script>

<style scoped lang="less">
::v-deep {
  .ant-card-body {
    text-align: left;
  }
  .ant-card-head-title {
    text-align: left;
  }
  .ant-form-inline .ant-form-item {
    margin-right: 2rem;
  }
}
</style>
