<template>
  <el-dialog
    title="出库单编辑"
    append-to-body
    fullscreen
    :before-close="crud.cancelCU"
    :visible.sync="crud.status.cu > 0 || crud.status.view > 0"
    @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-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.bill_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"
          clearable
          filterable
          size="mini"
          placeholder="业务类型"
          class="filter-item"
        >
          <el-option
            v-for="item in dict.ST_INV_OUT_TYPE"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单据状态" prop="bill_status">
        <el-select
          v-model="form.bill_status"
          clearable
          style="width: 210px"
          placeholder="单据状态"
          class="filter-item"
          disabled
        >
          <el-option
            v-for="item in dict.io_bill_status"
            :key="item.id"
            :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 v-if="crud.status.cu > 0" class="crud-opts-right2">

        <!--左侧插槽-->
        <slot name="left" />
         <el-button
           slot="left"
           class="filter-item"
           type="primary"
           icon="el-icon-plus"
           size="mini"
           @click="queryOrderDtl()"
         >
          用料清单物料
        </el-button>
        <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 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="plan_qty" label="数量" width="150" align="center" />
      <el-table-column prop="qty_unit_name" label="单位" align="center" />
      <el-table-column prop="source_bill_code" label="用料清单号" :min-width="flexWidth('source_bill_code',crud.data,'用料清单号')" />
      <el-table-column v-if="crud.status.cu > 0" 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" @tableChanged="tableChanged" />
    <AddOrderDtl :dialog-show.sync="dtlShow2" :stor-id="storId" @tableChanged="tableChanged" />
  </el-dialog>
</template>

<script>
import CRUD, { crud, form } from '@crud/crud'
import AddDtl from '@/views/wms/st/outbill/AddDtl'
import AddOrderDtl from '@/views/wms/st/outbill/AddOrderDtl'
import checkoutbill from '@/views/wms/st/outbill/checkoutbill'
import crudBsrealstorattr from '@/views/wms/basedata/bsrealstorattr/bsrealstorattr'

const defaultForm = {
  bill_code: '',
  stor_id: '',
  stor_code: '',
  stor_name: '',
  bill_status: '10',
  total_qty: '0',
  detail_count: '0',
  bill_type: '',
  remark: '',
  biz_date: new Date(),
  out_stor_id: '',
  create_mode: '',
  tableData: []
}
export default {
  name: 'AddDialog',
  components: { AddDtl, AddOrderDtl },
  mixins: [crud(), form(defaultForm)],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    }
  },
  dicts: ['io_bill_status', 'ST_INV_OUT_TYPE'],
  data() {
    return {
      dialogVisible: false,
      dtlShow: false,
      dtlShow2: false,
      flagnow: 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() {
      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
      }
    },
    [CRUD.HOOK.afterToEdit]() {
      checkoutbill.getOutBillDtl({ 'iostorinv_id': this.form.iostorinv_id }).then(res => {
        this.form.tableData = res
      })
    },
    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
        }
      })
    },
    queryOrderDtl(index, row) {
      if (this.form.bill_type === '') {
        this.crud.notify('请选择业务类型！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      if (this.form.stor_id === '') {
        this.crud.notify('请选择仓库！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      if (this.form.bill_type !== '1001') {
        this.crud.notify('请选择用料出库！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      this.storId = this.form.stor_id
      this.dtlShow2 = true
    },
    queryDtl(index, row) {
      if (this.form.bill_type === '') {
        this.crud.notify('请选择业务类型！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      if (this.form.stor_id === '') {
        this.crud.notify('请选择仓库！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      if (this.form.bill_type !== '1009') {
        this.crud.notify('请选择手工出库！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      this.storId = this.form.stor_id
      this.dtlShow = true
    },
    tableChanged(rows) {
      rows.forEach((item) => {
        // 添加plan_qty
        item.plan_qty = item.qty
        if (this.form.tableData.length !== 0) {
          this.flagnow = false
          for (let i = 0; i < this.form.tableData.length; i++) {
            if (this.form.tableData[i].storagevehicle_code === item.storagevehicle_code || this.form.tableData[i].source_bill_code === item.source_bill_code) {
              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 {
          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) {
      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
    }
  }
}
</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>
