<template>
  <el-dialog
    title="设备档案编辑"
    width="800px"
    :before-close="crud.cancelCU"
    :visible.sync="crud.status.cu > 0 || crud.status.view > 0"
    @close="close"
    @open="open"
  >
    <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="105px" label-suffix=":">
      <el-form-item label="设备档案编码" prop="devicerecord_code">
        <label slot="label">设备档案编码:</label>
        <el-input v-model="form.devicerecord_code" disabled placeholder="系统生成" style="width: 210px" />
      </el-form-item>
      <el-form-item label="设备状态" prop="status">
        <el-select
          v-model="form.status"
          placeholder="设备状态"
          style="width: 210px"
          class="filter-item"
          disabled
        >
          <el-option
            v-for="item in dict.device_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备编码" prop="device_code">
        <label slot="label">设备编码:</label>
        <el-input v-model="form.device_code" clearable  style="width: 210px" />
      </el-form-item>
      <el-form-item label="设备名称" prop="device_name">
        <label slot="label">设备名称:</label>
        <el-input v-model="form.device_name"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="设备规格" prop="device_spec">
        <label slot="label">设备规格:</label>
        <el-input v-model="form.device_spec"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="设备型号" prop="device_model">
        <label slot="label">设备型号:</label>
        <el-input v-model="form.device_model"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="制造商" prop="manufacturer">
        <label slot="label">制造商:</label>
        <el-input v-model="form.manufacturer"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="制造国别" prop="country_manufactur">
        <label slot="label">制造国别:</label>
        <el-input v-model="form.country_manufactur"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="出厂编号" prop="leavefactory_number">
        <label slot="label">出厂编号:</label>
        <el-input v-model="form.leavefactory_number"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="出厂日期" prop="leavefactory_date">
        <label slot="label">出厂日期:</label>
        <el-date-picker v-model="form.leavefactory_date" value-format="yyyy-MM-dd" type="date" placeholder="出厂日期" style="width: 210px"   />
      </el-form-item>
      <el-form-item label="图号" prop="drawing_number">
        <label slot="label">图号:</label>
        <el-input v-model="form.drawing_number"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="供应商" prop="supplier_name">
        <label slot="label">供应商:</label>
        <el-input v-model="form.supplier_name"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="设备类别" prop="material_type_id">
        <treeselect
          v-model="form.material_type_id"
          :options="classes"
          :load-options="loadClass"
          style="width: 210px;"
          placeholder="设备类别"
        />
      </el-form-item>
      <el-form-item label="生产用途" prop="is_produceuse">
        <el-select
          v-model="form.is_produceuse"
          placeholder="生产用途"
          style="width: 210px"
          class="filter-item"
        >
          <el-option
            v-for="item in dict.is_produceuse"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用部门" prop="use_deptid">
        <treeselect
          v-model="form.use_deptid"
          :options="depts"
          :load-options="loadDepts"
          style="width: 210px"
          placeholder="使用部门"
        />
      </el-form-item>
      <el-form-item label="使用班组" prop="use_groupid">
        <el-select
          v-model="form.use_groupid"
          placeholder="使用班组"
          style="width: 210px"
          class="filter-item"
        >
          <el-option
            v-for="item in dict.device_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备属性" prop="device_type">
        <el-select
          v-model="form.device_type"
          placeholder="设备属性"
          style="width: 210px"
          class="filter-item"
        >
          <el-option
            v-for="item in dict.device_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="外部编码" prop="extend_code">
        <label slot="label">外部编码:</label>
        <el-input v-model="form.extend_code"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="资产编码" prop="assets_code">
        <label slot="label">资产编码:</label>
        <el-input v-model="form.assets_code"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="资产名称" prop="assets_name">
        <label slot="label">资产名称:</label>
        <el-input v-model="form.assets_name"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="归属部门" prop="belong_deptid">
        <treeselect
          v-model="form.belong_deptid"
          :options="depts"
          :load-options="loadDepts"
          style="width: 210px"
          placeholder="选择部门"
        />
      </el-form-item>
      <el-form-item label="启用日期" prop="beginuse_date">
        <label slot="label">启用日期:</label>
        <el-date-picker v-model="form.beginuse_date" value-format="yyyy-MM-dd" type="date" placeholder="启用日期" style="width: 210px"   />
      </el-form-item>
      <el-form-item label="工序" prop="workprocedure_id">
        <el-select
          v-model="form.workprocedure_id"
          clearable
          placeholder="工序"
          style="width: 210px"
          class="filter-item"
        >
          <el-option
            v-for="item in workProcedureList"
            :key="item.workprocedure_id"
            :label="item.workprocedure_name"
            :value="item.workprocedure_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="理论节拍" prop="theory_beat">
        <label slot="label">理论节拍:</label>
        <el-input-number
          v-model="form.theory_beat"
          :controls="false"
          :precision="3"
          :min="0"
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="设备BOM" prop="device_bom_id">
        <el-select
          v-model="form.device_bom_id"
          placeholder="设备BOM"
          style="width: 210px"
          class="filter-item"
        >
          <el-option
            v-for="item in bomlist"
            :key="item.device_bom_id"
            :label="item.material_name"
            :value="item.device_bom_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="卡片编号" prop="card_code">
        <label slot="label">卡片编号:</label>
        <el-input v-model="form.card_code"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="安装地点" prop="setting_place">
        <label slot="label">安装地点:</label>
        <el-input v-model="form.setting_place"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="使用人" prop="user_name">
        <label slot="label">使用人:</label>
        <el-input v-model="form.user_name"  clearable style="width: 210px" />
      </el-form-item>
      <el-form-item label="本币原值" prop="oraginal_value">
        <label slot="label">本币原值:</label>
        <el-input-number
          v-model="form.oraginal_value"
          :controls="false"
          :precision="3"
          :min="0"
          style="width: 210px"
        />
      </el-form-item>
      <el-form-item label="净额" prop="net_amount">
        <label slot="label">净额:</label>
        <el-input-number
          v-model="form.net_amount"
          :controls="false"
          :precision="3"
          :min="0"
          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="form.remark" style="width: 480px;" rows="3" type="textarea" :disabled="crud.status.view > 0" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button slot="left" type="info" @click="crud.cancelCU">关闭</el-button>
      <el-button slot="left" type="primary" :loading="crud.cu === 2" @click="crud.submitCU">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import crudOperation from '@crud/CRUD.operation'
