<template>
  <el-dialog
    append-to-body
    :visible.sync="dialogVisible"
    destroy-on-close
    :show-close="false"
    fullscreen
    @close="close"
    @open="open"
  >

    <span slot="title" class="dialog-footer">
      <div class="crud-opts2">
        <span class="el-dialog__title2">入库分配</span>
        <span class="crud-opts-right2">
          <!--左侧插槽-->
          <slot name="left"/>
          <el-button slot="left" type="info" @click="dialogVisible = false">关闭</el-button>
        </span>
      </div>
    </span>
    <div class="crud-opts2">
      <span class="role-span">入库明细项</span>
    </div>
    <el-card class="box-card" shadow="never" :body-style="{padding:'0'}">
      <!--表格渲染-->
      <el-table
        ref="dtl_table"
        :data="this.openParam"
        style="width: 100%;"
        max-height="300"
        border
        :highlight-current-row="true"
        :row-class-name="tableRowClassName"
        :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
        @row-click="clcikRow"
      >
        <el-table-column prop="bill_status" label="状态" align="center" width="110px">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.bill_status"
              class="filter-item"
              :disabled="true"
            >
              <el-option
                v-for="item in dict.io_bill_status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="material_code" label="物料编码" align="center"/>
        <el-table-column show-overflow-tooltip prop="material_name" label="物料名称" align="center"/>
        <el-table-column show-overflow-tooltip prop="material_spec" label="型号规格" align="center"/>
        <el-table-column show-overflow-tooltip prop="plan_qty" :formatter="crud.formatNum3" label="数量" align="center"/>
        <el-table-column show-overflow-tooltip prop="assign_qty" :formatter="crud.formatNum3" label="已入数"
                         align="center"/>
        <el-table-column show-overflow-tooltip prop="unassign_qty" :formatter="crud.formatNum3" label="未入数"
                         align="center"/>
        <el-table-column show-overflow-tooltip prop="qty_unit_name" label="单位" align="center"/>
        <el-table-column show-overflow-tooltip prop="source_bill_type_name" label="源单类型" align="center"/>
        <el-table-column show-overflow-tooltip prop="source_bill_code" label="源单编号" align="center"/>
      </el-table>
    </el-card>
    <div class="crud-opts2">
      <span class="role-span">物料信息</span>
      <div class="crud-opts-form">
        <el-form ref="form" :inline="true" :model="form" size="mini">
          <el-form-item label="区域" prop="gender5">
            <el-cascader
              placeholder="请选择"
              :options="sects"
              :props="{ checkStrictly: true }"
              clearable
              @change="sectQueryChange"
            />
          </el-form-item>
        </el-form>
      </div>
      <span class="crud-opts-right2">
        <!--左侧插槽-->
        <slot name="left"/>
        <el-button
          slot="left"
          class="filter-item"
          type="warning"
          icon="el-icon-check"
          size="mini"
          @click="createCode()"
        >
          生成备件码
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="cancelCreate()"
        >
          取消生成
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="divStruct()"
        >
          分配货位
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="disConfirm()"
        >
          确认
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-toilet-paper"
          size="mini"
          @click="print()"
        >
          打印
        </el-button>
      </span>
    </div>
    <el-card class="box-card" shadow="never" :body-style="{padding:'0'}">
      <!--表格渲染-->
      <el-table
        ref="dis_table"
        :data="form.tableMater"
        :highlight-current-row="true"
        style="width: 100%;"
        max-height="300"
        border
        :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
      >
        <el-table-column show-overflow-tooltip type="index" label="序号" align="center"/>
        <el-table-column type="selection" width="55"/>
        <el-table-column show-overflow-tooltip prop="status_name" label="状态" align="center"/>
        <el-table-column show-overflow-tooltip prop="sparepart_only_id" label="备件编码" align="center"/>
        <el-table-column show-overflow-tooltip prop="material_name" label="备件名称" align="center"/>
        <el-table-column show-overflow-tooltip prop="pcsn" label="批次" align="center"/>
        <el-table-column show-overflow-tooltip prop="real_qty" label="数量" align="center"/>
        <el-table-column show-overflow-tooltip prop="qty_unit_name" label="单位" align="center"/>
        <el-table-column show-overflow-tooltip prop="struct_name" label="货位" align="center"/>
      </el-table>
    </el-card>

    <StructDiv ref="child" :dialog-show.sync="structShow" :sect-prop="sectProp" @tableChanged="tableChanged"/>
  </el-dialog>
</template>

<script>
import CRUD, { crud } from '@crud/crud'
import crudSectattr from '@/api/wms/basedata/st/sectattr'
import StructDiv from '@/views/wms/sb/inbill/StructDialog'
import sparePart from '@/api/wms/sb/sparepart'
import { getLodop } from '@/assets/js/lodop/LodopFuncs'

