<template>
  <el-dialog
    append-to-body
    title="出库详情"
    :visible.sync="dialogVisible"
    destroy-on-close
    fullscreen
    @close="close"
    @open="open"
  >
    <el-form ref="form" style="border: 1px solid #cfe0df;margin-top: 10px;padding-top: 10px;" :inline="true"
             :model="form" size="mini" label-width="85px" label-suffix=":">
      <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="bill_type">
        <el-select
          v-model="form.bill_type"
          clearable
          placeholder="业务类型"
          style="width: 210px"
          class="filter-item"
          :disabled="true"
        >
          <el-option
            v-for="item in billtypelist"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关联设备" prop="bizperson">
        <label slot="label">关联设备:</label>
        <el-input v-model="form.device_code" size="mini" style="width: 210px" disabled/>
      </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="true"
        >
          <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="bizdeptid">
        <treeselect
          v-model="form.bizdeptid"
          :options="depts"
          :load-options="loadDepts"
          style="width: 200px"
          placeholder="选择部门"
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="业务人员" prop="bizperson">
        <label slot="label">业务人员:</label>
        <el-input v-model="form.bizperson" size="mini" style="width: 210px" disabled/>
      </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="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="true"/>
      </el-form-item>
    </el-form>
    <div class="crud-opts2">
      <span class="role-span2">出库明细</span>
    </div>
    <el-card class="box-card" shadow="never" :body-style="{padding:'0'}">
      <!--表格渲染-->
      <el-table
        ref="dtl_table"
        :data="tableDtl"
        style="width: 100%;"
        max-height="300"
        border
        highlight-current-row
        :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
        @current-change="handleDtlCurrentChange"
      >
        <el-table-column prop="bill_status" label="状态" align="center" width="110px">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.bill_status"
              class="filter-item"
              :disabled="true"
            >
              <el-option
                v-for="item in dict.io_bill_status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </template>
        </el-table-column>
        <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 show-overflow-tooltip prop="material_spec" label="型号规格" align="center"/>
        <el-table-column show-overflow-tooltip prop="plan_qty" :formatter="crud.formatNum3" label="数量" align="center"/>
        <el-table-column show-overflow-tooltip prop="assign_qty" :formatter="crud.formatNum3" label="已入数"
                         align="center"/>
        <el-table-column show-overflow-tooltip prop="unassign_qty" :formatter="crud.formatNum3" label="未入数"
                         align="center"/>
        <el-table-column show-overflow-tooltip prop="qty_unit_name" label="单位" align="center"/>
        <el-table-column show-overflow-tooltip prop="source_bill_type_name" label="源单类型" align="center"/>
        <el-table-column show-overflow-tooltip prop="source_bill_code" label="源单编号" align="center"/>
      </el-table>
    </el-card>
    <div class="crud-opts2">
      <span class="role-span">作业明细</span>
    </div>
    <el-card class="box-card" shadow="never" :body-style="{padding:'0'}">
      <!--表格渲染-->
      <el-table
        ref="dis_table"
        :data="tabledis"
        style="width: 100%;"
        max-height="300"
        border
        @current-change="handleDisCurrentChange"
        :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
      >
        <el-table-column show-overflow-tooltip type="index" label="序号" align="center"/>
        <el-table-column show-overflow-tooltip prop="status_name" label="状态" align="center"/>
        <el-table-column show-overflow-tooltip prop="sparepart_only_id" label="备件编码" align="center"/>
        <el-table-column show-overflow-tooltip prop="material_name" label="备件名称" align="center"/>
        <el-table-column show-overflow-tooltip prop="pcsn" label="批次" align="center"/>
        <el-table-column show-overflow-tooltip prop="real_qty" label="数量" align="center"/>
        <el-table-column show-overflow-tooltip prop="qty_unit_name" label="单位" align="center"/>
        <el-table-column show-overflow-tooltip prop="struct_name" label="货位" align="center"/>
      </el-table>
    </el-card>
  </el-dialog>
</template>

<script>

import CRUD, { crud } from '@crud/crud'
import crudStorattr from '@/api/wms/basedata/st/storattr'
import crudRawAssist from '@/api/wms/st/core/inbill/rawassist'
import sparePart from '@/api/wms/sb/sparepart'
import { getDepts, getDeptSuperior } from '@/api/system/dept'
import Treeselect, { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'ViewDialog',
  components: { Treeselect },
  mixins: [crud()],
  dicts: ['io_bill_status', 'work_status', 'task_status', 'SCH_TASK_TYPE_DTL'],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    rowmst: {
      type: Object
    }
  },
  data() {
    return {
      dialogVisible: false,
      tableDtl: [],
      tabledis: [],
      depts: [],
      billtypelist: [],
      storlist: [],
      currentdtl: null,
      currentDis: {},
      form: {}
    }
  },
  watch: {
    dialogShow: {
      handler(newValue) {
        this.dialogVisible = newValue
      }
    },
    rowmst: {
      handler(newValue) {
        this.form = newValue
      }
    }
  },
  created() {
    crudRawAssist.getType({}).then(res => {
      this.billtypelist = res
    })
    // 查询原材料库的仓库
    crudStorattr.getStor({ 'is_materialstore': '1' }).then(res => {
      this.storlist = res.content
    })
  },
  methods: {
    open() {
      this.queryTableDtl()
      crudStorattr.getStor({ 'is_attachment': '1' }).then(res => {
        this.storlist = res.content
        this.form.stor_id = this.storlist[0].stor_id
      })
      if (!this.form.bill_code) {
        this.getDepts()
      } else {
        debugger
        this.getSupDepts(this.form.bizdeptid)
      }
    },
    close() {
      this.$emit('update:dialogShow', false)
      this.currentDis = {}
      this.currentdtl = null
      this.tableDtl = []
      this.tabledis = []
      this.$emit('TaskChanged')
    },
    bill_statusFormat(row) {
      return this.dict.label.io_bill_status[row.bill_status]
    },
    taskdtl_typeFormat(row) {
      return this.dict.label.SCH_TASK_TYPE_DTL[row.taskdtl_type]
    },
    task_statusFormat(row) {
      return this.dict.label.task_status[row.task_status]
    },
    work_statusFormat(row) {
      return this.dict.label.work_status[row.work_status]
    },
    handleDtlCurrentChange(current) {
      debugger
      if (current !== null) {
        this.tabledis = []
        this.currentdtl = current
        this.queryTableDdis()
      } else {
        this.tabledis = []
        this.currentdtl = {}
      }
    },
    invtypeFormat(row, column) {
      for (const item of this.billtypelist) {
        if (item.code === row.source_bill_type) {
          return item.name
        }
      }
    },
    handleDisCurrentChange(current) {
      this.currentDis = current
    },
    queryTableDtl() {
      debugger
      sparePart.getIODtl({ 'bill_code': this.form.bill_code }).then(res => {
        this.tableDtl = res
      })
    },
    getDepts() {
      getDepts({ enabled: true }).then(res => {
        this.depts = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    getSupDepts(deptId) {
      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)
        })
      }
    },
    queryTableDdis() {
      if (this.currentdtl !== null) {
        debugger
        sparePart.getDisDtl(this.currentdtl).then(res => {
          this.tabledis = res
        }).catch(() => {
          this.tabledis = []
        })
      }
    }
  }
}
</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-span2 {
  padding: 0px 0px 20px 0px;
}

.crud-opts2 {
  padding: 10px 0px 0px 50px;
}

</style>
