<template>
  <el-dialog
    append-to-body
    :visible.sync="dialogVisible"
    destroy-on-close
    :show-close="false"
    fullscreen
    @close="close"
  >

    <span slot="title" class="dialog-footer">
      <div class="crud-opts2">
        <span class="el-dialog__title2">出库分配</span>
        <span class="crud-opts-right2">
          <!--左侧插槽-->
          <slot name="left" />
          <el-button slot="left" type="info" @click="dialogVisible = false">关闭</el-button>
        </span>
      </div>
    </span>
    <div class="crud-opts2">
      <span class="role-span">出库明细</span>
      <div class="crud-opts-form">
        <el-form ref="form" :inline="true" :model="form" size="mini">
          <el-form-item label="库区" prop="gender2">
            <el-cascader
              placeholder="请选择"
              :options="sects"
              :props="{ checkStrictly: true }"
              clearable
              @change="sectQueryChange"
            />
          </el-form-item>
        </el-form>
      </div>
      <span class="crud-opts-right2">
        <!--左侧插槽-->
        <slot name="left" />
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="allDiv()"
        >
          全部分配
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="allCancel()"
        >
          全部取消
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-check"
          size="mini"
          :disabled="button1"
          @click="oneDiv()"
        >
          自动分配
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-check"
          size="mini"
          :disabled="button2"
          @click="oneCancel()"
        >
          自动取消
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="button3"
          @click="openStructIvt"
        >
          手工分配
        </el-button>
      </span>
    </div>
    <el-card class="box-card" shadow="never" :body-style="{padding:'0'}">
      <!--表格渲染-->
      <el-table
        ref="table"
        :data="tableDtl"
        style="width: 100%;"
        max-height="300"
        border
        :highlight-current-row="true"
        :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
        @current-change="handleDtlCurrentChange"
      >
        <el-table-column prop="bill_status" label="状态" align="center" width="110px" :formatter="bill_statusFormat" />
        <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="pcsn" label="批次号" align="center" />
        <el-table-column show-overflow-tooltip prop="plan_qty" label="重量" :formatter="crud.formatNum3" align="center" />
        <el-table-column show-overflow-tooltip prop="assign_qty" label="已分配重量" :formatter="crud.formatNum3" align="center" />
        <el-table-column show-overflow-tooltip prop="unassign_qty" label="未分配重量" :formatter="crud.formatNum3" align="center" />
        <el-table-column prop="quality_scode" label="品质类型" align="center" width="110px" :formatter="quality_scodeFormat" />
        <el-table-column prop="source_bill_type" label="源单类型" align="center" width="130px" :formatter="invtypeFormat" />
        <el-table-column show-overflow-tooltip prop="source_bill_code" label="源单编号" align="center" />
        <el-table-column show-overflow-tooltip prop="qty_unit_name" label="单位" align="center" />
      </el-table>
    </el-card>
    <div class="crud-opts2">
      <span class="role-span">分配明细</span>
      <div class="crud-opts-form">
        <el-form ref="form2" :inline="true" :model="form2" size="mini">
          <el-form-item label="待分配" prop="unassign_qty">
            <el-input-number
              v-model="form2.unassign_qty"
              :controls="false"
              :precision="3"
              :min="0"
              disabled
            />
          </el-form-item>
          <el-form-item label="已分配" prop="assign_qty">
            <el-input-number
              v-model="form2.assign_qty"
              :controls="false"
              :precision="3"
              :min="0"
              disabled
            />
          </el-form-item>
          <el-form-item label="站点选择" prop="point_code">
            <el-input v-model="form2.point_code" disabled class="input-with-select" style="width: 200px">
              <el-button slot="append" icon="el-icon-search" @click="openPoint" />
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <span class="crud-opts-right2">
        <!--左侧插槽-->
        <slot name="left" />
        <el-button
          slot="left"
          class="filter-item"
          type="warning"
          icon="el-icon-check"
          size="mini"
          :disabled="button4"
          @click="setPoint()"
        >
          设置站点
        </el-button>
      </span>
    </div>
    <el-card class="box-card" shadow="never" :body-style="{padding:'0'}">
      <!--表格渲染-->
      <el-table
        ref="table2"
        :data="tabledis"
        style="width: 100%;"
        max-height="300"
        border
        :highlight-current-row="true"
        :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
        @current-change="handleDisCurrentChange"
      >
        <el-table-column prop="bill_status" label="状态" align="center" width="110px">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.work_status"
              class="filter-item"
              :disabled="true"
            >
              <el-option
                v-for="item in dict.work_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="pcsn" label="子卷批次号" align="center" />
        <el-table-column show-overflow-tooltip prop="plan_qty" label="出库重量" :formatter="crud.formatNum3" align="center" />
        <el-table-column show-overflow-tooltip prop="struct_code" label="仓位编码" align="center" />
        <el-table-column show-overflow-tooltip prop="struct_name" label="仓位名称" align="center" />
        <el-table-column show-overflow-tooltip prop="task_code" label="任务号" align="center" />
        <el-table-column show-overflow-tooltip prop="point_code" label="出库点" align="center" />
        <el-table-column align="center" label="操作" width="160" fixed="right">
          <template scope="scope">
            <el-button :disabled="tabledisabled(scope.row)" type="danger" class="filter-item" size="mini" icon="el-icon-delete" @click.native.prevent="deleteRow(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <pointDialog ref="child" :dialog-show.sync="pointshow" :dialog-typedisable="typedisable" :dialog-areatype="areatype" @PointChanged="PointChanged" />
    <StructIvt :dialog-show.sync="structshow" :open-array="openParam" :rowmst="openRow" @StructIvtClosed="queryTableDtl" />
  </el-dialog>
