<template>
  <el-dialog
    :title="crud.status.title"
    append-to-body
    fullscreen
    :before-close="crud.cancelCU"
    :visible.sync="crud.status.cu > 0 || crud.status.view > 0"
    width="1200px"
    @open="open"
    @close="close"
  >
    <el-row v-show="crud.status.cu > 0" :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" :loading="crud.cu === 2" type="primary" @click="crud.submitCU">保存</el-button>
          <el-button icon="el-icon-close" size="mini" type="info" @click="crud.cancelCU">关闭</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-form-item label="载具编码" prop="storagevehicle_code">
        <label slot="label">载具编码:</label>
        <el-input v-model="form.storagevehicle_code" 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"
        >
          <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="ext_code">
        <label slot="label">源单据号:</label>
        <el-input v-model="form.ext_code" clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="源单类型" prop="ext_type">
        <el-select
          v-model="form.ext_type"
          clearable
          placeholder="源单类型"
          style="width: 210px"
          class="filter-item"
          :disabled="crud.status.view > 0"
        >
          <el-option
            v-for="item in dict.ST_INV_IN_TYPE"
            :key="item.value"
            :disabled="item.value === '0003'"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </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>

    <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
    <div class="crud-opts2" style="margin-bottom: 5px;">
      <span class="crud-opts-right2">
        <!--左侧插槽-->
        <slot name="left" />
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="insertEvent()"
        >
          添加物料
        </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="55" align="center" />
      <el-table-column show-overflow-tooltip width="180" prop="material_code" label="物料编码" />
      <el-table-column show-overflow-tooltip width="260" prop="material_name" label="物料名称" />
      <el-table-column show-overflow-tooltip width="260" prop="material_spec" label="物料规格" />
      <el-table-column show-overflow-tooltip width="180" prop="pcsn" label="批次号">
        <template slot-scope="scope">
          <el-input v-model="scope.row.pcsn" style="width: 150px;" />
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="180" prop="qty" label="数量">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.qty" style="width: 150px;" />
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip width="150" prop="qty_unit_name" label="计量单位名称" />
      <el-table-column v-if="crud.status.cu > 0" align="center" label="操作" width="150" 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>

    <MaterialDialog :dialog-show.sync="dtlShow" :is-single.sync="isSingle" @materialChoose="materialChoose" />

  </el-dialog>
</template>

<script>
import CRUD, { crud, form } from '@crud/crud'
import MaterialDialog from '@/views/wms/basedata/material/MaterialDialog'
import crudBsrealstorattr from '@/views/wms/basedata/bsrealstorattr/bsrealstorattr'

const defaultForm = {
  ext_code: '',
  storagevehicle_code: '',
  stor_id: '',
  status: '10',
  ext_type: '0',
  remark: '',
  tableData: []
}

export default {
  name: 'AddDialog',
  components: { MaterialDialog },
  mixins: [crud(), form(defaultForm)],
  dicts: ['io_bill_status', 'ST_QUALITY_SCODE', 'ST_INV_IN_TYPE'],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    bussConfig: {
      type: Object
    },
    openParam: {
      type: Object
    }
  },
  data() {
    return {
      dialogVisible: false,
      dtlShow: false,
      isSingle: false,
      bill_btn: false,
      mater_btn: false,
      storlist: [],
      billtypelist: [],
      rules: {
        storagevehicle_code: [
          { required: true, message: '载具不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    }
  },
  methods: {
    open() {
      crudBsrealstorattr.getStor().then(res => {
        this.storlist = res
      })
    },
    close() {
      this.$emit('AddChanged')
    },
    [CRUD.HOOK.beforeSubmit]() {
      // 提交前校验
      if (this.form.tableData.length === 0) {
        this.crud.notify('请至少选择一条明细', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
    },

    fun(val) {
      return Number(val).toFixed(3)
    },
    async insertdtl() {
      if (this.form.bill_type === '') {
        this.crud.notify('请选择业务类型！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      this.form.tableData.push({ quality_scode: '01', is_active: '1', ivt_level: '01', qty: '0' })
      this.form.detail_count = this.form.tableData.length
    },
    deleteRow(index, rows) {
      const pcsn = rows[index].pcsn
      let len = rows.length
      while (len--) {
        const obj = rows[len]
        if (pcsn === obj.pcsn) {
          const index = rows.indexOf(obj)
          if (index > -1) { // 移除找到的指定元素
            this.form.total_qty = parseFloat(this.form.total_qty) - parseFloat(rows[index].qty)
            rows.splice(index, 1)
            this.form.detail_count = this.form.tableData.length
          }
        }
      }
    },
    materialChoose(row) {
      debugger
      // 对新增的行进行校验不能存在相同物料批次
      row.forEach((item) => {
        debugger
        let same_mater = true
        this.form.tableData.forEach((row) => {
          debugger
          if (row.material_code === item.material_code && row.pcsn === item.pcsn) {
            debugger
            same_mater = false
          }
        })
        if (same_mater) {
          this.form.tableData.splice(-1, 0, item)
        }
      })
    },
    async insertEvent(row) {
      this.dtlShow = true
    }
  }
}
</script>

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

.crud-opts2 .crud-opts-right2 {
  margin-left: auto;
}
</style>
