<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--      查询操作-->
      <el-form ref="form" :inline="true" :model="form" label-width="70px">

        <el-input v-model="query.itemCode" clearable placeholder="品番" style="width: 140px;" class="filter-item"
                  @keyup.enter.native="crud.toQuery"
        />
        <el-select v-model="query.status" clearable placeholder="状态" value-key="id" class="filter-item"
                   style="width: 150px"
                   @keyup.enter.native="crud.toQuery"
        >
          <el-option

            v-for="item in this.dict.pick_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <AreaSelect v-model="query.ckAreaCode" :return-type="'value'" :return-value-key="'code'" :bexb="false"
                    class="filter-item" placeholder="拣货库区" style="width: 155px;"

        />
        <AreaSelect v-model="query.rkAreaCode" :return-type="'value'" :return-value-key="'code'" :bexb="true"
                    class="filter-item" placeholder="叫料库区" style="width: 155px;"

        />
        <date-range-picker v-model="query.createTime" class="date-item" style="width: 100px"/>
        <!--        搜索-->
        <rrOperation :crud="crud"/>
        <!--        重置-->
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="resetQuery">重置
        </el-button>
      </el-form>


      <!--      业务操作-->
      <crudOperation :permission="permission" :tableKey="this.$options.name">
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          size="mini"
          @click="cxjlFromFlag = true"
          icon="el-icon-document-add"
        >
          制造叫料
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          size="mini"
          @click="btnAllocate(crud.selections)"
          icon="el-icon-document-add"
        >
          分配
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          size="mini"
          @click="btnCancelAllocate(crud.selections)"
        >
          取消分配
        </el-button>

        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          size="mini"
          :loading="crud.delAllLoading"
          :disabled="show_jh"
          @click="getPickTask(crud.selections[0].id)"
        >
          拣货确认
        </el-button>

        <el-button
          slot="right"
          class="filter-item"
          type="primary"
          size="mini"
          :loading="crud.delAllLoading"
          :disabled="show_fp"
          @click="getPickDetailInv(crud.selections[0].id)"
        >
          箱单拣货确认
        </el-button>

      </crudOperation>
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small"
                style="width: 100%;"
                height="58vh"
                @selection-change="selectionChangeHandlerTwo" border
      >
        <el-table-column type="selection" width="50"/>
        <el-table-column prop="id" label="序号"/>
        <!-- <el-table-column prop="shArea.gcCode" label="工厂" width="150px"/>-->

        <el-table-column prop="item.code" label="品番" width="150px"/>
        <el-table-column prop="item.name" label="品名"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.pick_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="propC2" label="税别"/>
        <el-table-column prop="area.name" label="拣货库区" width="150px"/>
        <el-table-column prop="ckPoint.code" label="拣货库位"/>
        <el-table-column prop="po" label="拣货箱单号" width="100px"/>
        <el-table-column prop="shArea.name" label="叫料库区" width="150px"/>
        <el-table-column prop="point.code" label="制造库位"/>
        <el-table-column prop="orderQty" label="订单数量"/>
        <el-table-column prop="allocatedQty" label="分配数量"/>
        <el-table-column prop="pickedQty" label="拣货数量"/>
        <el-table-column prop="xdPf" label="箱单品番" width="100px"/>
        <el-table-column prop="item.extendD3" label="收容数" width="60px" align="center"/>
        <el-table-column prop="remark" label="备注"/>
        <el-table-column prop="createBy" label="创建人"/>
        <el-table-column prop="createTime" width="150px" label="创建时间"/>
        <el-table-column label="操作" width="150px" align="center">

          <template slot-scope="scope">
            <template>
              <el-popconfirm
                title="确定删除吗？"
                icon="el-icon-delete"
                icon-color="red"
                @confirm="deletePick(scope.row)"
              >
                <el-button slot="reference" :disabled="scope.row.status!='OPEN'" size="mini" type="danger"
                           icon="el-icon-delete"
                />
              </el-popconfirm>
            </template>

          </template>
        </el-table-column>
      </el-table>
      <!--拣货确认界面-->
      <PickTask ref="pickTask"/>
      <XdTask ref="xdTask"/>

      <!--表单组件-->
      <el-dialog title="制造叫料" :visible.sync="cxjlFromFlag" width="500px">
        <el-form ref="cxjlFrom" :model="cxjlFrom" :rules="rules" size="small" label-width="80px">
          <el-form-item label="制造库位" prop="zzkwCode">
            <BomAccountPointSelect v-model="cxjlFrom.zzkw_code" :value-key="'bom_account_id'" @change="getChangeVule"/>
          </el-form-item>
          <el-form-item label="品番">
            <el-input v-model="cxjlFrom.item_code"/>
          </el-form-item>
          <el-form-item label="品名">
            <el-input v-model="cxjlFrom.item_name"/>
          </el-form-item>
          <el-form-item label="数量">
            <el-input v-model="cxjlFrom.order_qty"/>
          </el-form-item>
          <el-form-item label="税别">
            <el-input v-model="cxjlFrom.bonded"/>
          </el-form-item>
          <el-form-item label="叫料库区">
            <el-input v-model="cxjlFrom.gw_name"/>
          </el-form-item>
          <el-form-item label="箱单单品品番">
            <el-checkbox v-model="cxjlFrom.beXdPf"/>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="cxjlFrom.remark" :rows="3" type="textarea" style="width: 370px;"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="cxjlFromFlag=false">取消</el-button>
          <el-button type="primary" @click="btnZzjl">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->

      <!--分页组件-->
      <div style="float: right;">
        <pagination/>
      </div>
    </div>
  </div>
