<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"
  >
    <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="100px" label-suffix=":">
      <el-row>
        <el-col :span="8">
          <el-form-item label="设备类别" prop="material_type_id">
            <treeselect
              v-model="form.material_type_id"
              :options="classes"
              :load-options="loadClass"
              style="width: 200px;"
              placeholder="请选择"
            />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="BOM编码" prop="device_bom_code">
            <el-input v-model="form.device_bom_code" style="width: 250px;" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="BOM名称" prop="material_name">
            <el-input v-model="form.material_name" style="width: 250px;" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" style="width: 580px;" rows="2" type="textarea" :disabled="crud.status.view > 0" />
          </el-form-item>
        </el-col>
      </el-row>
    </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-check"
          size="mini"
          @click="putMaterial"
        >
          添加物料
        </el-button>
<!--        <el-button
          slot="left"
          class="filter-item"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="delRow"
        >
          删除一行
        </el-button>-->
      </span>
    </div>

    <el-card class="box-card" shadow="never" :body-style="{padding:'0'}">
      <!--表格渲染-->
      <el-table
        ref="table2"
        :data="form.tableData"
        style="width: 100%;"
        max-height="500"
        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 show-overflow-tooltip v-if="false" prop="material_id" label="物料id" align="center" />
        <el-table-column show-overflow-tooltip v-if="false" prop="base_unit_id" label="单位id" align="center" />
        <el-table-column show-overflow-tooltip prop="material_code" label="备件物料编码" align="center" />
        <el-table-column show-overflow-tooltip prop="material_name" label="备件物料名称" align="center" />
        <el-table-column prop="qty" label="标准数量" align="center">
          <template slot-scope="scope">
            <el-input-number
              v-model="scope.row.qty"
              size="mini"
              :controls="false"
              :precision="0"
              :min="0"
              :disabled="scope.row.edit"
            />
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="base_unit_id_name" label="单位" align="center" />
        <el-table-column prop="dtl_remark" label="备注" align="center">
          <template slot-scope="scope">
            <el-input v-show="!scope.row.edit" v-model="scope.row.dtl_remark" class="input-with-select"/>
          </template>
        </el-table-column>

        <el-table-column v-permission="[]" label="操作" width="120px" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button type="danger" class="filter-item" size="mini" icon="el-icon-delete" @click.native.prevent="delRow(scope.$index, form.tableData)" />
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <DeviceMaterDialog :dialog-show.sync="materDialog" :mater-opt-code="materOpt_code" :is-single="false" @tableChanged2="tableChanged2"/>
  </el-dialog>
</template>

<script>

const defaultForm = {
  tableData: []
}

import crudClassstandard from '@/api/wms/basedata/master/classstandard'
import crudDevicebom from '@/api/wms/basedata/em/devicebom'
import DeviceMaterDialog from '@/views/wms/pub/DeviceMaterDialog'
import crudMaterialbase from '@/api/wms/basedata/master/materialbase'
import CRUD, { form, crud } from '@crud/crud'
import Treeselect, { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'AddDialog',
  components: { Treeselect, DeviceMaterDialog },
  mixins: [form(defaultForm), crud()],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    openParam: {
      type: Object
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        debugger
        this.dialogVisible = newValue
      }
    }
  },
  data() {
    return {
      classes: [],
      class_idStr: null,
      materOpt_code: '23',
      dialogVisible: false,
      materDialog: false,
      rules: {
        material_type_id: [
          { required: true, message: '设备类别不能为空', trigger: 'blur' }
        ],
        device_bom_code: [
          { required: true, message: '设备BOM编码不能为空', trigger: 'blur' }
        ],
        material_name: [
          { required: true, message: '设备BOM名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    const param = {
      'materOpt_code': this.materOpt_code
    }
    crudMaterialbase.getMaterOptType(param).then(res => {
      this.class_idStr = res.class_idStr
      this.crud.query.class_idStr = this.class_idStr
      this.crud.toQuery()
      this.queryClassId()
    })
  },
  methods: {
    [CRUD.HOOK.beforeSubmit]() {
      const arr = this.form.tableData
      if (arr.length === 0) {
        this.crud.notify('明细不能为空', CRUD.NOTIFICATION_TYPE.INFO)
        return false
      }
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].qty === undefined) {
          this.crud.notify('请填写数量', CRUD.NOTIFICATION_TYPE.INFO)
          return false
        }
      }
    },
    [CRUD.HOOK.afterToEdit]() {
      debugger
      const data = {
        'device_bom_id': this.form.device_bom_id
      }
      crudDevicebom.getDtl(data).then(res => {
        this.form.tableData = res
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    close() {
      this.$emit('update:dialogShow', false)
    },
    loadClass({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        crudClassstandard.getClass({ pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 100)
        })
      }
    },
    queryClassId() {
      const param = {
        'class_idStr': this.class_idStr
      }
      crudClassstandard.queryClassById(param).then(res => {
        this.classes = res.content.map(obj => {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    putMaterial() {
      this.materDialog = true
    },
    tableChanged2(row) {
      const arr = row
      const data = this.form.tableData
      arr.forEach((item) => {
        if (this.form.tableData.length !== 0) {
          this.fla = false
          for (let i = 0; i < data.length; i++) {
            if (data[i].material_id === item.material_id) {
              this.fla = true
            }
          }
          if (this.fla === false) {
            this.form.tableData.push(item)
          }
        } else {
          this.form.tableData.push(item)
        }
      })
    },
    delRow(index, rows) {
      rows.splice(index, 1)
    }
  }
}
</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;
}

.input-with-select  {
  background-color: #fff;
}
</style>
