<template>
  <div>
    <p-grid-form
      :ref="tablecolumns.gridform.ref"
      :span="8"
      :label-col="{ span: 6 }"
      :items="tablecolumns.gridform.items"
      @submit="hanlderSubmit"
    />

    <p-table
      style="margin-top:20px"
      v-if="!tablecolumns.MainTable.edit"
      bordered
      dragle
      auto-width
      :selectedRowKeys.sync="MainTableselectedRowKeys"
      :remote-url="tablecolumns.MainTable.remoteUrl"
      :ref="tablecolumns.MainTable.ref"
      :row-key="tablecolumns.MainTable.rowkey"
      :columns="tablecolumns.MainTable.columns"
      :remoteBefore="tablecolumns.MainTable.remoteBefore"
      :data-source.sync="data"
      :hasRowSelectCheck="true"
      :remoteParams="MainTableremoteParams"
      :rowSelectFixed="true"
      @selectChange="selectedRowsfun"
      :rowSelectType="1"
      :autoLoad="false"
      @clickRow="clickRow"
    >
      <!-- destroyPic -->
      <template slot="destroyPic" slot-scope="text, record">
        <a v-if="text" @click.stop="viewImg(text, record)">查看图片</a>
        <span v-else>暂无图片</span>
        <!-- @change="e => onChange(e, record, index)" :value="text" :min="1" -->
      </template>
      <!-- <span slot="customTitle"><a-icon type="smile-o" />aaaName</span> -->
      <template slot="auditText1" slot-scope="text, record, index">
        <p-input style="width:80px;" @click.stop="() => {}" @change="e => onChange(e, record, index)" :value="text" />
        <!-- @change="e => onChange(e, record, index)" :value="text" :min="1" -->
      </template>
      <template slot="operation" slot-scope="text, record">
        <a @click.stop="details(record)">销毁详情</a>
      </template>
    </p-table>

    <p-card
      :tab-list="tabList1"
      :active-tab-key="key1"
      :headStyle="{ border: 'none' }"
      :bordered="false"
      style="width: 100%;margin-top:20px"
    >
      <p-table
        bordered
        :ref="tablecolumns.SubTable.ref"
        :row-key="tablecolumns.SubTable.rowkey"
        :columns="tablecolumns.SubTable.columns"
        :data-source.sync="SubTabledata"
        :remoteParams="SubTableremoteParams"
        :autoLoad="false"
        :rowSelectType="false"
        :scroll="{ x: 2800 }"
      >
        <!--      :remoteUrl="tablecolumns.SubTable.remoteUrl"
        :doneBefore="tablecolumns.SubTable.doneBefore"
       -->
        <span slot="disabled" slot-scope="text, record">
          {{ text ? '禁用' : '启用' }}
        </span>
      </p-table>
    </p-card>
  </div>
</template>

<script>
import { setwarehouse } from '../../mixin/warehousemixin.js'
import { setCargoOwner } from '@/views/kzyWMS/utils/setCargoOwner'
import pick from 'lodash.pick'
export default {
  mixins: [setwarehouse, setCargoOwner],
  name: 'MainTable',
  props: {
    tablecolumns: {
      type: Object,
      default: () => {}
    },
    title: {
      type: String,
      default: '不合格品申请'
    },
    visible: {
      type: Boolean,
      default: false
    },
    config: {
      type: Object,
      default: null
    }
  },
  model: {
    prop: 'visible',
    event: 'change'
  },
  data() {
    return {
      data: [],
      MainTableselectedRowKeys: [],
      selectedRow: [],
      MainTableremoteParams: {},
      SubTableremoteParams: {},
      rowkey: 'id',
      SubTabledata: [],
      ref: 'aaa'
    }
  },
  created() {},
  computed: {
    key() {
      return this.tablecolumns.MainTable.title
    },
    key1() {
      return this.tablecolumns.SubTable.title
    },
    tabList() {
      let that = this
      return [
        {
          key: that.tablecolumns.MainTable.title,
          tab: that.tablecolumns.MainTable.title
        }
      ]
    },
    tabList1() {
      let that = this
      return [
        {
          key: that.tablecolumns.SubTable.title,
          tab: that.tablecolumns.SubTable.title
        }
      ]
    }
  },
  methods: {
    //获取gridform数据
    getgridformdata() {
      let data
      this.$refs[this.tablecolumns.gridform.ref].form.validateFields((err, value) => {
        if (!err) {
          //   console.log(value)
          data = value
        }
      })
      if (data) {
        return data
      }
    },
    //获取选中Key
    getMainTableselectedRowKeys() {
      return this.MainTableselectedRowKeys
      //  this.$emit('MainTableselectedRowKeys', this.MainTableselectedRowKeys)
    },
    //   刷新页面
    reload() {
      this.MainTableremoteParams = { ...this.MainTableremoteParams }
    },
    //查看图片
    viewImg(text, record) {
      console.log(text, record)
      this.$emit('viewImg', text)
    },
    selectedRowsfun(selectedRowKeys, selectedRow) {
      console.log(selectedRowKeys, selectedRow)
      if (selectedRow.length == 0 && selectedRowKeys.length == 0) {
        this.SubTabledata = []
      }
      //   this.selectedRowKeys=selectedRowKeys
      //   this.MainTableselectedRowKeys = selectedRowKeys
      this.selectedRow = selectedRow
    },
    details(data) {
      this.$emit('details', data)
    },
    onChange(e, record, index) {
      this.data[index].auditText = e.target.value
      console.log(this.data[index])
    },
    Sub() {
      this.$refs[this.tablecolumns.gridform.ref].form.validateFields((err, value) => {
        if (!err) {
          console.log(value)
          this.MainTableremoteParams = { ...this.MainTableremoteParams, ...value }
        } else {
          //   this.$message.warning('请选择货主和仓库名称')
          this.MainTableremoteParams = { ...this.MainTableremoteParams }
        }
      })
    },
    SetgridformValue(data) {
      this.$refs[this.tablecolumns.gridform.ref].form.setFieldsValue(
        pick(data, 'warehouseId', 'warehouseCode', 'warehouseName', 'cargoOwnerCode', 'cargoOwnerId', 'cargoOwnerName')
      )

      console.log(
        pick(data, 'warehouseId', 'warehouseCode', 'warehouseName', 'cargoOwnerCode', 'cargoOwnerId', 'cargoOwnerName')
      )

      //   this.$res[this.tablecolumns.gridform.ref].form.setFieldsValue(
      //        pick(data, 'warehouseId', 'warehouseCode', 'warehouseName','cargoOwnerCode','cargoOwnerId','cargoOwnerName')
      //   )
    },
    hanlderSubmit(event, form) {
      console.log('21113')
      event.preventDefault()
      this.$refs[this.tablecolumns.gridform.ref].form.validateFields((err, value) => {
        if (!err) {
          console.log(value)
          this.MainTableselectedRowKeys = []
          this.selectedRow = []
          this.MainTableremoteParams = { ...this.MainTableremoteParams, ...value }
          this.SubTabledata = []
          console.log(this.selectedRow)
        } else {
          this.$message.warning('请选择货主或仓库名称')
        }
      })
    },
    clear() {
      this.SubTabledata = []
    },
    clearMainTabledata() {
      this.data = []
    },
    //点击策略表 查询规则表
    clickRow(event, index, value) {
      console.log(index, value)
      this.strategy = value
      let data = {
        planId: value.id
      }
      this.$emit('getSubTableData', data)
    }
  }
}
</script>

<style>
.box-p20-bg {
  margin-bottom: 20px;
  background: #fff;
}
</style>
