<template>
  <div>
    <!-- 标签补打 -->
    <p-card title="标签补打">
      <span slot="extra">
        <p-button type="primary" style="margin-right: 10px" @click="RepeatPrintTask()">打印标签</p-button>
      </span>
      <p-grid-form :span="8" :items="config.items" @submit="hanlderSubmit" />
      <p-table
        ref="table"
        row-key="id"
        bordered
        :columns="config.columns"
        :remoteUrl="config.remoteUrl"
        :remoteParams="remoteParams"
        @selectChange="selectChange"
        :rowSelectFixed="true"
        :rowSelectType="1"
        :data-source="data"
        :scroll="{ x: 2500 }"
      >
        <!--   :doneBefore="doneBefore||null" -->
        <template slot="operation" slot-scope="text, record">
          <!-- {{ text ? '禁用' : '启用' }} -->
          <a @click="showmodel(text)">调整优先级</a>
          <!-- <a-divider type="vertical" />
          <a @click="del(text)">删除</a> -->
        </template>
      </p-table>
    </p-card>

    <p-modal
      title="详情"
      width="50%"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <p-grid-form ref="model" :span="8" :items="config.Modelitems" @submit="hanlderSubmit" />
    </p-modal>
    <p-modal
      title="批量打印"
      width="50%"
      :visible="visiblePrint"
      :confirm-loading="confirmLoading"
      @ok="handleOkPrint"
      @cancel="handleCancel"
    >
      <p-grid-form ref="modelPrint" :span="8" :items="Modelitems" />
    </p-modal>
  </div>
</template>

<script>
import config from './config.js'

import { doRepeatPrintTask, getPrintTaskConfigInfo } from './api'

export default {
  data() {
    return {
      selectedRows: [],
      selectedRowKeys: [],
      config: config(this),
      Modelitems: [
        {
          span: 24,
          type: 'select',
          name: 'id',
          required: true,
          label: '打印机信息',
          options: [],
          value: '',
        },
      ],
      data: [],
      type: true,
      remoteParams: {
        packageNo: '',
        label: '',
        itemCode: '',
        barCode: '',
        itemName: '',
        receiptCode: '',
        taskType: '',
        status: 0,
        vendorName: '',
        wmsTaskCode: '',
        wmsDetailId: '',
        createdTime: '',
        printList: [], //打印机信息列表
      },
      visible: false,
      visiblePrint: false,
      confirmLoading: false,
    }
  },
  mounted() {
    // this.getinfo()
  },
  methods: {
    selectChange(selectedRowKeys, selectedRows) {
      //   console.log(selectedRowKeys, selectedRows)
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },

    //重新打印 任务
    async RepeatPrintTask() {
      let that = this

      if (this.selectedRows.length > 0) {
        let res = await getPrintTaskConfigInfo()
        console.log(res)
        if (res.success) {
          this.visiblePrint = true
          //   form.resetFields
          this.$nextTick(() => {
            this.$refs.modelPrint.form.resetFields()
          })
          this.printList = res.data
          this.Modelitems[0].options = res.data.map((item) => {
            return { label: item.printName, value: item.id }
          })
          console.log(this.Modelitems)
        }
      } else {
        this.$message.error('请勾选打印行')
      }
    },
    // doneBefore(res) {
    //   res.data.list.forEach(item => {
    //     item.id = item.areaNo
    //   })
    //   console.log(res)
    //   return res
    // },
    hanlderSubmit(e, form) {
      e.preventDefault()
      form.validateFields((error, values) => {
        console.log(error, values, 'valuesvaluesvaluesvaluesvalues')
        // this.queryName = values.name
        // this.queryType = values.type
        // this.getlistCompanyByAuthId()
        if (!error) {
          this.remoteParams = { ...this.remoteParams, ...values }
        }
      })
    },
    handleOk(e) {
      console.log(this.$refs.model)
      this.$refs.model.form.validateFields(async (err, value) => {
        if (!err) {
          console.log(value)
          this.confirmLoading = true
          if (this.type) {
            let res = await update(value)
            console.log(res)
            if (res.success) {
              this.visible = false
              this.remoteParams = { ...this.remoteParams }
            } else {
              this.$exception(res)
            }
          } else {
            let res = await add(value)
            console.log(res)

            if (res.success) {
              this.$message.success(res.message)
              this.visible = false
              this.remoteParams = { ...this.remoteParams }
            } else {
              this.$exception(res)
            }
          }

          this.confirmLoading = false
          //
        }
      })
    },
    handleOkPrint(e) {
      let that = this
      this.$refs.modelPrint.form.validateFields(async (err, value) => {
        if (!err) {
          console.log(value, that.printList)
          let printData = that.printList.filter((item) => item.id == value.id)
          console.log(printData)
          let data = { ...printData, ...that.selectedRows[0] }
          //   console.log(data)
          that.confirmLoading = true
          let res = await doRepeatPrintTask(data)
          if (res.success) {
            that.$message.success('打印成功')
            that.remoteParams = { ...that.remoteParams }
            that.confirmLoading = false
            that.visiblePrint = false
          } else {
            that.$exception(res)
            that.confirmLoading = false
          }
        }
      })
    },
    handleCancel(e) {
      console.log('Clicked cancel button')
      this.visible = false
      this.visiblePrint = false
    },
    //点击弹出
    showmodel(data) {
      console.log(data)
      this.config.Modelitems = this.config.Modelitemsupdate1
      if (data && data.areaType == 20) {
        //接驳口
        this.config.Modelitems = this.config.Modelitemsupdate
      } else if (data && data.areaType == 10) {
        //区域
        this.config.Modelitems = this.config.Modelitemsupdate1
      }
      this.visible = true
      if (data) {
        this.config.Modelitems.forEach((item) => {
          item.name == 'areaType' && (item.disabled = true)
          item.name == 'areaNo' && (item.disabled = true)
        })
        this.$nextTick(() => {
          this.$refs.model.form.setFieldsValue(data)
        })
        this.type = true
      } else {
        this.config.Modelitemsupdate.forEach((item) => {
          item.disabled = false
        })
        this.config.Modelitems.forEach((item) => {
          item.disabled = false
        })
        console.log(this.config.Modelitems)
        this.$nextTick(() => {
          this.$refs.model.form.resetFields()
        })
        this.type = false
      }
    },
    del(text) {
      let that = this
      console.log(text)
      this.$confirm({
        title: '确定是否要删除?',
        content: '',
        async onOk() {
          let res = await deletes({ areaNo: text.areaNo })
          if (res.success) {
            that.$message.success('删除成功')
            that.remoteParams = { ...that.remoteParams }
          } else {
            that.$exception(res)
          }
        },
        onCancel() {},
      })
    },
  },
}
</script>

<style lang="less" scoped></style>