</template>

<script>
import CRUD, { crud } from '@crud/crud'
import checkoutbill from '@/views/wms/st/outbill/checkoutbill'
import StructIvt from '@/views/wms/st/outbill/StructIvt'
import PointDialog from '@/views/wms/pub/PointDialog'
import crudSectattr from '@/views/wms/basedata/st/sect/sectattr'

export default {
  name: 'DivDialog',
  components: { PointDialog, StructIvt },
  mixins: [crud()],
  dicts: ['io_bill_status', 'ST_QUALITY_SCODE', 'ST_IVT_LEVEL', 'is_used', 'work_status', 'is_usable'],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    rowmst: {
      type: Object
    },
    openArray: {
      type: Array,
      default: () => { return [] }
    }
  },
  data() {
    return {
      dialogVisible: false,
      areatype: '',
      typedisable: false,
      pointshow: false,
      structshow: false,
      button1: true,
      button2: true,
      button3: true,
      button4: true,
      tableDtl: [],
      openParam: [],
      mstrow: {},
      openRow: {},
      tabledis: [],
      currentRow: {},
      currentDis: {},
      form: {
        gender2: ''
      },
      form2: {
        unassign_qty: '0',
        assign_qty: '0',
        point_code: ''
      },
      sects: [],
      invtypelist: [],
      rules: {
      }
    }
  },
  watch: {
    dialogShow: {
      handler(newValue, oldValue) {
        this.dialogVisible = newValue
      }
    },
    openArray: {
      handler(newValue, oldValue) {
        this.tableDtl = newValue
      }
    },
    rowmst: {
      handler(newValue, oldValue) {
        this.mstrow = newValue
        this.mstrow.stor_id = ''
      }
    }
  },
  created() {
    crudSectattr.getSect({ is_materialstore: '1' }).then(res => {
      this.sects = res.content
    })
    checkoutbill.getInvTypes().then(res => {
      this.invtypelist = res
    })
  },
  methods: {
    close() {
      this.$refs['form2'].resetFields()
      this.tabledis = []
      this.$emit('DivChanged')
      this.$emit('update:dialogShow', false)
    },
    bill_statusFormat(row, column) {
      return this.dict.label.io_bill_status[row.bill_status]
    },
    quality_scodeFormat(row, column) {
      return this.dict.label.ST_QUALITY_SCODE[row.quality_scode]
    },
    ivt_levelFormat(row, column) {
      return this.dict.label.ST_IVT_LEVEL[row.ivt_level]
    },
    is_activeFormat(row, column) {
      return this.dict.label.is_usable[row.is_active]
    },
    invtypeFormat(row, column) {
      for (const item of this.invtypelist) {
        if (item.code === row.source_bill_type) {
          return item.name
        }
      }
    },
    openPoint() {
      if (this.mstrow.bill_type === '010101') {
        this.areatype = "'31'"
      } else if (this.mstrow.bill_type === '010201' || this.mstrow.bill_type === '010202' || this.mstrow.bill_type === '010401') {
        this.areatype = "'21'"
      } else if (this.mstrow.bill_type === '010503' || this.mstrow.bill_type === '010501') {
        this.areatype = "'22','23','24'"
      } else if (this.mstrow.bill_type === '010502') {
        this.areatype = "'21','22','23','24'"
      } else if (this.mstrow.bill_type === '010801') {
        this.areatype = "'22','23','24','31','35'"
      } else if (this.mstrow.bill_type === '010601') {
        this.areatype = ''
      }
      this.pointshow = true
      this.$refs.child.getMsg(this.areatype)
    },
    PointChanged(row) {
      this.form2.point_code = row.point_code
    },
    openStructIvt() {
      this.currentRow.remark = ''
      checkoutbill.getStructIvt(this.currentRow).then(res => {
        this.openParam = res
        this.structshow = true
        this.openRow = this.currentRow
      })
    },
    sectQueryChange(val) {
      if (val.length === 1) {
        this.mstrow.stor_id = val[0]
        this.mstrow.sect_id = ''
      }
      if (val.length === 0) {
        this.mstrow.sect_id = ''
        this.mstrow.stor_id = ''
      }
      if (val.length === 2) {
        this.mstrow.stor_id = val[0]
        this.mstrow.sect_id = val[1]
      }
    },
    tabledisabled(row) {
      if ((row.work_status === '00' || row.work_status === '01') && row.is_issued === '0') {
        return false
      } else {
        return true
      }
    },
    deleteRow(row) {
      checkoutbill.allCancel(row).then(res => {
        this.queryTableDtl()
      })
    },
    handleDtlCurrentChange(current) {
      if (current !== null) {
        this.currentRow = current
        this.form2.unassign_qty = current.unassign_qty
        this.form2.assign_qty = current.assign_qty
        this.tabledis = []
        if (current.bill_status === '10') {
          this.button1 = false
          this.button2 = true
          this.button3 = false
        } else if (current.bill_status === '30') {
          this.button1 = false
          this.button2 = false
          this.button3 = false
        } else if (current.bill_status === '40') {
          this.button1 = true
          this.button2 = false
          this.button3 = true
        }
        this.queryTableDdis(current.iostorinvdtl_id)
      } else {
        this.button1 = true
        this.button2 = true
        this.button3 = true
        this.mstrow.iostorinvdtl_id = ''
        this.currentRow = {}
        this.tabledis = []
      }
    },
    handleDisCurrentChange(current) {
      if (current !== null) {
        this.currentDis = current
        if (current.work_status === '01' || current.work_status === '00') {
          this.button4 = false
        } else {
          this.button4 = true
        }
      } else {
        this.button4 = true
        this.mstrow.iostorinvdis_id = ''
        this.currentDis = {}
      }
    },
    allDiv() {
      checkoutbill.allDiv(this.mstrow).then(res => {
        this.crud.notify('分配成功！', CRUD.NOTIFICATION_TYPE.INFO)
        this.queryTableDtl()
      })
    },
    oneDiv() {
      if (this.currentRow.iostorinvdtl_id !== null) {
        this.mstrow.iostorinvdtl_id = this.currentRow.iostorinvdtl_id
        checkoutbill.allDiv(this.mstrow).then(res => {
          this.queryTableDtl()
        })
      }
    },
    allCancel() {
      checkoutbill.allCancel(this.mstrow).then(res => {
        this.queryTableDtl()
      })
    },
    oneCancel() {
      if (this.currentRow.iostorinvdtl_id !== null) {
        checkoutbill.allCancel(this.currentRow).then(res => {
          this.queryTableDtl()
        })
      }
    },
    setPoint() {
      if (this.form2.point_code === '') {
        this.crud.notify('请先选择检验站点！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      if (this.currentDis.point_code) {
        this.crud.notify('站点已设置！', CRUD.NOTIFICATION_TYPE.INFO)
        return
      }
      if (this.currentDis.iostorinvdis_id !== null) {
        this.currentDis.point_code = this.form2.point_code
        checkoutbill.setPoint(this.currentDis).then(res => {
          this.queryTableDdis(this.currentDis.iostorinvdtl_id)
        })
      }
    },
    queryTableDtl() {
      checkoutbill.getOutBillDtl({ 'iostorinv_id': this.mstrow.iostorinv_id }).then(res => {
        this.tableDtl = res
      })
    },
    queryTableDdis(iostorinvdtl_id) {
      checkoutbill.getOutBillDis({ 'iostorinvdtl_id': iostorinvdtl_id, 'bill_status': '01' }).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-span {
  padding: 10px 0px 10px 0px;
}
.crud-opts2 .crud-opts-form {
  padding: 10px 0px 0px 20px;
}

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