<template>
  <div class="warehouse">
    <searchTable
        ref="searchTable"
        :title="pageTitle"
        :columns="columns"
        :remoteUrl="{ url: upcloud_base_wh+'/baseWmsWarehouseRoomMst/page2', method: 'get' }"
        :rowKey="rowKey"
        :formItems="items"
        :tableBtnArray="tableEtraBtns"
        :btnEventArray="btnEventArray"
        :searchColNum="3"
        :tableAutoLoad="false"
        :isQuery="true"
        @select="tableSelect"
        @remoteBeforeFM="remoteBefore"
    />
    <formModel
        :setFormItems="modalItems"
        :formData="formData"
        :title="title"
        v-model="formModelVisible"
        @handleOk="handleOk"
        @handleCancel="handleCancel"
        @remoteBeforeFM="remoteBeforeFM"
    >
      <div slot="bottomComponets">
        <a-divider/>
        <h3>扩展字段</h3>
        <p-grid-form :span="8" ref="forms1" :label-col="{ span: 10 }" :items="addItems1"/>
      </div>
    </formModel>
  </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 add_warehouse from './components/add_warehouse.vue'
import {setwarehouse} from '../config/warehousemixin.js'
import {upcloud_base_wh} from "@/config/serviceSettings";

import {
  baseWmsWarehouseRoomMst_infoWithExpand,
  baseWmsWarehouseRoomMst_addWithExpand,
  baseWmsWarehouseRoomMst_editWithExpand,
  baseWmsWarehouseRoomMst_delete
} from '../api/api_warehouse.js'

export default {
  components: {add_warehouse, searchTable, formModel},
  mixins: [setwarehouse],
  data() {
    return {
      upcloud_base_wh,
      pageTitle: '库房资料定义',
      rowKey: 'warehouseRoomId',
      items: item(this),
      columns,
      modalItems: formItems(this),
      warehouseId: this.$store.state.user.warehouse.warehouseId,
      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: [],
    }
  },
  methods: {
    tableSelect(record, selected, selectedRows, nativeEvent) {
      this.selectedRowKeys = selectedRows.reduce((prew, item) => {
        if (item.dcId) {
          prew.push(item.warehouseRoomId)
        }
        return prew
      }, [])
    },
    remoteBefore(event, remoteParams, name) {
      if (name == 'bldgName') {
        remoteParams.params.warehouseId = this.warehouseId
      }
    },
    remoteBeforeFM(event, remoteParams, name) {
      if (name == 'bldgName') {
        remoteParams.params.warehouseId = this.$store.state.user.warehouse.warehouseId
      }
    },
    // 删除主表
    delClick(data) {
      let id = data.warehouseRoomId
      let _this = this
      this.$confirm({
        title: '是否删除?',
        okText: '确认',
        cancelText: '取消',
        onOk() {
          baseWmsWarehouseRoomMst_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() {
          baseWmsWarehouseRoomMst_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) {
      console.log(values)
      values.isEnable = values.isEnable ? '1' : '0'
      values.isFunctionRoom = values.isFunctionRoom ? '1' : '0'
      let forms1 = this.$refs.forms1.form.getFieldsValue() // 扩展字段,暂不加入
      let master
      let profil
      if (title == '编辑库房') {
        master = {...this.data, ...values, ...forms1,}
        profil = {...this.data, ...values, ...forms1,}
      } else {
        master = {...values, ...forms1}
        profil = {...values, ...forms1}
      }
      const params = {master, profil}
      const res = title == '编辑库房' ? await baseWmsWarehouseRoomMst_editWithExpand(params) : await baseWmsWarehouseRoomMst_addWithExpand(params)
      if (res.success) {
        this.formModelVisible = false
        this.$message.success('操作成功')
        this.$refs.searchTable.tableReload()
      } else {
        this.$exception(res)
      }
    },
    changeBldg(data, selectData) {
      this.formData = {
        ...this.formData,
        dcId: data[0].dcId,
        warehouseId: data[0].warehouseId,
        warehouseName: data[0].warehouseName,
        dcName: data[0].dcName
      }
    },
    editEvent(data) {
      this.data = data
      this.title = "编辑库房"
      baseWmsWarehouseRoomMst_infoWithExpand({id: data.warehouseRoomId}).then(res => {
        let that = this
        this.formModelVisible = true
        this.formData = res.data.master
        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.formData.isFunctionRoom = this.formData.isFunctionRoom == '1' ? true : false
        console.log(this.formData, 'resresresres编辑')
        this.$nextTick(() => {
          // console.log(res.data.profil,'res.data.profilres.data.profil')
          that.$refs.forms1.form.setFieldsValue(res.data.profil)
          this.$refs.searchTable.tableReload()
        })
      })
    },
    addItem() {
      this.formModelVisible = true
      this.title = "新增库房"
      this.formData = {}
      this.$nextTick(() => {
        this.$refs.forms1.form.resetFields()
        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>
