<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>
          <el-button slot="left" type="primary" @click="submit">补料确认</el-button>
        </span>
      </div>
    </span>

    <el-form ref="form" style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;" :inline="true" :model="form" size="mini" label-width="115px" label-suffix=":">
      <el-form-item label="配方号" prop="formula_code">
        <label slot="label">配&nbsp;&nbsp;方&nbsp;号:</label>
        <el-input v-model="form.formula_code" disabled style="width: 210px" />
      </el-form-item>
      <el-form-item label="产品" prop="material_code">
        <label slot="label">产品:</label>
        <el-input v-model="form.material_code" disabled style="width: 210px" />
      </el-form-item>
      <el-form-item label="批次" prop="pcsn">
        <label slot="label">批次:</label>
        <el-input v-model="form.pcsn" style="width: 210px" disabled />
      </el-form-item>
      <el-form-item label="工令号" prop="workorder_code">
        <label slot="label">工&nbsp;&nbsp;令&nbsp;号:</label>
        <el-input v-model="form.workorder_code" disabled style="width: 210px" />
      </el-form-item>
      <el-form-item label="工令类型" prop="workorder_type">
        <el-select
          v-model="form.workorder_type"
          placeholder="工令类型"
          style="width: 210px"
          disabled
          class="filter-item"
        >
          <el-option
            v-for="item in dict.workorder_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="桶类型" prop="bucket_type">
        <el-select
          v-model="form.bucket_type"
          placeholder="桶类型"
          disabled
          style="width: 210px"
          class="filter-item"
        >
          <el-option
            v-for="item in dict.bucket_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="碳平衡" prop="c_balance">
        <label slot="label">碳平衡:</label>
        <el-input-number
          v-model="form.c_balance"
          :controls="false"
          :precision="3"
          :min="1"
          disabled
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="软废含量%" prop="total_qty">
        <label slot="label">软废含量%:</label>
        <el-input-number
          v-model="form.waste_limit_down"
          :controls="false"
          :precision="0"
          :min="0"
          :max="100"
          disabled
          style="width: 90px"
        />
        ----
        <el-input-number
          v-model="form.waste_limit_up"
          :controls="false"
          :precision="0"
          :min="0"
          :max="100"
          disabled
          style="width: 90px"
        />
      </el-form-item>
      <el-form-item label="是否二次投料" prop="is_again_put">
        <el-select
          v-model="form.is_again_put"
          style="width: 210px"
          placeholder="是否二次投料"
          class="filter-item"
          disabled
        >
          <el-option
            v-for="item in dict.is_again_put"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="配粉槽" prop="storagevehicle_code">
        <label slot="label">配&nbsp;&nbsp;粉&nbsp;槽:</label>
        <el-input v-model="form.storagevehicle_code" disabled style="width: 210px" />
      </el-form-item>
      <el-form-item label="配粉模式" prop="formula_type">
        <el-select
          v-model="form.formula_type"
          placeholder="配粉模式"
          style="width: 210px"
          class="filter-item"
          disabled
        >
          <el-option
            v-for="item in dict.formula_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="球磨时间">
        <label slot="label">球磨时间:</label>
        <el-input-number
          v-model="form.ball_time"
          :controls="false"
          :precision="2"
          :min="1"
          :max="9999"
          style="width:210px"
        />
      </el-form-item>
      <el-form-item label="产品系列" prop="product_series_id">
        <el-select
          v-model="form.product_series_id"
          style="width: 210px"
          placeholder="产品系列"
          class="filter-item"
          disabled
        >
          <el-option
            v-for="item in XLList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <label slot="label">备注:</label>
        <el-input v-model="form.remark" disabled style="width: 210px" />
      </el-form-item>
    </el-form>
    <div class="crud-opts2">
      <span class="crud-opts-right2">
        <!--左侧插槽-->
        <slot name="left" />
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="button1"
          @click="allCancel()"
        >
          添加新料
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="button2"
          @click="allCancel2()"
        >
          添加软废
        </el-button>
         <el-button
           slot="left"
           class="filter-item"
           type="primary"
           icon="el-icon-plus"
           size="mini"
           :disabled="button1"
           @click="openYzj"
         >
          添加抑制剂
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="button1"
          @click="openStructIvt"
        >
          添加分类
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="button1"
          @click="openCxj"
        >
          添加成型剂
        </el-button>
      </span>
    </div>
    <div class="crud-opts2">
      <span class="role-span">物料明细</span>
      <div class="crud-opts-form">
        <el-form ref="form" :inline="true" :model="form2" size="mini">
          <el-form-item label="重量" prop="workorder_qty">
            <el-input-number
              v-model="form2.workorder_qty"
              :controls="false"
              :precision="3"
              :min="0"
              disabled
            />
          </el-form-item>
          <el-form-item label="已开单重量" prop="bill_qty">
            <el-input-number
              v-model="form2.bill_qty"
              :controls="false"
              :precision="3"
              :min="0"
              disabled
            />
          </el-form-item>
          <el-form-item label="配粉重量" prop="total_qty1">
            <el-input-number
              v-model="form2.total_qty1"
              :controls="false"
              :precision="3"
              :min="0"
              disabled
            />
          </el-form-item>
          <el-form-item label="添加物料重量" prop="add_qty1">
            <el-input-number
              v-model="form2.add_qty1"
              :controls="false"
              :precision="3"
              :min="0"
              disabled
            />
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-card class="box-card" shadow="never" :body-style="{padding:'0'}">
      <!--表格渲染-->
      <el-table
        ref="table"
        :data="tableDtl"
        style="width: 100%;"
        max-height="400"
        border
        :highlight-current-row="true"
        :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
      >
        <el-table-column type="index" label="序号" width="55" align="center" />
        <el-table-column prop="status" label="状态" align="center" width="60px" :formatter="statusFormat" />
        <el-table-column prop="bucket_type" label="桶类型" align="center" width="60px" :formatter="bucket_typeFormat" />
        <el-table-column prop="material_code" label="物料编码" align="center" width="150px" />
        <el-table-column prop="material_name" label="物料名称" align="center" width="150px" />
        <el-table-column prop="material_type" label="物料类别" align="center" :formatter="material_typeFormat" />
        <el-table-column prop="pcsn" label="批次号" align="center" width="70px" />
        <el-table-column prop="quality_scode" label="品质类型" align="center" width="70px" :formatter="quality_scodeFormat" />
        <el-table-column prop="ivt_level" label="库存等级" align="center" width="70px" :formatter="ivt_levelFormat" />
        <el-table-column prop="is_active" label="是否可用" align="center" width="70px" :formatter="is_activeFormat" />
        <el-table-column prop="seq_no" label="配粉顺序号" align="center" width="85px"  />
        <el-table-column prop="formula_qty" label="重量(KG)" :formatter="crud.formatNum3" width="150" align="center">
          <template scope="scope">
            <el-input-number v-model="scope.row.formula_qty" :disabled="scope.row.edit" :precision="3" :controls="false" :min="0" :max="is_rf_xl(scope.row)" style="width: 120px" />
          </template>
        </el-table-column>
        <el-table-column prop="fact_qty" label="实际重量(KG)" :formatter="crud.formatNum3" align="center" />
        <el-table-column prop="need_sum_qty" label="需配重量(KG)" :formatter="crud.formatNum3" align="center" />
        <el-table-column prop="sum_ivt_qty" label="库存重量(KG)" :formatter="crud.formatNum3" align="center" />
        <el-table-column prop="is_need_manage" label="是否配粉" align="center" width="70px" :formatter="is_need_manageFormat" />
        <el-table-column prop="is_need_move" label="是否移库" align="center" width="70px" :formatter="is_need_moveFormat" />
        <el-table-column prop="is_add" label="是否补料" align="center" width="70px" :formatter="is_addFormat" />
        <el-table-column prop="is_tan" label="碳%" align="center" width="80px" :formatter="crud.formatQlNum4" />
        <el-table-column prop="change_qty" label="修正值%" align="center" width="100px" :formatter="crud.formatQlNum4"/>
        <el-table-column prop="is_09030102" label="钴%" align="center" width="80px" :formatter="crud.formatQlNum4"/>
        <el-table-column prop="is_CJZB00021" label="碳化铬%" align="center" width="100px" :formatter="crud.formatQlNum4"/>
        <el-table-column prop="is_CJZB00020" label="碳化钒%" align="center" width="100px" :formatter="crud.formatQlNum4"/>
        <el-table-column prop="is_CXJ001" label="石蜡" align="center" width="80px" :formatter="crud.formatQlNum4"/>
        <el-table-column prop="is_CXJ002" label="G3蜡" align="center" width="80px" :formatter="crud.formatQlNum4"/>
        <el-table-column prop="is_CXJ003" label="PEG2000" align="center" width="80px" :formatter="crud.formatQlNum4"/>
        <el-table-column prop="is_CXJ004" label="PVAC" align="center" width="80px" :formatter="crud.formatQlNum4"/>
        <el-table-column prop="is_CXJ005" label="SBP" align="center" width="80px" :formatter="crud.formatQlNum4"/>
        <el-table-column prop="is_CXJ006" label="白油" align="center" width="80px" :formatter="crud.formatQlNum4"/>
        <el-table-column prop="is_CXJ007" label="油酸" align="center" width="80px" :formatter="crud.formatQlNum4"/>
        <el-table-column prop="is_CXJ008" label="8号分散剂" align="center" width="80px" :formatter="crud.formatQlNum4"/>
        <el-table-column align="center" label="操作" width="270" fixed="right">
          <template scope="scope">
            <el-button :disabled="scope.row.edit || scope.row.status!='10'" type="danger" class="filter-item" size="mini" icon="el-icon-delete" @click.native.prevent="deleteRow(scope.$index,tableDtl)" />
            <el-button v-show="!scope.row.edit" :disabled="scope.row.status!='10'" type="primary" class="filter-item" size="mini" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button v-show="scope.row.edit" :disabled="scope.row.status!='10'" type="success" class="filter-item" size="mini" icon="el-icon-check" @click="handleEdit(scope.$index, scope.row)">完成</el-button>
            <el-button
              type="primary" class="filter-item"
              size="mini"
              icon="el-icon-top"
              @click="moveUp(scope.$index, scope.row)"
            >
            </el-button>
            <el-button
              type="primary" class="filter-item"
              size="mini"
              icon="el-icon-bottom"
              @click="moveDown(scope.$index, scope.row)"
            >
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <StructIvt :dialog-show.sync="structshow" :rowmst="form" @StructIvtClosed="queryTableDtl" />
    <StructIvt2 :dialog-show.sync="structshow2" :rowmst="form" @StructIvtClosed="queryTableDtl2" />
    <StructIvt3 :dialog-show.sync="structshow4" :rowmst="form" @StructIvtClosed="queryTableDtl4" />
    <StructIvt4 :dialog-show.sync="structshow5" :rowmst="form" @StructIvtClosed="queryTableDtl5" />
    <materTypeDialog :dialog-show.sync="structshow3" @StructIvtClosed="queryTableDtl3" />
  </el-dialog>
