<!-- GSP提取移库开票 -->
<template>
  <div>
    <p-modal
      title="养护不合格品提取"
      destroyOnClose
      :visible="visible"
      :enlargeable="true"
      :draggable="true"
      width="90%"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <p-table
        ref="table2"
        dragle
        auto-width
        hasRowSelectCheck
        :columns="columns2"
        :data-source.sync="data2"
        rowKey="id"
        :remoteUrl="remoteUrl2"
        :remoteParams="remoteUrl2.params"
        :remoteBefore="remoteBefore2"
        :rowSelectType="1"
        @checkRow="checkRow"
        bordered
      >
      </p-table>
    </p-modal>

    <p-card title="GSP提取移库开票">
      <template slot="extra">
        <p-button type="primary" icon="to-top" @click="extract" style="margin-right: 15px">提取</p-button>
        <!-- <p-button type="primary" icon="redo" @click="">重置</p-button> -->
        <p-button type="primary" icon="save" @click="hanlderSubmit">保存</p-button>
      </template>
      <div class="plg-inner-form-mb">
        <p-grid-form ref="search" :items="items" @submit="hanlderSubmit" />
      </div>
      <!-- hasRowSelectCheck -->
      <p-table-edit-pro
        ref="table"
        dragle
        auto-width
        :scroll="{ x: true }"
        :columns="columns"
        :data-source.sync="data"
        rowKey="gspLineId"
        :rowSelectType="false"
        :autoLoad="false"
        :remoteUrl="remoteUrl"
        :remoteParams="remoteUrl.params"
        bordered
      >
      </p-table-edit-pro>
    </p-card>
  </div>
</template>

<script>
// 设置默认仓库
import { setwarehouse } from '@/views/kzyWMS/SearchModal/mixins_warehouse.js'
import { items, columns, columns2 } from './config/GSP_extract.js'
import { dict_listbytype, move_gsp_add } from '../api/api.js'
import { mapGetters } from 'vuex'
import { upcloud_base_gsp } from '@/config/serviceSettings'
import { getCargoOwner } from '../../utils/index'

export default {
  mixins: [setwarehouse],
  computed: { ...mapGetters['userInfo'] },
  data() {
    return {
      items,
      columns,
      columns2,
      data: [],
      data2: [],
      common_stockstatus: [],
      selectedRows: [],
      visible: false,
      remoteUrl: {
        url: upcloud_base_gsp + '/gspUpkeepCheck/findUpkeepItem4MovePick',
        method: 'get',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        params: {},
      },
      remoteUrl2: {
        url: upcloud_base_gsp + '/gspUpkeepCheck/findUpkeep4MovePick',
        method: 'get',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        params: {},
      },
    }
  },
  methods: {
    hanlderSubmit(event, form) {
      event.preventDefault()
      if (this.data.length) {
        this.$refs.table.validateFields((err, values) => {
          if (!err) {
            let params = this.data.map((item) => {
              return Object.assign(item, {
                warehouseWorkerId: this.$refs.search.form.getFieldsValue().warehouseWorkerId, //库工id
                billId: item.gspBillId,
                billNo: item.gspBillNo,
                lineId: item.gspLineId,
                comments: this.$refs.search.form.getFieldsValue().comments, //备注
              })
            })
            move_gsp_add(params).then((res) => {
              if (res.success) {
                this.$message.success('保存成功！')
                this.data=[]
              } else {
                this.$exception(res)
              }
            })
          }
        })
      } else {
        this.$message.warning('无数据！')
      }
      /* let _this = this ;
			this.$refs.search.form.validateFields((err, values) => {
			        if (!err) {
			          console.log('验证通过: ', values);
					  _this.remoteUrl.params = {...values};
			        }
			      }); */
    },
    // 重置
    hanlderRest(event, form) {
      this.$refs.search.form.resetFields()
    },
    async getAuthBycargoOwner() {
      await getCargoOwner().then((res) => {
        if (res.success) {
          this.$nextTick(() => {
            this.$refs.search.form.setFieldsValue({
              cargoOwnerId: res.data.list[0].companyId,
              cargoOwnerName: res.data.list[0].companyName,
              cargoOwnerCode: res.data.list[0].companyCode,
            })
          })
        }
        if (!res.success) {
          this.$exception(res)
          return
        }
      })
    },
    // 提取确认
    handleOk() {
      console.log(this.selectedRows, 'this.selectedRowsthis.selectedRowsthis.selectedRows')
      if (this.selectedRows.length) {
        this.$refs.search.form.setFieldsValue({
          warehouseId: this.selectedRows[0].warehouseId,
          cargoOwnerId: this.selectedRows[0].cargoOwnerId,
          cargoOwnerCode: this.selectedRows[0].cargoOwnerCode,
          cargoOwnerName: this.selectedRows[0].cargoOwnerName,
          enterpriseId: this.selectedRows[0].enterpriseId,
          code: this.selectedRows[0].code,
          source: this.selectedRows[0].source,
          status: this.selectedRows[0].status,
          creatorName: this.selectedRows[0].creatorName,
          modifierName: this.selectedRows[0].modifierName,
          gmtModified: this.selectedRows[0].gmtModified,
          comments: this.selectedRows[0].remark,
        })
        console.log(this.$refs.search.form.getFieldsValue())
        this.$refs['search'].form.disableItem()
        // 备注不禁用
        this.$refs['search'].form.enableItem(['comments'])
        this.visible = false
        this.remoteUrl.params = { id: this.selectedRows[0].id }
      } else {
        this.$message.warning('请选择数据！')
      }
    },
    handleCancel(e) {
      this.visible = false //关闭弹窗
    },
    checkRow(selectedRowKeys, selectedRows) {
      console.log(selectedRowKeys, selectedRows)
      this.selectedRows = [...selectedRows]
    },
    // 提取
    extract() {
      this.visible = true
    },
    remoteBefore2(vn, p) {
      p.params.warehouseId = this.$refs.search.form.getFieldsValue().warehouseId
      return p
    },
  }, // methods
  created() {
    // this.remoteUrl2.params = {warehouseId:this.$refs.search.form.getFieldsValue().warehouseId}
  },
  mounted() {
    this.getAuthBycargoOwner()
    dict_listbytype({ dicttypeCodes: 'common_stockstatus' }).then((res) => {
      if (res.success) {
        this.common_stockstatus = res.data
        //dictId: "1000000229"  dictName: "初始"
      }
    })
  },
}
</script>

<style scoped lang="less"></style>
