<template>
  <div>
    <p-card v-if="!isShow" title="批号调整">
      <template slot="extra">
        <a-space>
          <!-- <a-button type="primary" @click="examine" icon="fork" v-action:check>审核</a-button> -->
        </a-space>
      </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"
        :autoLoad="true"
        :row-selection="rowSelection"
        rowKey="batchId"
        :remoteUrl="listRemoteUrl"
        :remoteData="remoteData"
        :data-source="data"
        @clickRow="clickRow"
        bordered
      >
        <div slot="action" href="javascript:;" slot-scope="text,record">
          <!-- <a slot="action" style="margin: 0 5px" @click.stop="edit(text)" v-action:check >审核</a> -->
          <a slot="action" style="margin: 0 5px" @click.stop="onEdit(record)" >修改</a>
        </div>
      </p-table>
      <!-- <detailDialog
        ref="detail"
        v-model="visible"
        :formData="formData"
        @handleCancel="handleCancel"
        @save="save"
      ></detailDialog> -->
    </p-card>
    <edit v-else :detailData="detailData" @close="onClose"></edit>
  </div>
</template>

<script>
// 设置默认仓库
import { setwarehouse } from '@/views/kzyWMS/SearchModal/mixins_warehouse.js'
import { columns, setFormItems } from './config/outerCheck'
import { secondaryLCLquery, listRemoteUrl, postbatch } from './api'
import detailDialog from './modules/detailDialog.vue'
import { getCargoOwner } from '../../utils'
import edit from './edit.vue'
import { mapGetters } from 'vuex'
export default {
  name: 'InnerCheck',
  mixins: [setwarehouse],
  components: { detailDialog, edit },
  data() {
    return {
      detailData:{},
      isShow:false,
      data: [],
      listRemoteUrl,
      remoteData: {
        pageSize: 10,
        pageNum: 1,
        // id: 'adjust-erp-page',
        // params: {},
      },
      columns,
      setFormItems: setFormItems(this),
      selectedRows: [],
      visible: false,
      formData: {},
      selectedRowKeys: [],
    }
  },
  created() {
    // this.params.params.params.warehouseId = this.$store.getters.warehouse.warehouseId
    // this.params.params.params.warehouseName = this.$store.getters.warehouse.warehouseName
    // this.getCargoOwnerFirst()
  },
  mounted() {
    let branchname = this.$store.getters.warehouse.warehouseName
    let branchid = this.$store.getters.warehouse.warehouseId
    this.$refs.formTarget.form.setFieldsValue({ warehouseName: branchname, warehouseId: branchid })
  },
  computed: {
    ...mapGetters(['userInfo']),
    rowSelection() {
      let _this = this
      return {
        selectedRowKeys: _this.selectedRowKeys,
        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
          _this.selectedRowKeys = selectedRowKeys
        },
      }
    },
  },
  methods: {
    onClose() {
     this.isShow = false 
    },
    onEdit(record) {
      this.isShow = true 
      this.detailData = record
    },
    hanlderSubmit(event, form) {
      event.preventDefault()
      this.$refs.formTarget.form.validateFields((error, values) => {
        if (!error) {
          console.log(values,'values');
          this.remoteData = {...values}
          // this.$refs.logTable.reload()
        }
      })
    },
    clickRow(form, rowKey, data) {},
    // examine() {
    //   if (this.selectedRowKeys.length < 1) {
    //     this.$message.error('请至少选择一条数据')
    //     return
    //   }
    //   this.visible = true
    // },
    // edit(data) {
    //   this.visible = true
    //   this.formData = data
    // },
    // handleCancel() {
    //   this.visible = false
    // },
    save(data) {
      this.visible = false
      data.lineidList = this.selectedRowKeys
      postbatch(data).then((res) => {
        if (res.success) {
          this.$emit('save')
          this.$message.success(res.message)
          this.$refs.detail.resetFields()
        } else {
          this.$message.error(res.message)
        }
      })
      this.$refs.logTable.reload()
    },

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