<template>
  <p-spin :spinning="spinning">
    <p-card :title="title">
      <div slot="extra">
        <p-button type="primary" @click="Makeplan">生成养护计划</p-button>
      </div>
      <main-table ref="maintable" :tablecolumns="tablecolumns"></main-table>
    </p-card>
    <!--打印弹窗 -->
    <p-modal width="80%" title="打印" :visible="visible" :confirm-loading="confirmLoading" @ok="handleOk"
             @cancel="handleCancel">
      <div class="printdiv">
        <p-button class="print" type="primary" @click="printclick">打印</p-button>
      </div>
      <p-grid-form ref="form" :label-col="{ span: 6 }" :items="items" @submit="hanlderSubmit"/>
      <p-table ref="mainlist" rowKey="itemCode" :columns="columns" bordered :data-source.sync="data" size="small"
               :has-row-count="true"
               :rowSelectType="false" :scroll="{ y: 1000, x: 2500 }"/>
    </p-modal>
  </p-spin>
</template>

<script>
import moment from 'moment'
import MainTable from '../components/mainTable'
import tablecolumns from './config'
import {batchAdd, printgetGSPUpkeepPrintData} from '../api'
import {toPrintServer, tempType} from '@/views/kzyWMS/utils/printServer.js'

export default {
  data() {
    return {
      spinning:false,
      tablecolumns,
      title: '重点养护计划',
      confirmLoading: false,
      visible: false,
      items: [
        {
          span: 8,
          type: 'radio',
          name: 'isProtected',
          label: '是否重点',
          value: 1,
          options: [
            {label: '重点养护', value: 1},
            {label: '一般养护', value: 0}
          ]
        },
        {
          span: 8,
          type: 'row',
          label: '日期',
          list: [
            {
              type: 'date',
              name: 'startTime',
              placeholder: '请输入开始日期',
              value: '',
              format: 'YYYY-MM-DD',
              style: 'padding-right:5px;'
            },
            {
              type: 'date',
              name: 'endTime',
              placeholder: '请输入结束日期',
              format: 'YYYY-MM-DD',
              value: ''
            }
          ]
        },
        {
          span: 8,
          type: 'buttonGroup',
          style: {textAlign: 'left'},
          options: [
            {
              value: '查询',
              icon: 'search',
              btnType: 'primary',
              style: 'margin-right:10px;margin-left:30px;',
              htmlType: 'submit'
            },
            {
              value: '重置',
              name: 'ccz',
              icon: 'redo',
              on: {
                click(event, form) {
                  form.resetFields()
                }
              }
            }
          ]
        }
      ],
      data: [],
      columns: [
        {
          title: '养护计划日期',
          dataIndex: 'planUpkeepDate',
          type: 'txt',
          width: 180,
          customRender(text) {
            if (text) {
              return moment(text).format('YYYY-MM-DD')
            } else return
          },
          format: 'YYYY-MM-DD',
          key: 'planUpkeepDate',
          scopedSlots: {customRender: 'planUpkeepDate'}

          //   slots: { title: 'warehouseName' }
        },
        // ...genBuilding(
        //   1,
        //   true,
        //   false,
        //   function(res, p) {
        //     p.params.warehouseId = warehouseId
        //     return p
        //   },
        //   function(rowData, form) {
        //     bldgId = rowData[0].bldgId
        //   },
        //   {}
        // ),
        {
          dataIndex: 'bldgName',
          title: '建筑物名称',
          type: 'txt'
        },
        // {
        //   title: '建筑物',
        //   dataIndex: 'bldgId',
        //   type: 'edit',
        //   key: 'bldgId'
        // },
        // {
        //   title: '货主名称',
        //   dataIndex: 'cargoOwnerName',
        // //   type: 'searchModal',
        //   key: 'cargoOwnerName',
        //   ...genwarehouse()
        // },
        // ...genCargoowner(),
        {
          title: '货主名称',
          type: 'txt',
          name: 'cargoOwnerName',
          ellipsis: true,
          dataIndex: 'cargoOwnerName'
        },
        {
          title: '实际货位',
          type: 'txt',
          name: 'locatorName',
          dataIndex: 'locatorName'
        },

        {
          title: '商品编号',
          dataIndex: 'itemCode',
          type: 'txt',
          key: 'itemCode'
        },
        {
          title: '商品名称',
          dataIndex: 'itemName',
          ellipsis: true,
          type: 'txt',
          key: 'itemName'
        },
        // genItemSearchModal(
        //   2,
        //   false,
        //   false,
        //   function(res, p) {
        //     p.data.cargoOwnerId = cargoOwnerId
        //     p.data.warehouseId = warehouseId
        //     console.log(p, '====')
        //     return p
        //   },
        //   function(value, index) {
        //     console.log(value, 'value', arguments)
        //   },
        //   { span: 6, labelCol: { span: 6 }, wrapperCol: { span: 14 } }
        // ),
        {
          title: '批号',
          ellipsis: true,
          dataIndex: 'lotNum',
          align: 'center',
          type: 'txt',
          key: 'lotNum'
        },
        {
          title: '商品大类',
          dataIndex: 'itemHealthCategoryName',
          ellipsis: true,
          type: 'txt',
          key: 'itemHealthCategoryName'
        },
        {
          title: '批准文号',
          dataIndex: 'itemHealthApprovalNbr',
          ellipsis: true,
          type: 'txt',
          key: 'itemHealthApprovalNbr'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          ellipsis: true,
          type: 'edit',
          key: 'remark'
        },
        {
          title: '库存数量',
          dataIndex: 'stockQty',
          //   scopedSlots: { customRender: 'stockQty' },
          align: 'center',
          width: 80,
          type: 'txt',
          key: 'stockQty'
        },
        {
          title: '养护数量',
          ellipsis: true,
          dataIndex: 'upkeepQty',
          //   scopedSlots: { customRender: 'upkeepQty' },
          align: 'center',
          width: 100,
          type: 'number',
          key: 'upkeepQty',
          on: {
            change(data) {
              //   console.log(data)
              //   let num
              //   if (data.record.id == that.ids.creatRowid) {
              //     let ss = that.ids.data.filter(item => {
              //       return item.id.replace(/_copy_\d*/, '') == that.ids.id
              //     })
              //     console.log(ss)
              // let upkeepQty = ss.stockQty
              // if (ss) {
              //   num = upkeepQty - data.value
              //   console.log(num)
              //   that.setRowQTy(that.ids.id, num)
              // }
              //   }
            }
          }
        },
        {
          title: '计划数量',
          ellipsis: true,
          align: 'center',
          width: 100,
          dataIndex: 'planStockQty',
          type: 'number',
          key: 'planStockQty',
          on: {
            change(data) {
            }
          }
        },
        {
          title: '养护结论',
          dataIndex: 'upkeepConclusion',
          required: true,
          type: 'select',
          options: [
            {label: '待处理', value: 0},
            {label: '合格', value: 1},
            // {label: '不合格', value: 2}
          ],
          customRender(text) {
            text == 0 && (text = '待处理')
            text == 1 && (text = '合格')
            text == 2 && (text = '不合格')
            return text
          },
          key: 'upkeepConclusion'
        },
        {
          title: '待处理原因',
          dataIndex: 'pendingReason',
          ellipsis: true,
          type: 'edit',
          key: 'pendingReason'
        },
        {
          title: '养护待处理数量',
          dataIndex: 'pendingQty',
          ellipsis: true,
          type: 'edit',
          key: 'pendingQty'
        },
        {
          title: '外观',
          dataIndex: 'checkWaiGuan',
          type: 'edit',
          width: 50,
          key: 'checkWaiGuan',
          slots: {title: 'checkWaiGuanTitle'},
          scopedSlots: {customRender: 'checkWaiGuan'}
        },
        {
          title: '包装',
          dataIndex: 'checkBaoZhuang',
          type: 'edit',
          width: 50,
          align: 'center',
          key: 'checkBaoZhuang',
          slots: {title: 'checkBaoZhuangTitle'},
          scopedSlots: {customRender: 'checkBaoZhuang'}
        },
        {
          title: '存储条件',
          dataIndex: 'checkChenChuTiaoJian',
          type: 'edit',
          width: 80,
          align: 'center',
          key: 'checkChenChuTiaoJian',
          slots: {title: 'checkChenChuTiaoJianTitle'},
          scopedSlots: {customRender: 'checkcheckChenChuTiaoJianFanDuo'}
        },
        {
          title: '翻跺',
          dataIndex: 'checkFanDuo',
          key: 'checkFanDuo',
          width: 50,
          align: 'center',
          slots: {title: 'checkFanDuoTitle'},
          scopedSlots: {customRender: 'checkFanDuo'}
        },
        {
          title: '除湿',
          dataIndex: 'checkChuShi',
          type: 'switch',
          width: 50,
          align: 'center',
          key: 'checkChuShi',
          slots: {title: 'checkChuShiTitle'},
          scopedSlots: {customRender: 'checkChuShi'}
        },
        {
          title: '加湿',
          dataIndex: 'checkJiaShi',
          key: 'checkJiaShi',
          width: 50,
          align: 'center',
          type: 'switch',
          slots: {title: 'checkJiaShiTitle'},
          scopedSlots: {customRender: 'checkJiaShi'}
        },
        {
          title: '升温',
          dataIndex: 'checkShenWen',
          key: 'checkShenWen',
          width: 50,
          align: 'center',
          type: 'switch',
          slots: {title: 'checkShenWenTitle'},
          scopedSlots: {customRender: 'checkShenWen'}
        },
        {
          title: '降温',
          dataIndex: 'checkJiangWen',
          key: 'checkJiangWen',
          type: 'switch',
          width: 50,
          align: 'center',
          slots: {title: 'checkJiangWenTitle'},
          scopedSlots: {customRender: 'checkJiangWen'}
        },
        {
          title: '通风',
          dataIndex: 'checkTongFeng',
          key: 'checkTongFeng',
          type: 'switch',
          width: 50,
          align: 'center',
          slots: {title: 'checkTongFengTitle'},
          scopedSlots: {customRender: 'checkTongFeng'}
        },
        {
          title: '除尘',
          dataIndex: 'checkChuChen',
          key: 'checkChuChen',
          type: 'switch',
          width: 50,
          align: 'center',
          slots: {title: 'checkChuChenTitle'},
          scopedSlots: {customRender: 'checkChuChen'}
        },
        {
          title: '上市许可持有人',
          dataIndex: 'itemHealthListedLicensor',
          ellipsis: true,
          type: 'txt',
          key: 'itemHealthListedLicensor'
        },
        {
          title: '生产厂家',
          dataIndex: 'manufacturerName',
          ellipsis: true,
          type: 'txt',
          key: 'manufacturerName'
        },
        {
          title: '生产日期',
          dataIndex: 'pdateFrom',
          type: 'txt',
          format: 'YYYY-MM-DD',
          customRender(text) {
            if (text) {
              return moment(text).format('YYYY-MM-DD')
            } else return
          },
          key: 'pdateFrom'
        },
        {
          title: '有效期至',
          dataIndex: 'validUntil',
          type: 'txt',
          format: 'YYYY-MM-DD',
          key: 'validUntil',
          customRender(text) {
            if (text) {
              return moment(text).format('YYYY-MM-DD')
            } else return
          }
        },
        {
          title: '质量状况',
          dataIndex: 'qualityStatus',
          type: 'txt',
          key: 'qualityStatus',
          customRender(text) {
            switch (text) {
              case '1':
                text = '合格'
                break
              case '2':
                text = '不合格'
                break
              case '3':
                text = '待检'
                break
              case '4':
                text = '退货'
                break
            }
            // console.log(text)
            return text
          }
        }
        //   {
        //     title: '操作',
        //     key: 'operation',
        //     scopedSlots: { customRender: 'operation' },
        //     align: 'center'
        //   }
      ]
    }
  },
  components: {
    MainTable
  },

  //   mixins: [setwarehouse],
  methods: {
    async Makeplan() {
      //        selectedRowsfun(selectedRowKeys, selectedRow) {
      //   this.selectedRow = selectedRow
      let selectedRow = this.$refs.maintable.getselectedRowsfun()
      console.log(selectedRow, '====')
      if (selectedRow.length > 0) {
        selectedRow.forEach(item => {
          item.isProtected = 1
        })
        this.spinning = true
        let res = await batchAdd(selectedRow)
        this.spinning = false
        console.log(res)
        if (res.success) {
          this.$success({
            title: '提示',
            // JSX support
            content: (
                <div>
                  <p>生成养护记录成功!</p>
                </div>
            )
          })
          this.$refs.maintable.reload()
        } else {
          this.$exception(res)
        }
      } else {
        this.$message.info('请选择数据')
      }
    },
    //查询打印
    hanlderSubmit(event, form) {
      let warehouse = this.$store.state.user.warehouse
      event.preventDefault()
      this.$refs.form.form.validateFields(async (err, value) => {
        if (!err) {
          console.log(value)
          let params = {
            warehouseId: warehouse.warehouseId,
            isProtected: value.isProtected
          }
          let res = await printgetGSPUpkeepPrintData(params)
          console.log(res.data.data[0].table)
          this.printdata = res.data.data
          this.data = res.data.data[0].table
          if (!res.success) {
            this.$exception(res)
            return
          }
        }
      })
    },
    printclick() {
      // 打印
      let warehouse = this.$store.state.user.warehouse
      const warehouseId = warehouse.warehouseId
      const printParms = {}
      console.log(this.printdata)
      let form = this.$refs.maintable.getgridformdata()
      if (this.printdata) {
        // this.confirmLoading = true
        toPrintServer(this.printdata, form.warehouseId, tempType.GSP_YH, printParms)
        // setTimeout(() => {
        //   this.visible = false
        //   this.confirmLoading = false
        // }, 0)
        this.$message.success(this.$lang('打印成功'))
      } else {
        this.$message.warning(this.$lang('请先查询打印数据'))
      }
    },
    handleOk(e) {
      this.data = []
      this.printdata = []
      setTimeout(() => {
        this.visible = false
        this.confirmLoading = false
      }, 0)
      //   let warehouse = this.$store.state.user.warehouse
      //   const warehouseId = warehouse.warehouseId
      //   const printParms = {}
      //   console.log(this.printdata)
      //   if (this.printdata) {
      //     this.confirmLoading = true
      //     toPrintServer(this.printdata, warehouseId, tempType.GSP_YH, printParms)
      //     setTimeout(() => {
      //       this.visible = false
      //       this.confirmLoading = false
      //     }, 0)
      //     this.$message.success(this.$lang('打印成功'))
      //     this.data = []
      //     this.printdata = []
      //   } else {
      //     this.$message.warning(this.$lang('请先查询打印数据'))
      //   }
    },
    handleCancel(e) {
      console.log('Clicked cancel button')
      this.visible = false
      this.data = []
      this.printdata = []
    }
  }
}
</script>

<style scoped>
.printdiv {
  width: 100%;
  height: 20px;
  position: relative;
}

.print {
  position: absolute;
  right: 5%;
}

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