<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--      查询操作-->
      <el-form ref="form" :inline="true" :model="form" label-width="70px">
        <el-form-item label="物料编码">
          <!--          <el-input v-model="query.itemCode" clearable placeholder="请输入物料编码" style="width: 140px;"-->
          <!--                    class="filter-item"-->
          <!--                    @keyup.enter.native="crud.toQuery"/>-->
          <el-select v-model="query.itemCode" placeholder="物料代码" style="width: 140px;" class="filter-item">
            <el-option
              v-for="item in this.itemListData"
              :key="item.code"
              :label="item.code"
              :value="item.code"
              @keyup.enter.native="crud.toQuery"
            >
              <span style="float: left">{{ item.code }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="物料名称">
          <el-input
            v-model="query.itemName"
            clearable
            placeholder="请输入物料名称"
            style="width: 140px;"
            class="filter-item"
            @keyup.enter.native="crud.toQuery"
          />
        </el-form-item>

        <el-form-item label="任务号">

          <el-input
            v-model="query.po"
            clearable
            placeholder="请输入任务号"
            style="width: 140px;"
            class="filter-item"
            @keyup.enter.native="crud.toQuery"
          />
        </el-form-item>

        <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>

      <div class="statusButton" style="border-bottom: solid lightgray 1px;">
        <el-radio-group v-model="radio3" size="small" @change="clickChange">
          <el-radio-button label="全部"> 全部</el-radio-button>
          <el-radio-button label="打开"> 打开</el-radio-button>
          <el-radio-button label="已分配">已分配</el-radio-button>
          <el-radio-button label="拣货完成"> 拣货完成</el-radio-button>
        </el-radio-group>

      </div>

      <!--      业务操作-->
      <crudOperation :permission="permission" :table-key="tableKey">
        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="show_fp"
          @click="allocate(crud.selections)"
        >
          分配
        </el-button>

        <el-button
          slot="right"
          class="filter-item"
          type="success"
          icon="el-icon-edit"
          size="mini"
          :loading="crud.delAllLoading"
          :disabled="show_cancelfp"
          @click="cancelAllocate(crud.selections)"
        >
          取消分配
        </el-button>

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

      </crudOperation>
      <el-table
        ref="table"
        v-loading="crud.loading"
        border
        :data="crud.data"
        size="small"
        style="width: 100%;"
        height="58vh"
        @selection-change="selectionChangeHandlerTwo"
      >
        <el-table-column type="selection" width="50" />
        <el-table-column
          prop="date"
          align="center"
          label="序号"
          :resizable="false"
          type="index"
          width="50"
        />
        <el-table-column prop="pick.relatedBill1" width="120" label="系统单号" />
        <el-table-column :show-overflow-tooltip="true" prop="itemName" label="物料编码">
          <template slot-scope="scope">
            <div>{{ scope.row.item.code }}</div>
          </template>
        </el-table-column>
        <el-table-column :show-overflow-tooltip="true" prop="itemCode" label="物料名称">
          <template slot-scope="scope">
            <div>{{ scope.row.item.name }}</div>
          </template>
        </el-table-column>
        <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="propC1" label="中标批次名称" />
        <el-table-column prop="propC2" label="成品物料编码" />
        <el-table-column prop="propC3" :show-overflow-tooltip="true" label="序列号" />
        <el-table-column prop="orderQty" label="订单数量" />
        <el-table-column prop="allocatedQty" label="分配数量" />
        <el-table-column prop="pickedQty" label="拣货数量" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="propC4" label="出库方式" />
        <el-table-column prop="propC5" label="目标点位" />

        <el-table-column prop="createBy" label="创建人" />
        <el-table-column prop="createTime" width="150px" label="创建时间" />
        <el-table-column label="操作" width="150px" fixed="right" align="center">

          <template slot-scope="scope">
            <el-button
              size="mini"
              :disabled="scope.row.status!='OPEN'"
              type="primary"
              icon="el-icon-edit"
              @click="editPick(scope.row)"
            />
            <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" />

      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="580px"
      >
        <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="物料" prop="item">
            <el-select
              v-model="form.item"
              filterable
              placeholder="请选择物料"
              value-key="id"
              style="width: 405px;"
              @focus="getItem"
            >
              <el-option
                v-for="item in items"
                :key="item.id"
                :label="item.code"
                :value="item"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="订单数量">
            <el-input v-model="form.orderQty" style="width: 135px;" />
          </el-form-item>
          <el-form-item label="中标批次名称">
            <el-input v-model="form.propC1" style="width: 135px;" />
          </el-form-item>
          <el-form-item label="成品物料编码">
            <el-input v-model="form.propC2" style="width: 135px;" />
          </el-form-item>
          <el-form-item label="序列号">
            <el-input v-model="form.propC3" style="width: 135px;" />
          </el-form-item>
          <el-form-item label="业务类型">
            <el-select
                v-model="form.propC4"
                size="small"
                placeholder="业务类型"
                class="filter-item"
                style="width: 135px"
            >
              <el-option
                  v-for="item in dict.bill_type"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="目标点位">
            <el-input v-model="form.propC5" style="width: 135px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->

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

<script>
import crudPickDetail from '@/api/pickDetail'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getItems, getItemsList } from '@/api/item'
import PickTask from '@/views/business-data/pickDetail/pickTicketTask.vue'
import DateRangePicker from '@/components/DateRangePicker/index.vue'