import CRUD, { crud, form } from '@crud/crud'
import crudWorkProcedure from '@/api/wms/basedata/pdm/workProcedure'
import crudClassstandard from '@/api/wms/basedata/master/classstandard'
import crudMaterialbase from '@/api/wms/basedata/master/materialbase'
import equipmentfile from '@/api/wms/basedata/em/equipmentfile'
import Treeselect, { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { getDepts, getDeptSuperior } from '@/api/system/dept'

const defaultForm = {
  devicerecord_id: '',
  devicerecord_code: '',
  status: '00',
  device_code: '',
  device_name: '',
  device_model: '',
  device_spec: '',
  device_type: '',
  manufacturer: '',
  country_manufactur: '',
  leavefactory_date: new Date().format('yyyy-MM-dd'),
  leavefactory_number: '',
  material_type_id: null,
  drawing_number: '',
  supplier_name: '',
  supplier_id: '',
  supplier_code: '',
  is_produceuse: '1',
  use_groupid: '',
  user_id: '',
  user_code: '',
  user_name: '',
  use_deptid: null,
  belong_deptid: null,
  extend_code: '',
  card_code: '',
  assets_id: '',
  assets_code: '',
  assets_name: '',
  beginuse_date: new Date().format('yyyy-MM-dd'),
  workprocedure_id: '',
  setting_place: '',
  device_bom_id: '',
  theory_beat: '',
  oraginal_value: '',
  net_amount: '',
  remark: ''
}
export default {
  name: 'AddDialog',
  components: { crudOperation, Treeselect },
  mixins: [crud(), form(defaultForm)],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    }
  },
  dicts: ['device_type', 'workorder_type', 'is_produceuse', 'device_status'],
  data() {
    return {
      dialogVisible: false,
      dtlShow: false,
      workProcedureList: [],
      classes: [],
      depts: [],
      bomlist: [],
      class_idStr: null,
      materShow: false,
      rules: {
        device_type: [
          { required: true, message: '设备属性不能为空', trigger: 'blur' }
        ],
        device_name: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ],
        device_code: [
          { required: true, message: '设备编码不能为空', trigger: 'blur' }
        ],
        material_type_id: [
          { required: true, message: '设备类别不能为空', trigger: 'blur' }
        ],
        device_bom_id: [
          { required: true, message: '设备Bom不能为空', trigger: 'blur' }
        ],
        is_produceuse: [
          { required: true, message: '生产用途不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    dialogShow: {
      handler(newValue) {
        this.dialogVisible = newValue
      }
    }
  },
  created() {
    crudWorkProcedure.downSelect().then(res => {
      this.workProcedureList = res
    })

    equipmentfile.queryBomList().then(res => {
      this.bomlist = res
    })
  },
  methods: {
    close() {
      this.$emit('AddChanged')
    },
    open() {
    },
    [CRUD.HOOK.afterToCU](crud, form) {
      debugger
      if (!form.devicerecord_code) {
        this.getDepts()

        const param = {
          'materOpt_code': '23'
        }
        crudMaterialbase.getMaterOptType(param).then(res => {
          this.class_idStr = res.class_idStr
          this.queryClassId()
        })
      } else {
        debugger
        this.getSupDepts(form.use_deptid)
        this.getSubTypes(form.material_type_id)
      }
    },
    getSubTypes(id) {
      debugger
      crudClassstandard.getClassSuperior(id).then(res => {
        debugger
        const date = res.content
        this.buildClass(date)
        this.classes = date
      })
    },
    buildClass(classes) {
      classes.forEach(data => {
        if (data.children) {
          this.buildClass(data.children)
        }
        if (data.hasChildren && !data.children) {
          data.children = null
        }
      })
    },
    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
        })
      })
    },
    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)
        })
      }
    },
    getSupDepts(deptId) {
      debugger
      var number = parseFloat(deptId)
      getDeptSuperior(number).then(res => {
        const date = res.content
        this.buildDepts(date)
        this.depts = date
      })
    },
    buildDepts(depts) {
      debugger
      depts.forEach(data => {
        if (data.children) {
          this.buildDepts(data.children)
        }
        if (data.hasChildren && !data.children) {
          data.children = null
        }
      })
    },
    // 获取弹窗内部门数据
    loadDepts({ action, parentNode, callback }) {
      if (action === LOAD_CHILDREN_OPTIONS) {
        getDepts({ enabled: true, pid: parentNode.id }).then(res => {
          parentNode.children = res.content.map(function(obj) {
            if (obj.hasChildren) {
              obj.children = null
            }
            return obj
          })
          setTimeout(() => {
            callback()
          }, 200)
        })
      }
    },
    getDepts() {
      getDepts({ enabled: true }).then(res => {
        this.depts = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    [CRUD.HOOK.beforeSubmit]() {
    }
  }
}
</script>

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