</template>

<script>
import CRUD, { crud } from '@crud/crud'
import StructIvt from '@/views/wms/pf/initformula/StructIvt'
import StructIvt2 from '@/views/wms/pf/initformula/StructIvt2'
import StructIvt3 from '@/views/wms/pf/initformula/StructIvt3'
import StructIvt4 from '@/views/wms/pf/initformula/StructIvt4'
import materTypeDialog from '@/views/wms/pf/initformula/materTypeDialog'
import formula from '@/api/wms/pf/formula'
import crudseriesProcessRoute from '@/api/wms/pdm/seriesProcessRoute'
export default {
  name: 'DivDialog',
  components: { StructIvt, StructIvt2,StructIvt3, StructIvt4, materTypeDialog },
  mixins: [crud()],
  dicts: ['material_type', 'workorder_type', 'formula_type', 'ST_QUALITY_SCODE', 'ST_IVT_LEVEL', 'is_used', 'is_again_put', 'is_usable', 'bucket_type','formuladtl_status'],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    rowmst: {
      type: Object
    }
  },
  data() {
    return {
      dialogVisible: false,
      structshow: false,
      structshow2: false,
      structshow3: false,
      structshow4: false,
      structshow5: false,
      button1: true,
      button2: true,
      XLList:[],
      tableDtl: [],
      mstrow: {},
      form: {},
      form2: {
        workorder_qty: '0',
        bill_qty: '0',
        total_qty1: '0',
        add_qty1: '0'
      }
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    },
    rowmst: {
      handler(newValue, oldValue) {
        this.mstrow = newValue
      }
    }
  },
  created() {
    debugger
    crudseriesProcessRoute.getXLlist2().then(res => {
      this.XLList = res
    })
  },
  methods: {
    open() {
      formula.getform({ 'formula_id': this.mstrow.formula_id }).then(res => {
        this.form = res.material
        this.form2.workorder_qty = this.form.workorder_qty
        this.form2.bill_qty = this.form.bill_qty
        if (this.form.bucket_type === '01') {
          this.button1 = false
          this.button2 = false
        } else {
          this.button1 = true
          this.button2 = false
        }
        this.tableDtl = res.tableDtl
        this.form2.total_qty1 = 0
        this.form2.add_qty1 = 0
        for (let i = 0; i < this.tableDtl.length; i++) {
          const row = this.tableDtl[i]
          row.edit = true
          row.is_new = '0'
          if (row.is_need_manage === '1') {
            this.form2.total_qty1 = this.form2.total_qty1 + parseFloat(row.formula_qty)
          } else {
            this.form2.add_qty1 = this.form2.add_qty1 + parseFloat(row.formula_qty)
          }
          this.tableDtl.splice(i, 1, row)
        }
      })
    },
    close() {
      this.tableDtl = []
      this.$emit('DivChanged')
      this.$emit('update:dialogShow', false)
    },
    bucket_typeFormat(row, column) {
      return this.dict.label.bucket_type[row.bucket_type]
    },
    quality_scodeFormat(row, column) {
      return this.dict.label.ST_QUALITY_SCODE[row.quality_scode]
    },
    statusFormat(row, column) {
      return this.dict.label.formuladtl_status[row.status]
    },
    ivt_levelFormat(row, column) {
      return this.dict.label.ST_IVT_LEVEL[row.ivt_level]
    },
    is_activeFormat(row, column) {
      return this.dict.label.is_usable[row.is_active]
    },
    material_typeFormat(row, column) {
      return this.dict.label.material_type[row.material_type]
    },
    is_need_moveFormat(row, column) {
      if (row.is_need_move === '') {
        return '否'
      } else {
        return this.dict.label.is_again_put[row.is_need_move]
      }
    },
    is_need_manageFormat(row, column) {
      return this.dict.label.is_again_put[row.is_need_manage]
    },
    is_addFormat(row, column) {
      return this.dict.label.is_again_put[row.is_add]
    },
    openStructIvt() {
      this.structshow3 = true
    },
    moveUp(index, item) {
      if (index > 0) {
        const upDate = this.tableDtl[index - 1]
        this.tableDtl.splice(index - 1, 1)
        this.tableDtl.splice(index, 0, upDate)
      } else {
        this.$message.error('已经是第一条，不可上移')
      }
    },
    moveDown(index, item) {
      if ((index + 1) === this.tableDtl.length) {
        this.$message.error('已经是最后一条，不可下移')
      } else {
        console.log(index)
        const downDate = this.tableDtl[index + 1]
        this.tableDtl.splice(index + 1, 1)
        this.tableDtl.splice(index, 0, downDate)
      }
    },
    deleteRow(index, rows) {
      this.$confirm('是否删除？')
        .then(_ => {
          rows.splice(index, 1)
          this.form2.total_qty1 = 0
          this.form2.add_qty1 = 0
          this.tableDtl.forEach((item) => {
            if (item.is_need_manage === '1') {
              this.form2.total_qty1 = this.form2.total_qty1 + item.formula_qty
            } else {
              this.form2.add_qty1 = this.form2.add_qty1 + item.formula_qty
            }
          })
        })
        .catch(_ => {
        })
    },
    allCancel() {
      this.structshow = true
    },
    allCancel2() {
      this.structshow2 = true
    },
    openCxj() {
      this.structshow4 = true
    },
    openYzj() {
      this.structshow5 = true
    },
    handleEdit(index, row) {
      if (row.formula_qty === 0) {
        this.crud.notify('重量不能为0！')
        return false
      }
      row.edit = !row.edit
      this.tableDtl.splice(index, 1, row) // 通过splice 替换数据 触发视图更新
      if (row.edit) {
        this.form2.total_qty1 = 0
        this.form2.add_qty1 = 0
        this.tableDtl.forEach((item) => {
          if (item.is_need_manage === '1') {
            this.form2.total_qty1 = this.form2.total_qty1 + item.formula_qty
          } else {
            this.form2.add_qty1 = this.form2.add_qty1 + item.formula_qty
          }
        })
      }
    },
    queryTableDtl5(rows) {
      const tablemap = new Map()
      rows.forEach((item) => {
        if (this.tableDtl.length !== 0) {
          this.flagnow = false
          for (let i = 0; i < this.tableDtl.length; i++) {
            // ----就物料、批次、品质类型、等级、是否可用
            if (this.tableDtl[i].material_id === item.material_id  && this.tableDtl[i].status === '10') {
              this.crud.notify('该物料明细'+item.material_code+'已存在，请修改重量！')
              this.flagnow = true
            }
          }
          if (!this.flagnow) {
            const row = { edit: false, status:'10', is_rf_xl: '0',bucket_type: '01', material_type: '01', material_id: '', material_code: '',
              material_name: '', pcsn: '', is_add: '1',need_sum_qty:'0',quality_scode: '01', ivt_level: '01', is_active: '1', formula_qty: '0', sum_ivt_qty: '', is_need_move: '0', is_need_manage: '1', is_tan: '', is_09030102: '', is_CXJ001: '', is_CXJ003: '',is_CXJ004: '',is_CXJ005: '',is_CXJ006: '',is_CXJ007: '',is_CXJ008: '',is_CXJ002: '',
              qty_unit_id: '', qty_unit_name: '' }
            row.material_id = item.material_id
            row.is_need_move = item.is_need_move
            row.is_need_manage = item.is_need_manage
            row.material_name = item.material_name
            row.material_code = item.material_code
            row.qty_unit_id = item.qty_unit_id
            row.qty_unit_name = item.qty_unit_name
            row.sum_ivt_qty = item.sum_ivt_qty
            row.need_sum_qty = item.need_sum_qty
            row.formula_qty = item.need_sum_qty
            row.is_CXJ001 = item.is_CXJ001
            row.is_CXJ003 = item.is_CXJ003
            row.is_CXJ004 = item.is_CXJ004
            row.is_CXJ005 = item.is_CXJ005
            row.is_CXJ006 = item.is_CXJ006
            row.is_CXJ007 = item.is_CXJ007
            row.is_CXJ008 = item.is_CXJ008
            row.is_CXJ002 = item.is_CXJ002
            tablemap.set(item.material_id, row)
          }
        } else {
          const row = { edit: false, status:'10', is_rf_xl: '0',bucket_type: '01', material_type: '01', material_id: '', material_code: '',
            material_name: '', pcsn: '', is_add: '1', need_sum_qty:'0',quality_scode: '01', ivt_level: '01', is_active: '1', formula_qty: '0', sum_ivt_qty: '', is_need_move: '0', is_need_manage: '1', is_tan: '', is_09030102: '',is_CXJ001: '', is_CXJ003: '',is_CXJ004: '',is_CXJ005: '',is_CXJ006: '',is_CXJ007: '',is_CXJ008: '',is_CXJ002: '',
            qty_unit_id: '', qty_unit_name: '' }
          row.material_id = item.material_id
          row.is_need_move = item.is_need_move
          row.is_need_manage = item.is_need_manage
          row.material_name = item.material_name
          row.material_code = item.material_code
          row.qty_unit_id = item.qty_unit_id
          row.qty_unit_name = item.qty_unit_name
          row.sum_ivt_qty = item.sum_ivt_qty
          row.need_sum_qty = item.need_sum_qty
          row.formula_qty = item.need_sum_qty
          row.is_CXJ001 = item.is_CXJ001
          row.is_CXJ003 = item.is_CXJ003
          row.is_CXJ004 = item.is_CXJ004
          row.is_CXJ005 = item.is_CXJ005
          row.is_CXJ006 = item.is_CXJ006
          row.is_CXJ007 = item.is_CXJ007
          row.is_CXJ008 = item.is_CXJ008
          row.is_CXJ002 = item.is_CXJ002
          tablemap.set(item.material_id, row)
        }
      })
      for (const value of tablemap.values()) {
        this.tableDtl.push(value)
      }
    },
    queryTableDtl4(rows) {
      const tablemap = new Map()
      rows.forEach((item) => {
        if (this.tableDtl.length !== 0) {
          this.flagnow = false
          for (let i = 0; i < this.tableDtl.length; i++) {
            // ----就物料、批次、品质类型、等级、是否可用
            if (this.tableDtl[i].material_id === item.material_id  && this.tableDtl[i].status === '10') {
              this.crud.notify('该物料明细'+item.material_code+'已存在，请修改重量！')
              this.flagnow = true
            }
          }
          if (!this.flagnow) {
            const row = { edit: false, status:'10', is_rf_xl: '0',bucket_type: '01', material_type: '01', material_id: '', material_code: '',
              material_name: '', pcsn: '', is_add: '1',need_sum_qty:'0', quality_scode: '01', ivt_level: '01', is_active: '1', formula_qty: '0', sum_ivt_qty: '', is_need_move: '0', is_need_manage: '0', is_tan: '', is_09030102: '', is_CXJ001: '', is_CXJ003: '',is_CXJ004: '',is_CXJ005: '',is_CXJ006: '',is_CXJ007: '',is_CXJ008: '',is_CXJ002: '',
              qty_unit_id: '', qty_unit_name: '' }
            row.material_id = item.material_id
            row.is_need_move = item.is_need_move
            row.material_name = item.material_name
            row.material_code = item.material_code
            row.qty_unit_id = item.qty_unit_id
            row.qty_unit_name = item.qty_unit_name
            row.sum_ivt_qty = item.sum_ivt_qty
            row.need_sum_qty = item.need_sum_qty
            row.formula_qty = item.need_sum_qty
            row.is_CXJ001 = item.is_CXJ001
            row.is_CXJ003 = item.is_CXJ003
            row.is_CXJ004 = item.is_CXJ004
            row.is_CXJ005 = item.is_CXJ005
            row.is_CXJ006 = item.is_CXJ006
            row.is_CXJ007 = item.is_CXJ007
            row.is_CXJ008 = item.is_CXJ008
            row.is_CXJ002 = item.is_CXJ002
            tablemap.set(item.material_id, row)
          }
        } else {
          const row = { edit: false, status:'10', is_rf_xl: '0',bucket_type: '01', material_type: '01', material_id: '', material_code: '',
            material_name: '', pcsn: '', is_add: '1', need_sum_qty:'0',quality_scode: '01', ivt_level: '01', is_active: '1', formula_qty: '0', sum_ivt_qty: '', is_need_move: '0', is_need_manage: '0', is_tan: '', is_09030102: '',is_CXJ001: '', is_CXJ003: '',is_CXJ004: '',is_CXJ005: '',is_CXJ006: '',is_CXJ007: '',is_CXJ008: '',is_CXJ002: '',
            qty_unit_id: '', qty_unit_name: '' }
          row.material_id = item.material_id
          row.is_need_move = item.is_need_move
          row.material_name = item.material_name
          row.material_code = item.material_code
          row.qty_unit_id = item.qty_unit_id
          row.qty_unit_name = item.qty_unit_name
          row.sum_ivt_qty = item.sum_ivt_qty
          row.need_sum_qty = item.need_sum_qty
          row.formula_qty = item.need_sum_qty
          row.is_CXJ001 = item.is_CXJ001
          row.is_CXJ003 = item.is_CXJ003
          row.is_CXJ004 = item.is_CXJ004
          row.is_CXJ005 = item.is_CXJ005
          row.is_CXJ006 = item.is_CXJ006
          row.is_CXJ007 = item.is_CXJ007
          row.is_CXJ008 = item.is_CXJ008
          row.is_CXJ002 = item.is_CXJ002
          tablemap.set(item.material_id, row)
        }
      })
      for (const value of tablemap.values()) {
        this.tableDtl.push(value)
      }
    },
    queryTableDtl3(rows) {
      const tablemap = new Map()
      rows.forEach((item) => {
        if (this.tableDtl.length !== 0) {
          this.flagnow = false
          for (let i = 0; i < this.tableDtl.length; i++) {
            // ----就物料、批次、品质类型、等级、是否可用
            if (this.tableDtl[i].material_id === item.class_id  && this.tableDtl[i].status === '10') {
              this.crud.notify('该物料明细'+item.material_code+'已存在，请修改重量！')
              this.flagnow = true
            }
          }
          if (!this.flagnow) {
            const row = { edit: false, status:'10', is_rf_xl: '0', bucket_type: '01', material_type: '02', material_id: '', material_code: '',
              material_name: '', pcsn: '', quality_scode: '01', ivt_level: '01', is_active: '1', formula_qty: '0', sum_ivt_qty: '0', is_need_move: '0', is_need_manage: '1', is_tan: '', is_add: '1', is_09030102: '', qty_unit_id: '', qty_unit_name: '' }
            row.material_id = item.class_id
            row.material_code = item.class_code
            row.material_name = item.class_name
            if(row.material_id==='1503644362788179968'){
              row.is_tan = '100.0'
            }else if(row.material_id==='1503644362192588800'){
              row.is_09030102 = '100.0'
            }
            tablemap.set(item.class_id, row)
          }
        } else {
          const row = { edit: false,status:'10', is_rf_xl: '0', bucket_type: '01', material_type: '02', material_id: '', material_code: '',
            material_name: '', pcsn: '',quality_scode: '01', ivt_level: '01', is_active: '1', formula_qty: '0', sum_ivt_qty: '0', is_need_move: '0', is_need_manage: '1', is_tan: '', is_add: '1', is_09030102: '', qty_unit_id: '', qty_unit_name: '' }
          row.material_id = item.class_id
          row.material_code = item.class_code
          row.material_name = item.class_name
          if(row.material_id==='1503644362788179968'){
            row.is_tan = '100.0'
          }else if(row.material_id==='1503644362192588800'){
            row.is_09030102 = '100.0'
          }
          tablemap.set(item.class_id, row)
        }
      })
      for (const value of tablemap.values()) {
        this.tableDtl.push(value)
      }
    },
    queryTableDtl2(rows) {
      if (this.form.bucket_type === '01') {
        this.queryTableDtl(rows)
      } else {
        const tablemap = new Map()
        rows.forEach((item) => {
          if (this.tableDtl.length !== 0) {
            this.flagnow = false
            for (let i = 0; i < this.tableDtl.length; i++) {
              // ----就物料、批次、品质类型、等级、是否可用
              if (this.tableDtl[i].material_id === item.material_id
                && this.tableDtl[i].pcsn === item.pcsn
                && this.tableDtl[i].is_active === item.is_active
                && this.tableDtl[i].ivt_level === item.ivt_level
                && this.tableDtl[i].status === '10'
                && this.tableDtl[i].quality_scode === item.quality_scode ) {
                this.crud.notify('该物料明细'+item.material_code+'已存在，请修改重量！')
                this.flagnow = true
              }
            }
            if (!this.flagnow) {
              const row = { edit: false, status:'10', is_rf_xl: '1', bucket_type: '02', material_type: '01', material_id: '', material_code: '',
                material_name: '', pcsn: '', quality_scode: '', ivt_level: '', is_active: '', formula_qty: '0', sum_ivt_qty: '', is_need_move: '', is_need_manage: '1', is_add: '1',is_tan: '', is_09030102: '', is_CXJ001: '',
                is_CXJ003: '',is_CXJ004: '',is_CXJ005: '',is_CXJ006: '',is_CXJ007: '',is_CXJ008: '',is_CXJ002: '',
                qty_unit_id: '', qty_unit_name: '' }
              row.material_id = item.material_id
              row.quality_scode = item.quality_scode
              row.pcsn = item.pcsn
              row.material_name = item.material_name
              row.material_code = item.material_code
              row.ivt_level = item.ivt_level
              row.is_active = item.is_active
              row.sum_ivt_qty = item.sum_ivt_qty
              row.is_need_move = item.is_need_move
              row.qty_unit_id = item.qty_unit_id
              row.qty_unit_name = item.qty_unit_name
              row.is_tan = item.is_tan
              row.is_09030102 = item.is_09030102
              row.is_CXJ001 = item.is_CXJ001
              row.is_CXJ003 = item.is_CXJ003
              row.is_CXJ004 = item.is_CXJ004
              row.is_CXJ005 = item.is_CXJ005
              row.is_CXJ006 = item.is_CXJ006
              row.is_CXJ007 = item.is_CXJ007
              row.is_CXJ008 = item.is_CXJ008
              row.is_CXJ002 = item.is_CXJ002
              tablemap.set(item.material_id+"--"+item.quality_scode+"--"+item.pcsn+"--"+item.is_active+"--"+item.ivt_level, row)
            }
          } else {
            const row = { edit: false, status:'10', is_rf_xl: '1', bucket_type: '02', material_type: '01', material_id: '', material_code: '',
              material_name: '', pcsn: '', quality_scode: '', ivt_level: '', is_active: '', formula_qty: '0', sum_ivt_qty: '', is_need_move: '', is_need_manage: '1', is_add: '1', is_tan: '', is_09030102: '', is_CXJ001: '',
              is_CXJ003: '',is_CXJ004: '',is_CXJ005: '',is_CXJ006: '',is_CXJ007: '',is_CXJ008: '',is_CXJ002: '',
              qty_unit_id: '', qty_unit_name: '' }
            row.material_id = item.material_id
            row.quality_scode = item.quality_scode
            row.pcsn = item.pcsn
            row.material_name = item.material_name
            row.material_code = item.material_code
            row.ivt_level = item.ivt_level
            row.is_active = item.is_active
            row.sum_ivt_qty = item.sum_ivt_qty
            row.is_need_move = item.is_need_move
            row.qty_unit_id = item.qty_unit_id
            row.qty_unit_name = item.qty_unit_name
            row.is_tan = item.is_tan
            row.is_09030102 = item.is_09030102
            row.is_CXJ001 = item.is_CXJ001
            row.is_CXJ003 = item.is_CXJ003
            row.is_CXJ004 = item.is_CXJ004
            row.is_CXJ005 = item.is_CXJ005
            row.is_CXJ006 = item.is_CXJ006
            row.is_CXJ007 = item.is_CXJ007
            row.is_CXJ008 = item.is_CXJ008
            row.is_CXJ002 = item.is_CXJ002
            tablemap.set(item.material_id+"--"+item.quality_scode+"--"+item.pcsn+"--"+item.is_active+"--"+item.ivt_level, row)
          }
        })
        for (const value of tablemap.values()) {
          this.tableDtl.push(value)
        }
      }
    },
    queryTableDtl(rows) {
      const tablemap = new Map()
      rows.forEach((item) => {
        if (this.tableDtl.length !== 0) {
          this.flagnow = false
          for (let i = 0; i < this.tableDtl.length; i++) {
            // ----就物料、批次、品质类型、等级、是否可用
              if (this.tableDtl[i].material_id === item.material_id
                && this.tableDtl[i].pcsn === item.pcsn
                && this.tableDtl[i].is_active === item.is_active
                && this.tableDtl[i].ivt_level === item.ivt_level
                && this.tableDtl[i].status === '10'
                && this.tableDtl[i].quality_scode === item.quality_scode ) {
                this.crud.notify('该物料明细'+item.material_code+'已存在，请修改重量！')
              this.flagnow = true
            }
          }
          if (!this.flagnow) {
            const row = { edit: false, status:'10', is_rf_xl: '1', bucket_type: '01', material_type: '01', material_id: '', material_code: '',is_CJZB00021: '',is_CJZB00020: '',
              material_name: '', pcsn: '', quality_scode: '',need_sum_qty:'0', ivt_level: '', is_active: '', formula_qty: '0', sum_ivt_qty: '', is_need_move: '', is_need_manage: '1', is_add: '1', is_tan: '', is_09030102: '', qty_unit_id: '', qty_unit_name: '',change_qty:'0' }
            row.material_id = item.material_id
            row.quality_scode = item.quality_scode
            row.pcsn = item.pcsn
            row.material_name = item.material_name
            row.material_code = item.material_code
            row.sum_ivt_qty = item.sum_ivt_qty
            row.need_sum_qty = item.need_sum_qty
            row.formula_qty = item.need_sum_qty
            row.ivt_level = item.ivt_level
            row.is_active = item.is_active
            row.is_tan = item.is_tan
            row.change_qty = item.change_qty
            row.is_09030102 = item.is_09030102
            row.is_CXJ001 = item.is_CXJ001
            row.is_CXJ003 = item.is_CXJ003
            row.is_CXJ004 = item.is_CXJ004
            row.is_CXJ005 = item.is_CXJ005
            row.is_CXJ006 = item.is_CXJ006
            row.is_CXJ007 = item.is_CXJ007
            row.is_CXJ008 = item.is_CXJ008
            row.is_CXJ002 = item.is_CXJ002
            row.sum_ivt_qty = item.sum_ivt_qty
            row.is_need_move = item.is_need_move
            row.qty_unit_id = item.qty_unit_id
            row.qty_unit_name = item.qty_unit_name
            row.is_CJZB00020 = item.is_CJZB00020
            row.is_CJZB00021 = item.is_CJZB00021
            tablemap.set(item.material_id+"--"+item.quality_scode+"--"+item.pcsn+"--"+item.is_active+"--"+item.ivt_level, row)
          }
        } else {
          const row = { edit: false, status:'10', is_rf_xl: '1', bucket_type: '01', material_type: '01', material_id: '', material_code: '',is_CJZB00021: '',is_CJZB00020: '',
            material_name: '', pcsn: '', quality_scode: '',need_sum_qty:'0',  ivt_level: '', is_active: '', formula_qty: '0', sum_ivt_qty: '', is_need_move: '', is_need_manage: '1', is_add: '1', is_tan: '', is_09030102: '', qty_unit_id: '', qty_unit_name: '',change_qty:'0' }
          row.material_id = item.material_id
          row.quality_scode = item.quality_scode
          row.pcsn = item.pcsn
          row.sum_ivt_qty = item.sum_ivt_qty
          row.need_sum_qty = item.need_sum_qty
          row.formula_qty = item.need_sum_qty
          row.material_name = item.material_name
          row.material_code = item.material_code
          row.ivt_level = item.ivt_level
          row.is_tan = item.is_tan
          row.change_qty = item.change_qty
          row.is_09030102 = item.is_09030102
          row.is_CXJ001 = item.is_CXJ001
          row.is_CXJ003 = item.is_CXJ003
          row.is_CXJ004 = item.is_CXJ004
          row.is_CXJ005 = item.is_CXJ005
          row.is_CXJ006 = item.is_CXJ006
          row.is_CXJ007 = item.is_CXJ007
          row.is_CXJ008 = item.is_CXJ008
          row.is_CXJ002 = item.is_CXJ002
          row.is_active = item.is_active
          row.sum_ivt_qty = item.sum_ivt_qty
          row.is_need_move = item.is_need_move
          row.qty_unit_id = item.qty_unit_id
          row.qty_unit_name = item.qty_unit_name
          row.is_CJZB00020 = item.is_CJZB00020
          row.is_CJZB00021 = item.is_CJZB00021
          tablemap.set(item.material_id+"--"+item.quality_scode+"--"+item.pcsn+"--"+item.is_active+"--"+item.ivt_level, row)
        }
      })
      for (const value of tablemap.values()) {
        this.tableDtl.push(value)
      }
    },
    is_rf_xl(row){
      let maxNum = 9999999999.0
      if(row.is_rf_xl === '1'){
        return parseFloat(row.sum_ivt_qty).toFixed(3)
      }else{
        return parseFloat(maxNum).toFixed(3)
      }
    },
    submit() {
      if (this.tableDtl.length === 0) {
        this.crud.notify('明细不能为空！')
        return false
      }
      for (let i = 0; i < this.tableDtl.length; i++) {
        if (!this.tableDtl[i].edit) {
          this.crud.notify('尚有未完成编辑的物料，明细序号为' + (i + 1) + '请检查！')
          return false
        }
      }
      this.form.tableDtl = this.tableDtl
      formula.submit(this.form).then(res => {
        this.crud.notify('操作成功！')
        this.$emit('DivChanged')
        this.$emit('update:dialogShow', 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;
}
</style>
