<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>
          <p-button type="primary" v-if="regValue" @click="check(false)">驳回</p-button> -->
          <p-button type="primary" @click="print">打印</p-button>
        </div>
      </div>
      <p-spin size="small" :spinning="spinning">
        <main-table
          ref="maintable"
          :tablecolumns="tablecolumns"
          @SetRegvalue="SetRegvalue"
          @getSubTableData="getSubTableData"
        ></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
        :remote-url="tablecolumns.modalItems.remoteUrl"
        :ref="tablecolumns.modalItems.ref"
        :row-key="tablecolumns.modalItems.rowkey"
        :columns="tablecolumns.modalItems.columns"
        :autoLoad="false"
        dragle
        auto-width
        :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 { printgetGSPUpkeepPrintData, updateGspYHPrintCount } from './api.js'
import { toPrintServer, tempType } from '@/views/kzyWMS/utils/printServer.js'
import MainTable from './mainTable'
import tablecolumns from './config/tableconfjg'
// import { setwarehouse } from '../mixin/warehousemixin.js'
export default {
  data() {
    return {
      chektype: true,
      spinning: false,
      MainTableselectedRowKeys: [],
      ModalItemsParams: {},
      ModalData: [],
      selectedRow: [],
      ModalDataRowKeys: [],
      confirmLoading: false,
      visible: false,
      regValue: true,
      printdata: {},
      title: '养护打印',
      tablecolumns: tablecolumns(this),
    }
  },
  computed: {
    titleList() {
      let that = this
      return [
        {
          tab: that.title,
          key: that.title,
        },
      ]
    },
  },
  components: {
    MainTable,
  },
  //   mixins: [setwarehouse],打他
  methods: {
    SetRegvalue(bool) {
      this.regValue = bool
    },
    async check(type) {
      let that = this
      if (!this.spinning) {
        console.log('审核')
        //   console.log(this.$refs.maintable.MainTableselectedRowKeys)
        //   console.log(this.$refs.maintable.data)
        let a = this.$refs.maintable.data.filter((item) => {
          return this.$refs.maintable.MainTableselectedRowKeys.some((item1) => {
            return item.id == item1
          })
        })
        console.log(a, '====')
        if (a.length > 0) {
          this.$refs.maintable.$refs[this.tablecolumns.gridform.ref].form.validateFields(async (err, value) => {
            if (!err) {
              console.log(value)
              let b = a.map((item) => {
                return (item = { ...item, ...value })
              })
              if (type) {
                this.spinning = true
                let res = await batchAuditYes(b)
                if (res.success) {
                  that.$message.success('审核成功!')
                  that.$refs.maintable.$refs[that.tablecolumns.MainTable.ref].reload()
                  that.$refs.maintable.clear()
                } else {
                  that.$exception(res)
                }
                this.spinning = false
              } else {
                console.log('驳回')
                this.spinning = true
                let res = await batchAuditNo(b)
                if (res.success) {
                  that.$message.success('审核成功!')
                  that.$refs.maintable.$refs[that.tablecolumns.MainTable.ref].reload()
                  that.$refs.maintable.clear()
                } else {
                  that.$exception(res)
                }
              }
              this.spinning = false
            } else {
              this.$message.info('请选择数据')
            }
          })
        } else {
          this.$message.info('请选择数据')
        }
      }

      // auditText:"[string] 审核意见"  status:"[string] 状态：10=初始 20=已审批 30=已驳回"  id:"[string] undefined"
      //   let res = await batchAuditYes(this.$refs.maintable.MainTableselectedRowKeys)
      //   if (res.success) {
      //     that.$message.success('审核成功!')
      //     console.log(that.$refs.maintable.$refs[that.tablecolumns.MainTable.ref].reload())
      //   } else {
      //     that.$exception(res)
      //   }
    },
    async print() {
      console.log('打印')
      // 打
      let warehouse = this.$store.state.user.warehouse
      const warehouseId = warehouse.warehouseId
      const printParms = {
        planId: this.ModalDataRowKeys[0],
      }
      //   let data = {
      //     planId: this.$refs.maintable.getMainTableselectedRowKeys()[0]
      //   }
      //   let res1 = await updateGspYHPrintCount(data)
      //   if (!res1.success) {
      //     this.$exception(res1)
      //     return
      //   } else {
      //     this.$refs.maintable.reload()
      //   }
      console.log(this.printdata)
      if (this.printdata.length > 0) {
        // this.confirmLoading = true
        let form = this.$refs.maintable.getgridformdata()
        let res = await toPrintServer(this.printdata, form.warehouseId, tempType.GSP_YH, printParms)
        console.log(res)
        if (!res.success) {
          this.$exception(res)
          return
        } else {
          let data = {
            planId: this.$refs.maintable.getMainTableselectedRowKeys()[0],
          }
          let res1 = await updateGspYHPrintCount(data)
          if (!res1.success) {
            this.$exception(res1)
            return
          } else {
            this.$refs.maintable.reload()
          }
        }
        this.$message.success(this.$lang(res.message))
      } else {
        this.$message.warning(this.$lang('请先查询打印数据'))
      }
    },
    selectedRowsfun(selectedRowKeys, selectedRow) {
      console.log(selectedRowKeys, selectedRow)
      this.ModalDataRowKeys = selectedRowKeys
      this.selectedRow = selectedRow
    },
    // async save() {
    //   //保存提取商品信息
    //   //   getCurrentRow
    //   let data = {}
    //   data.gspUnqualifiedLossDList = this.selectedRow
    //   //   console.log(this.selectedRow)
    //   this.$refs[this.tablecolumns.modalItems.ModalItemsOpinion.ref].form.validateFields((err, value) => {
    //     if (!err) {
    //       //   console.log(value, '部门意见')
    //       data = { ...data, ...value }
    //     }
    //   })
    //   this.$refs[this.tablecolumns.modalItems.gridformref].form.validateFields((err, value) => {
    //     if (!err) {
    //       //   console.log(value, '仓库货主id')
    //       data = { ...data, ...value }
    //     }
    //   })
    //   if (this.selectedRow.length > 0) {
    //     console.log(data)
    //     let res = await addItem(data)
    //     console.log(res)
    //     if (res.success) {
    //       this.confirmLoading = false
    //       this.visible = false
    //       this.$refs.maintable.Sub()
    //     } else {
    //       this.confirmLoading = false
    //       this.$exception(res)
    //     }
    //   } else {
    //     this.$message.info('请选择提取单')
    //   }
    // },
    //明细表赋值
    setSubTableData(data) {
      this.$refs.maintable.SubTabledata = data
    },
    async getSubTableData(data) {
      console.log(data)
      let res = await printgetGSPUpkeepPrintData(data)
      console.log(res)
      this.printdata = res.data.data
      //   this.data = res.data.data[0].table
      this.setSubTableData(res.data.data[0].table)
      if (!res.success) {
        this.$exception(res)
        return
      }
    },
    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>
