<template>
  <p-card title="批号维护">
    <template slot="extra"> </template>

    <div class="plg-inner-form-mb">
      <p-grid-form
        ref="formTarget"
        :span="8"
        :label-col="{ span: 8 }"
        :items="setFormItems"
        @submit="hanlderSubmit"
      ></p-grid-form>
    </div>
    <p-table
      ref="logTable"
      :scroll="{ x: 3000 }"
      :columns="columns"
      :row-selection="rowSelection"
      rowKey="batchId"
      :remoteUrl="params"
      dragle
      auto-width
      :remoteData="params.params"
      :remoteParams="params.params"
      :data-source="data"
      @clickRow="clickRow"
      bordered
    >
      <div slot="action" href="javascript:;" slot-scope="text">
        <a slot="action" style="margin: 0 5px" @click.stop="edit(text)" v-action:update>修改</a>
      </div>
    </p-table>
    <detailDialog v-if="visible" v-model="visible" :formData="formData" @handleCancel="handleCancel" @save="save"></detailDialog>
  </p-card>
</template>

<script>
// 设置默认仓库
// import { setwarehouse } from '@/views/kzyWMS/SearchModal/mixins_warehouse.js'
import { columns, setFormItems } from './config/outerCheck'
import { secondaryLCLquery, secondaryLCLpost } from './api'
import detailDialog from './modules/detailDialog.vue'
import { sorter } from '../../utils/sort'
import { getCargoOwner } from '../../utils'

import { mapGetters } from 'vuex'
import { upcloud_base_inventory } from '@/config/serviceSettings'
export default {
  name: 'InnerCheck',
  // mixins: [setwarehouse],
  components: { detailDialog },
  data() {
    return {
      data: [],
      params: {
        url: upcloud_base_inventory + '/batch-adjust/v1/item-batchinfo-page',
        params: {},
        timeout: 30000,
        method: 'post',
      },
      columns: null,
      setFormItems: setFormItems(this),
      selectedRows: [],
      visible: false,
      formData: {},
    }
  },
  created() {
    this.columns = sorter(columns)
    this.params.params.warehouseId = this.$store.getters.warehouse.warehouseId
    this.params.params.warehouseName = this.$store.getters.warehouse.warehouseName
  },
  mounted() {
    let branchname = this.$store.getters.warehouse.warehouseName
    let branchid = this.$store.getters.warehouse.warehouseId
    this.getCargoOwnerFirst()
    this.$refs.formTarget.form.setFieldsValue({ warehouseName: branchname, warehouseId: branchid })
  },
  computed: {
    ...mapGetters(['userInfo']),
    rowSelection() {
      let _this = this
      return {
        onSelectAll(selected, selectedRows, changeRows) {
          var checked = selected
          var key = changeRows.map((item) => item.id)
        },
        onSelect(record, selected, selectedRows, nativeEvent) {
          var checked = selected
          var key = record.id
        },
        onChange(selectedRowKeys, selectedRows) {
          _this.selectedRows = selectedRows
        },
      }
    },
  },
  methods: {
    hanlderSubmit(event, form) {
      event.preventDefault()
      this.$refs.formTarget.form.validateFields((error, values) => {
        if (!error) {
          this.params.params = values
        }
      })
    },
    clickRow(form, rowKey, data) {},

    edit(data) {
      this.visible = true
      this.formData = data
    },
    handleCancel() {
      this.visible = false
    },
    save() {
      this.visible = false
      this.$refs.logTable.reload()
    },
    //获取货主信息
    getCargoOwnerFirst() {
      getCargoOwner().then((res) => {
        if (!res.success) {
          this.$exception(res)
          return
        }
        this.$nextTick(() => {
          if (res.data.list.length > 0) {
            this.$refs.formTarget.form.setFieldsValue({
              cargoOwnerId: res.data.list[0].companyId,
              cargoOwnerCode: res.data.list[0].companyCode,
              cargoOwnerName: res.data.list[0].companyName,
            })
          }
        })
        // }
      })
    },
  },
}
</script>
