<template>
  <div class="detail">
    <a-card>
      <div slot="title" style="display: inline-block; align-items: center; cursor: pointer" @click="clickEvent">
        <p-icon type="arrow-left" style="color: #1990ff" />
        {{ titleName }}
      </div>
      <div>
        <div>
          <p-grid-form ref="formTarget" :disable-item="disableItem" :items="formItems"> </p-grid-form>
        </div>
        <a-row :gutter="[16, 16]" style="margin-top: 5px">
          <a-col :span="10">
            <p-table
              ref="logTable"
              :columns="tableColumns"
              rowKey="id"
              :expandRowByClick="false"
              :remoteUrl="{
                url: titleName == '报溢' ? '/kz/check/extraHandle' : '/kz/check/reduceHandle',
                method: 'post'
              }"
              :remoteData="remoteParams1"
              :remoteParams="remoteParams1"
              :autoLoad="true"
              @checkRow="select"
              @clickRow="clickRow"
              dragle
              auto-width
              bordered
            >
            </p-table>
          </a-col>
          <a-col :span="14">
            <p-table
              ref="logTable2"
              :columns="columns"
              rowKey="id"
              :expandRowByClick="false"
              :remoteUrl="{ url: '/kz/check/getCheckDiffInboundGoods', method: 'post' }"
              :remoteData="remoteParams2"
              :remoteParams="remoteParams2"
              :autoLoad="false"
              dragle
              auto-width
              bordered
            >
            </p-table>
          </a-col>
        </a-row>
      </div>
      <a slot="extra" href="#">
        <p-button @click="spillClick" type="primary"> {{ titleName }}处理 </p-button>
      </a>
    </a-card>
  </div>
</template>
<script>
import { spillColumns, columns, item } from '../js/config'
import { extraSubmit, reduceSubmit } from '../api'

export default {
  name: 'modelForm',
  component: {},
  props: {
    // formData: {
    //   type: Object,
    //   default() {
    //     return {}
    //   },
    // },
    // formItem: {
    //   type: Array,
    //   default() {
    //     return []
    //   },
    // },
    titleName: {
      type: String,
      default: ''
    },
    checkTaskNo: {
      type: String,
      default: ''
    },
    formData: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      formItems: item(this),
      tableColumns: spillColumns(this),
      columns: columns(this),
      dataSource: [],
      tabLoding: false,
      remoteParams1: {},
      remoteParams2: {},
      disableItem: ['checkTaskNo', 'wmsCheckNo', 'checkState', 'remark'],
      billNoList:[],
    }
  },
  watch: {
    checkTaskNo: {
      handler(val) {
        console.log(val, 'valvalvalval')
        this.remoteParams1.checkTaskNo = val
      },
      immediate: true
    }
  },
  created() {
    // this.fmd = this.pubConsignor.concat(this.formItem)
  },
  mounted() {
    this.$refs.formTarget.form.setFieldsValue(this.formData)
  },
  methods: {
    clickEvent() {
      this.$emit('clickEven')
    },
    select(selectedRowskey, selectedRows) {
      console.log(selectedRows)
      this.billNoList = selectedRows.reduce((current, item) => {
        current.push(item.billNo)
        return current
      }, [])
      this.goodsIdList = selectedRows.reduce((current, item) => {
        current.push(item.goodsId)
        return current
      }, [])
    },
    clickRow(event, rowRecordRowkey, rowRecord) {
      this.remoteParams2 = {
        checkTaskNo: rowRecord.checkTaskNo,
        handleType: this.titleName == '报损' ? 2 : 1,
        goodsIdList: rowRecord.goodsId ? rowRecord.goodsId : ''
      }
    },
    spillClick() {
      const params = {
        billNoList: this.billNoList,
        goodsIdList: this.goodsIdList,
        checkTask: this.formData.checkTaskNo
      }
      if(this.billNoList.length <1) {
        this.$message.error('请至少选择一条数据')
        return
      }
      console.log(this.billNoList,'this.billNoList');
      if(this.titleName == '报溢') {
        extraSubmit(params).then(res => {
          this.$emit('clickEven')
          if(res.success) {
            this.$message.success(res.message)
            this.$refs.logTable.reload()
            this.billNoList= []
          } else {
            this.$message.error(res.message)
          }
        })
      } else {
        reduceSubmit(params).then(res => {
          this.$emit('clickEven')
          if(res.success) {
            this.$message.success(res.message)
            this.$refs.logTable.reload()
            this.billNoList= []
          } else {
            this.$message.error(res.message)
          }
        })
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
