<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.trim="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="check_type">
        <el-select
          v-model="form.check_type"
          clearable
          placeholder="业务类型"
          style="width: 210px"
          class="filter-item"
          :disabled="crud.status.view > 0"
        >
          <el-option
            v-for="item in dict.ST_INV_TYPE_CK"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="单据状态" prop="status">
        <el-select
          v-model="form.status"
          placeholder="单据状态"
          class="filter-item"
          :disabled="true"
        >
          <el-option
            v-for="item in dict.CHECK_BILL_STATUS"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="明细数" prop="dtl_num">
        <label slot="label">明&nbsp;&nbsp;细&nbsp;数:</label>
        <el-input v-model.trim="form.dtl_num" size="mini" disabled style="width: 210px" />
      </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.trim="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="queryDtl()"
        >
          添加盘点物料
        </el-button>
      </span>

    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      :data="form.tableData"
      border
      :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
    >
      <el-table-column type="index" label="序号" align="center" :min-width="flexWidth('index',crud.data,'序号')" />
      <el-table-column prop="status" label="状态" align="center" :formatter="bill_statusFormat" :min-width="flexWidth('status',crud.data,'状态')" />
      <el-table-column prop="sect_name" label="盘点库区" align="center" :min-width="flexWidth('sect_name',crud.data,'盘点库区')" />
      <el-table-column prop="struct_code" label="盘点货位" align="center" :min-width="flexWidth('struct_code',crud.data,'盘点货位')" />
      <el-table-column prop="storagevehicle_code" label="载具号" :min-width="flexWidth('storagevehicle_code',crud.data,'载具号')" />
      <el-table-column prop="material_code" label="物料编码" align="center" :min-width="flexWidth('material_code',crud.data,'物料编码')" />
      <el-table-column prop="material_name" label="物料名称" align="center" :min-width="flexWidth('material_name',crud.data,'物料名称')" />
      <el-table-column prop="pcsn" label="批次" :min-width="flexWidth('pcsn',crud.data,'批次')" />
      <el-table-column prop="base_qty" label="库存数量" align="center" :formatter="crud.formatNum3" :min-width="flexWidth('base_qty',crud.data,'库存数量')" />
      <el-table-column v-if="crud.status.view > 0" prop="fac_qty" label="盘点数量" align="center" :formatter="crud.formatNum3" :min-width="flexWidth('fac_qty',crud.data,'盘点数量')" />
      <el-table-column prop="qty_unit_name" label="计量单位" align="center" :min-width="flexWidth('input_time',crud.data,'创建日期')" />
      <el-table-column v-if="crud.status.cu > 0" align="center" label="操作" width="160" 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" :sect-prop="defaultList" @tableChanged="tableChanged" />
  </el-dialog>
</template>

<script>
import CRUD, { crud, form } from '@crud/crud'
import AddDtl from '@/views/wms/st/checkbill/AddDtl'
import check from '@/views/wms/st/checkbill/check'
import crudStorattr from '@/views/wms/basedata/bsrealstorattr/bsrealstorattr'

const defaultForm = {
  check_code: '',
  stor_id: '',
  stor_code: '',
  stor_name: '',
  status: '10',
  dtl_num: '0',
  check_type: '',
  remark: '',
  create_mode: '1',
  tableData: []
}
export default {
  name: 'AddDialog',
  components: { AddDtl },
  mixins: [crud(), form(defaultForm)],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    }
  },
  dicts: ['CHECK_BILL_STATUS', 'ST_INV_TYPE_CK', 'CHECK_DTL_STATUS'],
  data() {
    return {
      dialogVisible: false,
      dtlShow: false,
      flagnow: false,
      nowrow: {},
      nowindex: '',
      storlist: [],
      defaultList: [],
      rules: {
        stor_id: [
          { required: true, message: '仓库不能为空', trigger: 'blur' }
        ],
        check_type: [
          { required: true, message: '业务类型不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    dialogShow: {
      handler(newValue) {
        this.dialogVisible = newValue
      }
    }
  },
  methods: {
    open() {
      // 查询原材料库的仓库
      crudStorattr.getStor({}).then(res => {
        this.storlist = res
      })
    },
    close() {
      this.$emit('AddChanged')
    },
    [CRUD.HOOK.afterToEdit]() {
      check.getDtl({ 'check_id': this.form.check_id }).then(res => {
        this.form.tableData = res
        // 将明细变成不可编辑
        for (let i = 0; i < this.form.tableData.length; i++) {
          const row = this.form.tableData[i]
          row.edit = true
          this.form.tableData.splice(i, 1, row)
        }
      })
    },
    [CRUD.HOOK.afterToView]() {
      check.getDtl({ 'check_id': this.form.check_id }).then(res => {
        this.form.tableData = res
        // 将明细变成不可编辑
        for (let i = 0; i < this.form.tableData.length; i++) {
          const row = this.form.tableData[i]
          row.edit = true
          this.form.tableData.splice(i, 1, row)
        }
      })
    },
    bill_statusFormat(row) {
      return this.dict.label.CHECK_DTL_STATUS[row.status]
    },
    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
        }
      })
    },
    async queryDtl() {
      if (this.form.check_type === '') {
        this.crud.notify('请选择盘点单类型！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      this.defaultList = []
      this.defaultList.push(this.form.stor_id)
      this.dtlShow = true
    },
    tableChanged(rows) {
      const tablemap = new Map()
      rows.forEach((item) => {
        if (this.form.tableData.length !== 0) {
          this.flagnow = false
          for (let i = 0; i < this.form.tableData.length; i++) {
            if ((this.form.tableData[i].struct_code === item.struct_code) && (this.form.tableData[i].material_code === item.material_code)) {
              this.flagnow = true
            }
          }
          if (!this.flagnow) {
            item.edit = false
            item.status = '10'
            tablemap.set(item.struct_code + '间隔' + item.material_code, item)
          }
        } else {
          item.edit = false
          item.status = '10'
          tablemap.set(item.struct_code + '间隔' + item.material_code, item)
        }
      })
      for (const value of tablemap.values()) {
        this.form.tableData.push(value)
      }
      this.form.dtl_num = this.form.tableData.length
    },
    deleteRow(index, rows) {
      rows.splice(index, 1)
      this.nowindex = ''
      this.nowrow = {}
      this.form.dtl_num = this.form.tableData.length
    },
    [CRUD.HOOK.beforeSubmit]() {
      if (this.form.tableData.length === 0) {
        this.crud.notify('请至少选择一条明细', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
    }
  }
}
</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;
}
</style>