const defaultForm = {
  id: null,
  pick: null,
  item: null,
  lineNo: null,
  po: null,
  status: 'OPEN',
  orderQty: 0,
  allocatedQty: 0,
  pickedQty: 0,
  shippedQty: 0,
  weight: 0,
  volume: 0,
  remark: null,
  propC1: null,
  propC2: null,
  propC3: null,
  propC4: null,
  propC5: null,
  propC6: null,
  propD1: null,
  propD2: null,
  deptId: null,
  sourceName: null,
  sourceId: null,
  createBy: null,
  updateBy: null,
  createTime: null,
  updateTime: null
}
export default {
  name: 'PickDetail',
  components: { DateRangePicker, PickTask, pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['pick_status', 'bill_type'],
  cruds() {
    return CRUD({
      title: '出库明细',
      url: 'api/pickDetail',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudPickDetail },
      optShow: {
        add: true,
        edit: false,
        del: false,
        reset: false,
        download: true
      }
    })
  },
  data() {
    return {
      show_fp: true,
      show_jh: true,
      show_cancelfp: true,
      radio3: '全部',
      items: [],
      permission: {
        add: ['admin', 'pickDetail:add'],
        edit: ['admin', 'pickDetail:edit'],
        del: ['admin', 'pickDetail:del']
      },
      tableKey: 'api/pickDetail',
      rules: {
        item: [
          { required: true, message: '物料必填', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'itemCode', display_name: '物料编码' },
        { key: 'itemName', display_name: '物料名称' },
        { key: 'po', display_name: '任务号' },
        { key: 'status', display_name: '状态' }
      ],
      itemListData: []
    }
  },
  mounted() {
    // 初始化数据
    this.getItem()
    this.itemDataGet()
  },
  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
        })
      })
    },

    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 = true
        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
      }
    },
    allocate(datas) {
      this.$confirm(`选中的${datas.length}条数据分配确认?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = []
        for (let i = 0; i < datas.length; i++) {
          ids.push(datas[i].id)
        }
        console.log('ids:' + ids)
        crudPickDetail.allocate(ids).then(res => {
          this.crud.notify('分配成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
          this.crud.toQuery()
        }).catch(() => {

        })
      }).catch(() => {
        this.$refs.table.clearSelection()
        this.crud.notify('取消成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
      })
    },
    cancelAllocate(datas) {
      this.$confirm(`选中的${datas.length}条数据取消分配确认?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = []
        for (let i = 0; i < datas.length; i++) {
          ids.push(datas[i].id)
        }
        crudPickDetail.cancelAllocate(ids).then(res => {
          this.crud.notify('取消分配成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
          this.crud.toQuery()
        }).catch(() => {

        })
      }).catch(() => {
        this.$refs.table.clearSelection()
        this.crud.notify('取消成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
      })
    },
    getPickTask(id) {
      this.$refs.pickTask.dialog = true
      this.$refs.pickTask.queryPickTask(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 = 'ALLOCATE'
        this.crud.toQuery()
      } else if (lab === '拣货完成') {
        this.query.status = 'PICK_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
      }
    },
    // 物料数据
    itemDataGet() {
      getItemsList().then(res => {
        this.itemListData = res
      }).catch(e => {
        this.$message({
          showClose: true,
          message: '物料加载失败',
          type: 'error'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
