<template>
  <el-dialog
    v-loading.fullscreen.lock="fullscreenLoading"
    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-span2">出库明细</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="bill_code" label="单据号" 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 show-overflow-tooltip prop="pcsn" label="子卷号" align="center" />
        <el-table-column show-overflow-tooltip prop="sap_pcsn" label="sap批次" 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="source_bill_type" label="源单类型" align="center" />
        <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="form" :inline="true" :model="form" size="mini">
          <el-form-item prop="checked">
            <el-checkbox v-model="form.checked" @change="queryTableDdis">仅显示未完成的作业指令</el-checkbox>
          </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-position"
          size="mini"
          :disabled="button1"
          @click="issueTask"
        >
          下发
        </el-button>
        <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-position"
          size="mini"
          :disabled="button2"
          @click="finishTask"
        >
          标示完成
        </el-button>
        <!--       <el-button
          slot="left"
          class="filter-item"
          type="primary"
          icon="el-icon-close"
          size="mini"
          :disabled="button3"
          @click="cancleTaskfinish"
        >
          取消完成
        </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 show-overflow-tooltip prop="material_code" label="物料编码" align="center" width="130px" />
        <el-table-column show-overflow-tooltip prop="material_name" label="物料名称" align="center" width="130px" />
        <el-table-column show-overflow-tooltip prop="pcsn" label="子卷批次号" align="center" width="140px" />
        <el-table-column show-overflow-tooltip prop="sap_pcsn" label="sap批次号" align="center" width="120px" />
        <el-table-column show-overflow-tooltip prop="box_no" label="箱号" align="center" width="250px" />
        <el-table-column show-overflow-tooltip prop="plan_qty" :formatter="crud.formatNum3" label="重量" align="center" />
        <el-table-column show-overflow-tooltip prop="struct_code" label="起始位置" align="center" width="120px" />
        <el-table-column show-overflow-tooltip prop="next_point_code" label="目的位置" align="center" width="120px" />
        <el-table-column show-overflow-tooltip prop="task_code" label="任务号" align="center" />
        <el-table-column show-overflow-tooltip prop="task_type_name" label="任务类型" align="center" width="150px" />
        <el-table-column show-overflow-tooltip prop="task_status" label="状态" align="center" width="120px" :formatter="task_statusFormat" min-width="150px" />
        <el-table-column 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.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </el-dialog>
</template>

<script>

import CRUD, { crud } from '@crud/crud'
import checkoutbill from '@/views/wms/st/outbill/checkoutbill'

export default {
  name: 'TaskDialog',
  components: { },
  mixins: [crud()],
  dicts: ['task_status', 'io_bill_status', 'SCH_TASK_TYPE_DTL'],
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    rowmst: {
      type: Object
    },
    openArray: {
      type: Array,
      default: () => { return [] }
    }
  },
  data() {
    return {
      dialogVisible: false,
      fullscreenLoading: false,
      button1: true,
      button2: true,
      button3: true,
      tableDtl: [],
      mstrow: {},
      tabledis: [],
      currentdtl: null,
      currentDis: {},
      form: {
        checked: true
      },
      rules: {
      }
    }
  },
  watch: {
    dialogShow: {
      handler(newValue) {
        this.dialogVisible = newValue
      }
    },
    openArray: {
      handler(newValue) {
        this.tableDtl = newValue
      }
    },
    rowmst: {
      handler(newValue) {
        this.mstrow = newValue
      }
    }
  },
  methods: {
    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]
    },
    task_statusFormat(row) {
      return this.dict.label.task_status[row.task_status]
    },
    tabledisabled(row) {
      if (row.task_status === '01' && row.is_issued === '0') {
        return false
      } else {
        return true
      }
    },
    deleteRow(row) {
      if (!'00,01'.includes(row.work_status)) {
        return this.crud.notify('任务不可删除！', CRUD.NOTIFICATION_TYPE.INFO)
      }
      checkoutbill.cancelTask(row).then(res => {
        this.queryTableDtl()
        this.crud.notify('任务删除成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
      })
    },
    issueTask() {
      if (this.currentDis != null) {
        this.fullscreenLoading = true
        checkoutbill.issueTask(this.currentDis).then(res => {
          this.queryTableDdis()
          this.crud.notify('下发成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
          this.fullscreenLoading = false
        }).catch(() => {
          this.fullscreenLoading = false
        })
      }
    },
    finishTask() {
      if (this.currentDis != null) {
        this.fullscreenLoading = true
        checkoutbill.finishTask(this.currentDis).then(res => {
          this.queryTableDdis()
          this.crud.notify('操作成功成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
          this.fullscreenLoading = false
        }).catch(() => {
          this.fullscreenLoading = false
        })
      }
    },
    cancleTaskfinish() {
      if (this.currentDis != null) {
        this.fullscreenLoading = true
        checkoutbill.cancleTaskfinish(this.currentDis).then(res => {
          this.queryTableDdis()
          this.fullscreenLoading = false
        }).catch(() => {
          this.fullscreenLoading = false
        })
      }
    },
    handleDtlCurrentChange(current) {
      if (current !== null) {
        this.tabledis = []
        this.currentdtl = current
        this.queryTableDdis()
      } else {
        this.tabledis = []
        this.currentdtl = {}
      }
    },
    handleDisCurrentChange(current) {
      if (current === null || this.rowmst.bill_status === '99') {
        this.button1 = true
        this.button2 = true
        this.button3 = true
        this.currentDis = {}
        return
      }
      // debugger
      this.currentDis = current
      if (current.task_status === '04') {
        this.button1 = false
      } else {
        this.button1 = true
      }
      if (current.task_status !== '07') {
        this.button2 = false
        this.button3 = true
      } else {
        this.button2 = true
        this.button3 = false
      }
    },
    queryTableDtl() {
      this.tableDtl = []
      checkoutbill.getOutBillDtl({ 'iostorinv_id': this.mstrow.iostorinv_id }).then(res => {
        this.tableDtl = res
      })
    },
    queryTableDdis() {
      if (this.currentdtl !== null) {
        checkoutbill.getOutBillTask({ 'iostorinvdtl_id': this.currentdtl.iostorinvdtl_id, 'checked': this.form.checked }).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 .crud-opts-form {
  padding: 10px 0px 0px 50px;
}

</style>
