<template>
  <div>
    <!-- :title="title" -->
    <!-- :tab-list="titleList" :active-tab-key="title" -->
    <p-card :title="title">
      <div slot="extra">
        <div class="btns">
          <p-button v-if="chektype" type="primary" @click="check(true)">确认</p-button>
        </div>
      </div>
      <p-spin size="small" :spinning="spinning">
        <main-table ref="maintable" :tablecolumns="tablecolumns" @SetRegvalue="SetRegvalue"></main-table>
      </p-spin>
    </p-card>
    <p-modal
      width="80%"
      :maskClosable="false"
      :enlargeable="true"
      :draggable="true"
      :title="tablecolumns.modalItems.title"
      :visible="visible"
      :okText="'保存'"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <!-- <div style="width:100%;height:20px;">
        <p-button style="float:right;margin-right:10px;" @click="save">保存</p-button>
      </div> -->
      <p-grid-form
        :ref="tablecolumns.modalItems.gridformref"
        :span="8"
        :label-col="{ span: 9 }"
        :items="tablecolumns.modalItems.items"
        @submit="hanlderSubmit"
      />
      <a-divider />
      <p-table
        bordered
        dragle
        auto-width
        :remote-url="tablecolumns.modalItems.remoteUrl"
        :ref="tablecolumns.modalItems.ref"
        :row-key="tablecolumns.modalItems.rowkey"
        :columns="tablecolumns.modalItems.columns"
        :autoLoad="false"
        :selectedRowKeys.sync="ModalDataRowKeys"
        :data-source.sync="ModalData"
        :row-selection="{ fixed: true, onChange: selectedRowsfun }"
        :remoteParams="ModalItemsParams"
        :remoteBefore="tablecolumns.modalItems.remoteBefore"
        :rowSelectType="2"
        :scroll="{ x: 1500 }"
      >
        <template slot="number" slot-scope="text, record, index">
          <p-input-number style="width:80px;" @change="e => onChange(e, record, index)" :value="text" :min="1" />
        </template>
      </p-table>
      <a-divider />
      <p-grid-form
        :ref="tablecolumns.modalItems.ModalItemsOpinion.ref"
        :span="8"
        :label-col="{ span: 9 }"
        :items="tablecolumns.modalItems.ModalItemsOpinion.items"
        @submit="hanlderSubmit"
      />
    </p-modal>
  </div>
</template>

<script>
import { batchAuditConfirm } from '../api.js'
import MainTable from './mainTable'
import { toPrintServer, tempType } from '@/views/kzyWMS/utils/printServer.js'
import tablecolumns from './config/tableconfjg'
// import { setwarehouse } from '../mixin/warehousemixin.js'
import { getBusiness } from '@/views/kzyWMS/api/business' // 系统参数接口
export default {
  data() {
    return {
      chektype: true,
      spinning: false,
      MainTableselectedRowKeys: [],
      ModalItemsParams: {},
      ModalData: [],
      selectedRow: [],
      ModalDataRowKeys: [],
      confirmLoading: false,
      visible: false,
      regValue: true,
      title: '不合格品报损确认',
      tablecolumns: tablecolumns(this)
    }
  },
  computed: {
    titleList() {
      let that = this
      return [
        {
          tab: that.title,
          key: that.title
        }
      ]
    }
  },
  components: {
    MainTable
  },
  //   mixins: [setwarehouse],打他
  methods: {
    async SetRegvalue(bool) {
      let res = await getBusiness()
      console.log(res)
      //   console.log(bool)
      if (res != 'int') this.regValue = bool
    },
    async check(type) {
      let that = this
      let data = this.$refs.maintable.GetselectedRow()
      let b = data.some(item => {
        // if (item.status == '20') return false
        return item.status == '20'
      })
      console.log(b)
      if (!b) {
        this.$message.info('已确认单据不能再次确认')
        return
      }
      try {
        let res = await batchAuditConfirm(data)
        if (res.success) {
          this.$message.success(res.message)
          this.$refs.maintable.MainTableReload()
        } else {
          this.$exception(res)
        }
      } catch (error) {
        this.$exception(error)
      }
    },

    selectedRowsfun(selectedRowKeys, selectedRow) {
      console.log(selectedRowKeys, selectedRow)
      this.ModalDataRowKeys = selectedRowKeys
      this.selectedRow = selectedRow
    },

    hanlderSubmit(event, form) {
      event.preventDefault()
      console.log('弹窗查询')
      form.validateFields((err, value) => {
        if (!err) {
          console.log(value, '=========')
          this.ModalItemsParams = { ...value }
          console.log(this.$refs[this.tablecolumns.modalItems.ref])

          //   this.$refs[this.tablecolumns.modalItems.ref].reload()
          //   console.log(this.$refs[this.tablecolumns.modalItems.ref])
        }
      })
    },
    showModal() {
      this.$refs.maintable.$refs[this.tablecolumns.gridform.ref].form.validateFields((err, value) => {
        if (!err) {
          //   console.log(value)
          this.visible = true
          //   console.log(this.$refs[this.tablecolumns.modalItems.ref])
          this.$nextTick(() => {
            this.$refs[this.tablecolumns.modalItems.gridformref].form.setFieldsValue(value)
            console.log(value)
            setTimeout(() => {
              this.ModalItemsParams = { ...value }
            }, 0)
            this.$refs[this.tablecolumns.modalItems.ModalItemsOpinion.ref].form.resetFields()
            this.selectedRow = []
          })
        } else {
          this.$message.warning('请选择货主和仓库名称')
        }
      })

      //   this.visible = true
    },
    handleOk(e) {
      this.confirmLoading = true
      this.save()
    },
    handleCancel(e) {
      console.log('Clicked cancel button')
      this.visible = false
    },
    // del() {
    //   let that = this
    //   this.$confirm({
    //     title: '警告',
    //     content: `真的要删除选中行吗?`,
    //     okText: '删除',
    //     okType: 'danger',
    //     cancelText: '取消',
    //     async onOk() {
    //       let res = await delItem(that.$refs.maintable.MainTableselectedRowKeys)
    //       console.log(res)
    //       if (res.success) {
    //         that.$message.success('删除成功!')
    //         console.log(that.$refs.maintable.$refs[that.tablecolumns.MainTable.ref].reload())
    //         // that.$refs.maintable[that.tablecolumns.MainTable.ref].reload()
    //       } else {
    //         that.$exception(res)
    //       }
    //     },
    //     onCancel() {
    //       console.log('Cancel')
    //     }
    //   })
    // },
    onChange(e, record, index) {
      //   console.log(e, record, value)
      //   e = record.number
      //   e = value
      console.log(this.ModalData[index])
      this.ModalData[index].lossQty = e
    }
  }
}
</script>

<style scoped>
.btns {
  width: 200px;
  display: flex;
  justify-content: space-around;
}
</style>
