<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"/>
    </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="handlerSubmit"/>
      <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 {batchAdd, printgetGSPUpkeepPrintData} from '../api'
import {toPrintServer, tempType} from '@/views/kzyWMS/utils/printServer.js'
import tablecolumns from './config'

export default {
  components: {MainTable},
  data() {
    return {
      spinning:false,
      tablecolumns,
      title: '一般养护计划',
      confirmLoading: false,
      visible: false,
      items: [
        {
          span: 8,
          type: 'radio',
          name: 'isProtected',
          label: '是否重点',
          value: 0,
          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')
            }
          },
          format: 'YYYY-MM-DD',
          key: 'planUpkeepDate',
          scopedSlots: {customRender: 'planUpkeepDate'}
        },
        {
          dataIndex: 'bldgName',
          title: '建筑物名称',
          type: 'txt'
        },
        {
          title: '货主名称',
          type: 'txt',
          ellipsis: true,
          dataIndex: 'cargoOwnerName'
        },
        {
          title: '实际货位',
          type: 'txt',
          dataIndex: 'locatorName'
        },

        {
          title: '商品编号',
          dataIndex: 'itemCode',
          type: 'txt',
        },
        {
          title: '商品名称',
          dataIndex: 'itemName',
          ellipsis: true,
          type: 'txt',
        },
        {
          title: '批号',
          ellipsis: true,
          dataIndex: 'lotNum',
          align: 'center',
          type: 'txt',
        },
        {
          title: '商品大类',
          dataIndex: 'itemHealthCategoryName',
          ellipsis: true,
          type: 'txt',
        },
        {
          title: '批准文号',
          dataIndex: 'itemHealthApprovalNbr',
          ellipsis: true,
          type: 'txt',
        },
        {
          title: '备注',
          dataIndex: 'remark',
          ellipsis: true,
          type: 'edit',
        },
        {
          title: '库存数量',
          dataIndex: 'stockQty',
          align: 'center',
          width: 80,
          type: 'txt',
        },
        {
          title: '养护数量',
          ellipsis: true,
          dataIndex: 'upkeepQty',
          align: 'center',
          width: 100,
          type: 'number'
        },
        {
          title: '计划数量',
          ellipsis: true,
          align: 'center',
          width: 100,
          dataIndex: 'planStockQty',
          type: 'number'
        },
        {
          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
          }
        },
        {
          title: '待处理原因',
          dataIndex: 'pendingReason',
          ellipsis: true,
          type: 'edit',
        },
        {
          title: '养护待处理数量',
          dataIndex: 'pendingQty',
          ellipsis: true,
          type: 'edit',
        },
        {
          title: '外观',
          dataIndex: 'checkWaiGuan',
          type: 'edit',
          width: 50,
          slots: {title: 'checkWaiGuanTitle'},
          scopedSlots: {customRender: 'checkWaiGuan'}
        },
        {
          title: '包装',
          dataIndex: 'checkBaoZhuang',
          type: 'edit',
          width: 50,
          align: 'center',
          slots: {title: 'checkBaoZhuangTitle'},
          scopedSlots: {customRender: 'checkBaoZhuang'}
        },
        {
          title: '存储条件',
          dataIndex: 'checkChenChuTiaoJian',
          type: 'edit',
          width: 80,
          align: 'center',
          slots: {title: 'checkChenChuTiaoJianTitle'},
          scopedSlots: {customRender: 'checkcheckChenChuTiaoJianFanDuo'}
        },
        {
          title: '翻跺',
          dataIndex: 'checkFanDuo',
          width: 50,
          align: 'center',
          slots: {title: 'checkFanDuoTitle'},
          scopedSlots: {customRender: 'checkFanDuo'}
        },
        {
          title: '除湿',
          dataIndex: 'checkChuShi',
          type: 'switch',
          width: 50,
          align: 'center',
          slots: {title: 'checkChuShiTitle'},
          scopedSlots: {customRender: 'checkChuShi'}
        },
        {
          title: '加湿',
          dataIndex: 'checkJiaShi',
          width: 50,
          align: 'center',
          type: 'switch',
          slots: {title: 'checkJiaShiTitle'},
          scopedSlots: {customRender: 'checkJiaShi'}
        },
        {
          title: '升温',
          dataIndex: 'checkShenWen',
          width: 50,
          align: 'center',
          type: 'switch',
          slots: {title: 'checkShenWenTitle'},
          scopedSlots: {customRender: 'checkShenWen'}
        },
        {
          title: '降温',
          dataIndex: 'checkJiangWen',
          type: 'switch',
          width: 50,
          align: 'center',
          slots: {title: 'checkJiangWenTitle'},
          scopedSlots: {customRender: 'checkJiangWen'}
        },
        {
          title: '通风',
          dataIndex: 'checkTongFeng',
          type: 'switch',
          width: 50,
          align: 'center',
          slots: {title: 'checkTongFengTitle'},
          scopedSlots: {customRender: 'checkTongFeng'}
        },
        {
          title: '除尘',
          dataIndex: 'checkChuChen',
          type: 'switch',
          width: 50,
          align: 'center',
          slots: {title: 'checkChuChenTitle'},
          scopedSlots: {customRender: 'checkChuChen'}
        },
        {
          title: '上市许可持有人',
          dataIndex: 'itemHealthListedLicensor',
          ellipsis: true,
          type: 'txt',
        },
        {
          title: '生产厂家',
          dataIndex: 'manufacturerName',
          ellipsis: true,
          type: 'txt'
        },
        {
          title: '生产日期',
          dataIndex: 'pdateFrom',
          type: 'txt',
          format: 'YYYY-MM-DD',
          customRender(text) {
            if (text) {
              return moment(text).format('YYYY-MM-DD')
            }
          }
        },
        {
          title: '有效期至',
          dataIndex: 'validUntil',
          type: 'txt',
          format: 'YYYY-MM-DD',
          customRender(text) {
            if (text) {
              return moment(text).format('YYYY-MM-DD')
            }
          }
        },
        {
          title: '质量状况',
          dataIndex: 'qualityStatus',
          type: 'txt',
          customRender(text) {
            switch (text) {
              case '1':
                text = '合格'
                break
              case '2':
                text = '不合格'
                break
              case '3':
                text = '待检'
                break
              case '4':
                text = '退货'
                break
            }
            return text
          }
        }
      ]
    }
  },
  methods: {
    printclick() {
      let form = this.$refs.maintable.getgridformdata()
      const printParms = {}
      if (this.printdata) {
        toPrintServer(this.printdata, form.warehouseId, tempType.GSP_YH, printParms)
        this.$message.success(this.$lang('打印成功'))
      } else {
        this.$message.warning(this.$lang('请先查询打印数据'))
      }
    },
    async Makeplan() {
      let selectedRow = this.$refs.maintable.getselectedRowsfun()
      console.log(selectedRow, '====')
      if (selectedRow.length > 0) {
        selectedRow = selectedRow.map(_ => ({ ..._, isProtected: 0 }))
        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('请选择数据')
      }
    },
    //查询打印
    handlerSubmit(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
          }
        }
      })
    },
    handleOk(e) {
      let warehouse = this.$store.state.user.warehouse
      const warehouseId = warehouse.warehouseId
      const printParms = {}
      let form = this.$refs.maintable.getgridformdata()
      console.log(this.printdata)
      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('打印成功'))
        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>
</style>
