<template>
  <div>
    <p-card :title="title">
      <div slot="extra">
        <div class="btns">
          <p-button v-if="chektype" type="primary" @click="check(true)">审核</p-button>
          <!-- v-if="regValue" -->
          <!-- <p-button type="primary"  @click="check(false)">驳回</p-button> -->
          <p-button type="primary" @click="print">打印</p-button>
        </div>
      </div>
      <main-table
        ref="maintable"
        :tablecolumns="tablecolumns"
        @details="details"
        @SetRegvalue="SetRegvalue"
        @viewImg="viewImg"
      ></main-table>
    </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"
        :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>

    <p-modal :title="$lang('销毁详情')" v-model="isShowModal" :width="800" :footer="false" @ok="handleOk" @cancel="onCancel" >
      <a-descriptions v-if="detailData">
        <a-descriptions-item v-for="(item, key, index) in tablecolumns.details" :key="index" :label="item">
          {{ detailData[Object.keys(tablecolumns.details)[index]] }}
          <!-- {{ index }} -->
        </a-descriptions-item>
      </a-descriptions>
      <p-button @click="showpic(detailData.destroyPic)">销毁图片</p-button>
      <imgsUpload v-model="isUpload" :isView="lookats" :uploadIdlist="uploadIdlist"></imgsUpload>
    </p-modal>
    <imgsUpload v-model="isUpload" :isView="lookats" :uploadIdlist="uploadIdlist"></imgsUpload>
  </div>
</template>

<script>
import { batchAuditYes, DestroybatchAuditYes, DestroybatchAuditNo, DestroygetById } from '../api.js'
import MainTable from '../components/mainTable'
import tablecolumns from './config/tableconfjg'
import { toPrintServer, tempType } from '@/views/kzyWMS/utils/printServer.js'
import ImgsUpload from '@/views/kzyWMS/components/ImgsUpload'
import { getBusiness } from '@/views/kzyWMS/api/business' // 系统参数接口
// import { setwarehouse } from '../mixin/warehousemixin.js'
export default {
  data() {
    return {
      chektype: true,
      uploadIdlist: [],
      MainTableselectedRowKeys: [],
      ModalItemsParams: {},
      ModalData: [],
      selectedRow: [],
      isUpload: false,
      lookats: true,
      ModalDataRowKeys: [],
      confirmLoading: false,
      visible: false,
      isShowModal: false,
      detailData: {},
      regValue: true,
      title: '不合格品销毁审核',
      tablecolumns: tablecolumns(this)
    }
  },
  components: {
    MainTable,
    ImgsUpload
  },

  //   mixins: [setwarehouse],g
  methods: {
    onCancel() {
      this.isShowModal = false
    },
    viewImg(data) {
      console.log(data)
      this.uploadIdlist = data.split(',')
      console.log(this.uploadIdlist)
      this.lookats = true
      this.isUpload = true
    },

    //展示销毁图片
    showpic(item) {
      console.log(item)
      if (item) {
        this.uploadIdlist = item.split(',')
        console.log(this.uploadIdlist)
        this.lookats = true
        this.isUpload = true
      }
    },
    async SetRegvalue(bool) {
      let res = await getBusiness()
      console.log(res)
      //   console.log(bool)
      if (res != 'int') this.regValue = bool
    },
    async details(data) {
      console.log(data, '查看详情')
      let aaa = {
        id: data.id
      }
      console.log(aaa)
      let res = await DestroygetById({ id: data.id })
      console.log(res)
      this.detailData = res.data
      this.isShowModal = true
      console.log(Object.keys(this.tablecolumns.details))
    },
    async check(type) {
      let that = this
      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
        })
      })
      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 })
            })
            console.log(b)
            if (type) {
              let res = await DestroybatchAuditYes(b)
              if (res.success) {
                that.$message.success('审核成功!')
                that.$refs.maintable.$refs[that.tablecolumns.MainTable.ref].reload()
                that.$refs.maintable.clear()
              } else {
                that.$exception(res)
              }
            } else {
              console.log('驳回')
              let res = await DestroybatchAuditNo(b)
              if (res.success) {
                that.$message.success('驳回成功!')
                that.$refs.maintable.$refs[that.tablecolumns.MainTable.ref].reload()
                that.$refs.maintable.clear()
              } else {
                that.$exception(res)
              }
            }
          }
        })
      } else {
        this.$message.info('请选择数据')
      }
    },
    async print() {
      console.log('打印')
      let list = this.$refs.maintable.GetselectedRow()
      let sublist = this.$refs.maintable.GetSubTabledata()
      console.log(list)
      if (list.length == 1) {
        console.log('主表数据', list)
        console.log('子表数据', sublist)
        if (sublist.length == 0) {
          this.$message.warning('请先查询明细表数据!')
          return
        }

        let warehouse = this.$store.state.user.warehouse
        const warehouseId = warehouse.warehouseId
        console.log(list[0])
        const printParms = {
          planId: list[0].id
        }
        let printdata = {
          head: list[0],
          table: sublist
        }
        console.log(printdata, printParms)
        let res = await toPrintServer(printdata, warehouseId, tempType.GSP_XH, printParms)
        console.log(res)
        if (!res.success) {
          this.$exception(res)
          return
        }
      } else {
        this.$message.warning('打印时请选择一条数据!')
      }
    },
    selectedRowsfun(selectedRowKeys, selectedRow) {
      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('请选择提取单')
    //   }
    // },
    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>