</template>

<script>
import crudPickDetail, { zzjl, allocatePickDetail, cancelAllocatePickDetail } from '@/api/pickDetail'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation.vue'
import crudOperation from '@crud/CRUD.operation.vue'
import udOperation from '@crud/UD.operation.vue'
import pagination from '@crud/Pagination.vue'
import { queryBomAccountPoints } from '@/api/bomAccount'
import { getItems } from '@/api/item'
import PickTask from '@/views/business-pick/pickDetail/pickTicketTask.vue'
import XdTask from '@/views/business-pick/pickDetail/xdTask.vue'
import DateRangePicker from '@/components/DateRangePicker/index.vue'
import { formatDate } from '@/utils/commonUtils'
import ItemSelect from '@/views/generic-component/ItemSelect.vue'
import BomAccountPointSelect from '@/views/generic-component/BomAccountPointSelect.vue'
import { queryPickDetailXd } from '@/api/inventory'
import AreaSelect from '@/views/generic-component/AreaSelect.vue'
import PointCodeSelect from '@/views/generic-component/PointCodeSelect.vue'

const defaultForm = {
  id: null,
  remark: null
}
export default {
  name: 'PickDetail',
  components: {
    PointCodeSelect,
    AreaSelect,
    XdTask,
    BomAccountPointSelect,
    ItemSelect, DateRangePicker, PickTask, pagination, crudOperation, rrOperation, udOperation
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['pick_status'],
  cruds() {
    return CRUD({
      title: '制造叫料明细',
      url: 'api/pickDetail/queryPickDetailZzjl',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudPickDetail },
      optShow: {
        add: false,
        edit: false,
        del: true,
        reset: false,
        download: true
      }
    })
  },
  data() {
    return {
      show_fp: true,
      show_jh: true,
      show_cancelfp: true,
      radio3: '未拣货',
      items: [],
      cxjlFromFlag: false,
      cxjlFrom: {
        bom_account_id: null,
        order_qty: 0,
        item_code: null,
        item_name: null,
        zzkw_id: null,
        bonded: null,
        zzkw_code: null,
        gw_code: null,
        gw_name: null,
        status: null,
        remark: null,
        srs: 0,
        beXdPf: false
      },
      permission: {},
      rules: {
        item: [
          { required: true, message: '品番必填', trigger: 'blur' }
        ],
        zzkwCode: [
          { required: true, message: '制造库位必填', trigger: 'blur' }
        ]
      },
      itemListData: [],
      //品番
      itemOptions: [],
      zzkwPointOptions: [],
      loading_add: false
    }

  },
  mounted() {
    //初始化数据
    this.getItem()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    getItem() {
      getItems({}).then(res => {
        this.items = res.content.map(function(obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    getBomAccountPoints() {
      queryBomAccountPoints().then(res => {
        this.zzkwPointOptions = res
      })
    },
    selectionChangeHandlerTwo(val) {
      this.crud.selections = val
      if (this.crud.selections.length == 0) {
        this.show_jh = true
        this.show_fp = true
        this.show_cancelfp = true
        return
      }

      const status = []
      for (let i = 0; i < val.length; i++) {
        status.push(this.crud.selections[i].status)
      }

      if (this.isAllEqual(status) && status[0] == 'OPEN') {
        this.show_jh = true
        this.show_fp = false
        return
      }

      if (this.isAllEqual(status) && status[0] == 'ALLOCATE') {
        this.show_jh = true
        this.show_fp = false
        this.show_cancelfp = false
      }
      //拣货
      if (this.crud.selections.length == 1 && this.crud.selections[0].orderQty == this.crud.selections[0].allocatedQty) {
        this.show_jh = false
        this.show_fp = true
        return
      }

    },
    btnAllocate(datas) {
      const ids = []
      for (let i = 0; i < datas.length; i++) {
        ids.push(datas[i].id)
      }
      console.log(ids)
      allocatePickDetail(ids).then(res => {
        this.crud.toQuery()
      })
    },
    btnCancelAllocate(datas) {
      const ids = []
      for (let i = 0; i < datas.length; i++) {
        ids.push(datas[i].id)
      }
      console.log(ids)
      cancelAllocatePickDetail(ids).then(res => {
        this.crud.toQuery()
      })
    },
    getPickTask(id) {
      this.$refs.pickTask.dialog = true
      this.$refs.pickTask.queryPickTask(id)
    },

    getPickDetailInv(id) {
      this.$refs.xdTask.dialog = true
      this.$refs.xdTask.queryPickDetailXd(id)
    },
    shuaxin() {
      this.crud.toQuery()
    },
    clickChange(lab) {
      if (lab === '全部') {
        this.crud.resetQuery()
        this.crud.toQuery()
      } else if (lab === '未拣货') {
        this.query.status = 'OPEN'
        this.crud.toQuery()
      } else if (lab === '已拣未发') {
        this.query.status = 'PICK_ALL'
        this.crud.toQuery()
      } else if (lab === '已发未收') {
        this.query.status = 'SHIP_ALL'
        this.crud.toQuery()
      }
    },
    resetQuery() {
      this.radio3 = '未拣货'
      this.crud.resetQuery()
    },
    editPick(data) {
      this.crud.toEdit(data)
    },
    deletePick(data) {
      this.crud.doDelete(data)
    }, isAllEqual(array) {
      if (array.length > 0) {
        return !array.some(function(value, index) {
          return value !== array[0]
        })
      } else {
        return true
      }
    },
    getChangeVule(dben) {
      this.cxjlFrom.order_qty = dben.srs
      this.cxjlFrom.item_code = dben.item_code
      this.cxjlFrom.item_name = dben.item_name
      this.cxjlFrom.gw_code = dben.gw_code
      this.cxjlFrom.gw_name = dben.gw_name
      this.cxjlFrom.bom_account_id = dben.bom_account_id
      this.cxjlFrom.item_id = dben.item_id
      this.cxjlFrom.zzkw_id = dben.zzkw_id
      this.cxjlFrom.rk_id = dben.rk_id
      this.cxjlFrom.bonded = dben.bonded
    },

    btnZzjl() {
      console.log(this.cxjlFrom)
      zzjl(this.cxjlFrom).then(res => {
        if (res.status == 200) {
          this.crud.notify(res.message, CRUD.NOTIFICATION_TYPE.SUCCESS)
        }
        this.cxjlFromFlag = false
        this.crud.toQuery()
        this.resetForm()
      }).catch(() => {
      })
    },
    //日期格式化
    getFormatDate(data) {
      return formatDate(data)
    },
    //重置表单
    resetForm() {
      this.$refs['cxjlFrom'].resetFields()
      this.$data.cxjlFrom = JSON.parse(JSON.stringify(this.$options.data().cxjlFrom))
    }
  }
}
</script>

<style scoped>

</style>