export default {
  name: 'DivDialog',
  components: { StructDiv },
  mixins: [crud()],
  dicts: ['io_bill_status'],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    bussConfig: {
      type: Object
    },
    openParam: {
      type: Array,
      default: () => {
        return []
      }
    },
    billType: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      dialogVisible: false,
      stor_id: '',
      sect_id: '',
      sectProp: null,
      structShow: false,
      bucketObj: null,
      sects: [],
      sect_val: null,
      form: {
        dtl_row: null,
        tableMater: []
      },
      storlist: [],
      rules: {}
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    }
  },
  methods: {
    open() {
      crudSectattr.getSect({ 'is_attachment': '1', 'sect_type_attr': '00' }).then(res => {
        this.sects = res.content
      })
    },
    toDelete(data) {
      data.pop = true
    },
    close() {
      this.form.tableMater = []
      this.form.dtl_row = null
      this.sectProp = null
      this.$emit('update:dialogShow', false)
      this.$emit('AddChanged')
      this.crud.refresh()
      this.$refs['form2'].resetFields()
    },
    clcikRow(row, column, event) {
      this.form.dtl_row = row
      sparePart.getIODtl({ 'bill_code': this.form.dtl_row.bill_code }).then(res => {
        this.openParam = res
        this.form.dtl_row = res[row.index]
      })
      sparePart.getDisDtl(row).then(res => {
        this.form.tableMater = res
      })
    },
    tableRowClassName({ row, rowIndex }) {
      row.index = rowIndex
    },
    createCode() {
      debugger
      if (!this.form.dtl_row) {
        this.crud.notify('请选择一条入库明细', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }

      if (this.form.dtl_row.bill_status !== '10') {
        this.crud.notify('请选择状态为生成的明细', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }

      this.form.sect_id = this.sect_id
      this.form.stor_id = this.stor_id

      if (!this.form.sect_id || !this.form.stor_id) {
        this.crud.notify('请先选择备件库区！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      sparePart.createCode(this.form).then(res => {
        sparePart.getIODtl({ 'bill_code': this.form.dtl_row.bill_code }).then(res => {
          this.openParam = res
        })
        sparePart.getDisDtl(this.form.dtl_row).then(res => {
          this.form.tableMater = res
          this.crud.notify('操作成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        })
      })
    },
    cancelCreate() {
      if (!this.form.dtl_row) {
        this.crud.notify('请选择一条入库明细', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      if (this.form.dtl_row.bill_status !== '40') {
        this.crud.notify('请选择状态为分配完的明细', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      sparePart.cancelCreate(this.form).then(res => {
        sparePart.getIODtl({ 'bill_code': this.form.dtl_row.bill_code }).then(res => {
          this.openParam = res
        })
        sparePart.getDisDtl(this.form.dtl_row).then(res => {
          this.form.tableMater = res
          this.crud.notify('操作成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        })
      })
    },
    disConfirm() {
      debugger
      let rows = this.$refs.dis_table.selection
      if (rows.length == 0) {
        this.crud.notify('请至少勾选一条记录！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      sparePart.disConfirm({ rows: rows }).then(res => {
        sparePart.getIODtl({ 'bill_code': this.form.dtl_row.bill_code }).then(res => {
          this.openParam = res
        })
        sparePart.getDisDtl(this.form.dtl_row).then(res => {
          this.form.tableMater = res
          this.crud.notify('操作成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        })
      })
    },
    sectQueryChange(val) {
      this.sectProp = val
      if (val.length === 1) {
        this.stor_id = val[0]
        this.sect_id = ''
      }
      if (val.length === 0) {
        this.sect_id = ''
        this.stor_id = ''
      }
      if (val.length === 2) {
        this.stor_id = val[0]
        this.sect_id = val[1]
      }
    },
    tableChanged(row) {
      let rows = this.$refs.dis_table.selection
      const data = {}
      data.struct = row
      data.rows = rows
      sparePart.divStruct(data).then(res => {
        sparePart.getIODtl({ 'bill_code': this.form.dtl_row.bill_code }).then(res => {
          this.openParam = res
        })
        sparePart.getDisDtl(this.form.dtl_row).then(res => {
          this.form.tableMater = res
          this.crud.notify('操作成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        })
      })
    },
    print() {
      let rows = this.$refs.dis_table.selection
      if (rows.length === 0) {
        this.crud.notify('请至少勾选一条记录！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      for (var i = 0; i < rows.length; i++) {
        let row = rows[i]
        const LODOP = getLodop()
        LODOP.SET_SHOW_MODE('HIDE_DISBUTTIN_SETUP', 1)// 隐藏那些无效按钮
        // 打印纸张大小设置https://www.it610.com/article/2094844.html
        LODOP.PRINT_INIT('')
        LODOP.SET_PRINT_PAGESIZE(1, '80mm', '60mm', '')
        LODOP.ADD_PRINT_RECT('1mm', '3mm', '74mm', '54mm', 0, 1)
        LODOP.SET_PRINT_STYLE('FontSize', 9)
        LODOP.SET_PRINT_STYLE('Bold', 1)
        LODOP.ADD_PRINT_BARCODE('10mm', '15mm', '60mm', '20mm', '128A', row.sparepart_only_id)
        LODOP.ADD_PRINT_TEXT('35mm', '15mm', '80mm', '15mm', '备件名称：' + row.material_name + '')
        LODOP.ADD_PRINT_TEXT('45mm', '15mm', '80mm', '15mm', '型号：' + row.material_spec + '')
        LODOP.PRINT()// 打印
      }

      this.crud.notify('打印成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      this.crud.toQuery()
    },
    bucketChange(row) {
      debugger
      this.bucketObj = row
      this.form.bucketunique = row.bucketunique
      this.form.storage_qty = row.storage_qty
    },
    divStruct() {
      debugger
      let rows = this.$refs.dis_table.selection
      if (rows.length == 0) {
        this.crud.notify('请至少勾选一条记录！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      if (!this.sect_id || !this.stor_id) {
        this.crud.notify('请先选择备件库区！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      this.structShow = true
      this.$refs.child.getMsg(false)
    }
  }
}
</script>

<style>
.crud-opts2 {
  padding: 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}

.crud-opts2 .el-dialog__title2 {
  line-height: 24px;
  font-size: 20px;
  color: #303133;
}

.crud-opts2 .role-span {
  padding: 10px 0px 10px 0px;
}

.crud-opts2 .crud-opts-form {
  padding: 10px 0px 0px 20px;
}

.input-with-select {
  background-color: #fff;
}
</style>
