<template>
  <el-dialog
    title="库存管控"
    append-to-body
    fullscreen
    :visible.sync="dialogVisible"
    @open="open"
    @close="close"
  >
    <el-row :gutter="20">
      <el-col :span="20" style="border: 1px solid white">
        <span />
      </el-col>
      <el-col :span="4">
        <span>
          <el-button icon="el-icon-check" size="mini" type="primary" @click="submit">保存</el-button>
          <el-button icon="el-icon-close" size="mini" type="info" @click="close">关闭</el-button>
        </span>
      </el-col>
    </el-row>

    <el-form ref="form" style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;" :inline="true" :model="form" :rules="rules" size="mini" label-width="85px" label-suffix=":">
      <el-input v-show="false" v-model="form.stor_code" placeholder="仓库编码" />
      <el-input v-show="false" v-model="form.stor_name" placeholder="仓库名称" />
      <el-form-item label="单据号" prop="bill_code">
        <label slot="label">单&nbsp;&nbsp;据&nbsp;号:</label>
        <el-input v-model="form.control_code" disabled placeholder="系统生成" clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="仓库" prop="stor_id">
        <label slot="label">仓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;库:</label>
        <el-select
          v-model="form.stor_id"
          clearable
          placeholder="仓库"
          class="filter-item"
          style="width: 210px"
          :disabled="crud.status.view > 0"
          @change="storChange"
        >
          <el-option
            v-for="item in storlist"
            :key="item.stor_id"
            :label="item.stor_name"
            :value="item.stor_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="管控类型" prop="bill_type">
        <el-select
          v-model="form.bill_type"
          disabled
          filterable
          size="mini"
          placeholder="业务类型"
          class="filter-item"
        >
          <el-option
            v-for="item in dict.CONTORL_TYPE"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="明细数" prop="detail_count">
        <label slot="label">明&nbsp;&nbsp;细&nbsp;数:</label>
        <el-input v-model="form.detail_count" size="mini" disabled style="width: 210px" />
      </el-form-item>
      <el-form-item label="总重量" prop="total_qty">
        <label slot="label">总&nbsp;&nbsp;重&nbsp;量:</label>
        <el-input-number
          v-model="form.total_qty"
          :controls="false"
          :precision="3"
          :min="0"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="业务日期" prop="biz_date">
        <el-date-picker v-model="form.biz_date" type="date" placeholder="选择日期" style="width: 210px" value-format="yyyy-MM-dd" :disabled="crud.status.view > 0" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <label slot="label">备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注:</label>
        <el-input v-model="form.remark" style="width: 380px;" rows="2" type="textarea" :disabled="crud.status.view > 0" />
      </el-form-item>
    </el-form>

    <div class="crud-opts2">
      <span class="role-span">管控明细</span>
      <span class="crud-opts-right2">

        <!--左侧插槽-->
        <slot name="left" />
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="queryDtl()"
        >
          添加库存物料
        </el-button>
      </span>

    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      :data="form.tableData"
      style="width: 100%;"
      border
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
    >
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column show-overflow-tooltip prop="material_code" label="物料编码" width="190" align="center" />
      <el-table-column prop="material_name" label="物料名称" align="center" min-width="150px" show-overflow-tooltip />
      <el-table-column prop="box_no" label="箱号" align="center" min-width="250px" show-overflow-tooltip />
      <el-table-column show-overflow-tooltip prop="pcsn" label="子卷号" width="150px" align="center" />
      <el-table-column show-overflow-tooltip prop="sap_pcsn" label="sap批次号" width="140px" align="center" />
      <el-table-column prop="plan_qty" label="重量" width="150" align="center">
        <template scope="scope">
          <el-input-number v-show="!scope.row.edit" v-model="scope.row.plan_qty" :precision="3" :controls="false" :min="1" style="width: 120px" />
          <span v-show="scope.row.edit">{{ scope.row.plan_qty }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="struct_code" label="仓位号" width="140px" align="center" />
      <el-table-column align="center" label="操作" width="120" fixed="right">
        <template scope="scope">
          <el-button type="danger" class="filter-item" size="mini" icon="el-icon-delete" @click.native.prevent="deleteRow(scope.$index, form.tableData)" />
        </template>
      </el-table-column>
    </el-table>

    <AddDtl :dialog-show.sync="dtlShow" :stor-id="storId" :lock-type="lockType" @tableChanged="tableChanged" />
  </el-dialog>
</template>

<script>
import CRUD, { crud, form } from '@crud/crud'
import AddDtl from '@/views/wms/st/outbill/AddDtl'
import crudUserStor from '@/views/wms/basedata/st/userStor/userStor'
import checkoutbill from '@/views/wms/st/outbill/checkoutbill'
import crudControl from '@/views/wms/control/ivtcontrol/control'

const defaultForm = {
  control_code: '',
  stor_id: '',
  stor_code: '',
  stor_name: '',
  total_qty: '0',
  detail_count: '0',
  bill_type: '',
  remark: '',
  biz_date: new Date(),
  tableData: []
}
export default {
  name: 'AddDialog',
  components: { AddDtl, crudControl },
  mixins: [crud(), form(defaultForm)],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    openType: {
      type: String,
      default: null
    },
    billType: {
      type: String
    },
    lockType: {
      type: String,
      default: ''
    }
  },
  dicts: ['CONTORL_TYPE'],
  data() {
    return {
      dialogVisible: false,
      dtlShow: false,
      storlist: [],
      storId: null,
      rules: {
        stor_id: [
          { required: true, message: '仓库不能为空', trigger: 'blur' }
        ],
        bill_type: [
          { required: true, message: '业务类型不能为空', trigger: 'blur' }
        ],
        biz_date: [
          { required: true, message: '业务日期不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    dialogShow: {
      handler(newValue) {
        this.dialogVisible = newValue
      }
    }
  },
  methods: {
    open() {
      this.form.bill_type = this.billType
      crudUserStor.getUserStor().then(res => {
        this.storlist = res
      })
    },
    close() {
      this.form.stor_code = ''
      this.form.stor_id = ''
      this.form.total_qty = '0'
      this.form.detail_count = '0'
      this.form.bill_type = ''
      this.form.remark = ''
      this.form.tableData = []
      this.$emit('update:dialogShow', false)
    },
    [CRUD.HOOK.beforeSubmit]() {
      if (this.form.tableData.length === 0) {
        this.crud.notify('请至少选择一条明细', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
    },
    storChange(row) {
      this.storlist.forEach((item) => {
        if (item.stor_id === row) {
          this.form.stor_code = item.stor_code
          this.form.stor_name = item.stor_name
        }
      })
    },
    queryDtl(index, row) {
      if (this.form.stor_id === '') {
        this.crud.notify('请选择仓库！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      this.storId = this.form.stor_id
      this.dtlShow = true
    },
    tableChanged(rows) {
      const data = {
        'data': rows
      }
      checkoutbill.queryBox(data).then(res => {
        res.forEach((item) => {
          debugger
          if (this.form.tableData.length !== 0) {
            this.flagnow = false
            for (let i = 0; i < this.form.tableData.length; i++) {
              if (this.form.tableData[i].box_no === item.box_no && this.form.tableData[i].pcsn === item.pcsn) {
                this.flagnow = true
              }
            }
            if (!this.flagnow) {
              item.edit = true
              this.form.tableData.splice(-1, 0, item)
              this.form.total_qty = parseFloat(this.form.total_qty) + parseFloat(item.plan_qty)
            }
          } else {
            item.edit = true
            this.form.tableData.splice(-1, 0, item)
            this.form.total_qty = parseFloat(this.form.total_qty) + parseFloat(item.plan_qty)
          }
        })
        this.form.detail_count = this.form.tableData.length
      })
    },
    deleteRow(index, rows) {
      const box_no = rows[index].box_no
      let len = rows.length
      while (len--) {
        const obj = rows[len]
        if (box_no === obj.box_no) {
          const index = rows.indexOf(obj)
          if (index > -1) { // 移除找到的指定元素
            this.form.total_qty = parseFloat(this.form.total_qty) - parseFloat(rows[index].plan_qty)
            rows.splice(index, 1)
            this.form.detail_count = this.form.tableData.length
          }
        }
      }
    },
    submit() {
      // 0-新增管控 1-新增解控 2-新增出库
      if (this.form.tableData.length === 0) {
        this.crud.notify('请至少选择一条明细！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      this.form.open_type = this.openType
      if (this.openType === '0' || this.openType === '1') {
        if (this.form.remark === '') {
          this.crud.notify('备注不能为空！', CRUD.NOTIFICATION_TYPE.INFO)
          return
        }
      }
      crudControl.addSubmit(this.form).then(res => {
        this.crud.notify('操作成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.toQuery()
        this.close()
      })
    }
  }
}
</script>

<style>
.crud-opts2 {
  padding: 0 0;
  display: -webkit-flex;
  display: flex;
  align-items: center;
}
.crud-opts2 .crud-opts-right2 {
  margin-left: auto;
  padding: 4px 4px;
}
.input-with-select  {
  background-color: #fff;
}
</style>
