<template>
  <div class="warehouse">
    <searchTable
      ref="searchTable"
      title="库区定义"
      :columns="columns"
      :remoteUrl="{ url: upcloud_base_wh + '/baseWmsWarehouseArea/page2', method: 'get' }"
      rowKey="warehouseAreaId"
      :formItems="items"
      :tableBtnArray="tableEtraBtns"
      :btnEventArray="btnEventArray"
      :searchColNum="3"
      :tableAutoLoad="false"
      :isQuery="true"
      @select="tableSelect"
    />
    <formModel
      ref="formModel"
      :setFormItems="modalItems"
      :formData="formData"
      :title="title"
      v-model="formModelVisible"
      @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, addItems1 } from './cofing/modalConfig.js'
import { item } from './cofing/searchItems.js'
import { setwarehouse } from '../config/warehousemixin.js'
import {
  baseWmsWarehouseArea_add,
  baseWmsWarehouseArea_edit,
  baseWmsWarehouseArea_delete,
  baseWmsWarehouseRoomMst_infoWithExpand,
} from '../api/api_warehouse.js'
import { upcloud_base_wh } from '@/config/serviceSettings'
export default {
  components: { searchTable, formModel },
  mixins: [setwarehouse],
  data() {
    return {
      upcloud_base_wh,
      rowKey: 'warehouseAreaId',
      items: item(this),
      columns,
      modalItems: formItems(this),
      title: '',
      formModelVisible: false,
      addItems1: addItems1,
      formData: {},
      tableEtraBtns: [
        {
          label: '修改',
          click: (data) => {
            this.editEvent(data)
          },
        },
        {
          label: '删除',
          click: (data) => {
            this.delClick(data)
          },
        },
      ],
      btnEventArray: [
        {
          label: '新增',
          type: 'primary',
          click: () => {
            this.addItem()
          },
        },
        {
          label: '批量删除',
          type: 'primary',
          click: () => {
            this.delAll()
          },
        },
      ],
      selectedRowKeys: [],
      data: [],
      disabledArray: ['warehouseName', 'bldgName'],
    }
  },
  methods: {
    tableSelect(record, selected, selectedRows, nativeEvent) {
      this.selectedRowKeys = selectedRows.reduce((pre, item) => {
        if (item.dcId) {
          pre.push(item.warehouseRoomId)
        }
        return prew
      }, [])
    },
    // 删除主表
    delClick(data) {
      let id = data.warehouseAreaId
      let _this = this
      this.$confirm({
        title: '是否删除?',
        okText: '确认',
        cancelText: '取消',
        onOk() {
          baseWmsWarehouseArea_delete({ ids: id }).then((res) => {
            if (res.success) {
              _this.$message.success(`删除成功！${res.message}`)
              _this.$refs.searchTable.tableReload()
            } else {
              _this.$exception(res)
            }
          })
        },
      })
    },
    delAll() {
      if (this.selectedRowKeys.length < 1) {
        this.$message.error('请至少选择一条数据')
        return
      }
      let id = this.selectedRowKeys.join(',')
      let _this = this
      this.$confirm({
        title: '是否删除?',
        okText: '确认',
        cancelText: '取消',
        onOk() {
          baseWmsWarehouseArea_delete({ ids: id }).then((res) => {
            if (res.success) {
              _this.selectedRowKeys = []
              _this.$message.success(`删除成功！${res.message}`)
              _this.$refs.searchTable.tableReload()
            } else {
              _this.$exception(res)
            }
          })
        },
      })
    },
    handleCancel(e) {
      this.formModelVisible = false
    },
    async handleOk(error, values, title) {
      let data
      if (title == '修改库区') {
        data = { ...this.data, ...values }
      } else {
        data = { ...values }
      }
      data.isEnable = data.isEnable ? 1 : 0
      const params = { ...data }
      const res = title == '修改库区' ? await baseWmsWarehouseArea_edit(params) : await baseWmsWarehouseArea_add(params)
      if (res.success) {
        this.formModelVisible = false
        this.$message.success('操作成功')
        this.$refs.searchTable.tableReload()
      } else {
        this.$exception(res)
      }
    },
    editEvent(data) {
      this.data = data
      this.title = '修改库区'
      this.formModelVisible = true
      this.formData = { ...data}
      // this.formData.warehouseName = data.warehouseName
      // this.formData.bldgName = data.bldgName
      // this.formData.dcName = data.dcName
      this.formData.isEnable = this.formData.isEnable == '1' ? true : false
      this.$nextTick(() => {
        this.$refs.formModel.$refs.formTarget.form.disableItem(this.disabledArray)
      })
    },
    addItem() {
      this.formModelVisible = true
      this.title = '新增库区'
      this.formData = {}
      this.$nextTick(() => {
        this.$refs.searchTable.tableReload()
      })
    },
  },
}
</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>
