<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.itemName" 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" @change="clickChange" size="small">
          <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" :tableKey="this.$options.name">
        <el-button
          slot="right"
          v-permission="permission.cxjl"
          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="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="show_fp"
          @click="btnAllocate(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" :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="shArea.name" label="指示纳所" width="150px"/>
        <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="item.extendD3" label="收容数" width="60px" align="center"/>
        <el-table-column prop="point.code" label="制造库位" width="100px"/>
        <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="createBy" label="创建人"/>
        <el-table-column prop="createTime" width="150px" label="创建时间">
          <template slot-scope="scope">
            {{getFormatDate(scope.row.createTime)}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px" 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 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"  :rules=" { required: true, message: '制造库位不能为空', trigger: 'blur' }">
               <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.gw_code"  />
          </el-form-item>
          <el-form-item label="工厂名">
            <el-input  v-model="cxjlFrom.gw_name" />
          </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="btnCxjl">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->

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

<script>
import crudPickDetail,{cxjl,allocatePickDetail} 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 PickTask from "@/views/business-pick/pickDetail/pickTicketTask.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";

const defaultForm = {
  id: null,
  remark: null
}
export default {
  name: 'PickDetail',
  components: {
    BomAccountPointSelect,
    ItemSelect, DateRangePicker, PickTask, pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['pick_status'],
  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: [],
      cxjlFromFlag: false,
      cxjlFrom: { bom_account_id: null,order_qty:0, item_code:null, item_name:null, zzkw_id:null, zzkw_code:null, gw_code:null, gw_name:null, status:null, remark:null,srs:0  },
      permission: {
        del: ['admin', 'pickDetail:del']
      },
      rules: {
        item: [
          {required: true, message: '物料必填', trigger: 'blur'}
        ]
      },
      itemListData: [],
      //品番
      itemOptions: [],
      zzkwPointOptions: [],
      loading_add:false
    }

  },
  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
        })
      })
    },
    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 = 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;
      }


    },
    btnAllocate(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)
        allocatePickDetail(ids).then(res => {
          if (res.status == 200) {
            this.crud.notify(res.message, CRUD.NOTIFICATION_TYPE.SUCCESS)
          }
          this.crud.toQuery()
        }).catch(() => {
        })
      })
    },
    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 = '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;
    },

    btnCxjl() {
      cxjl(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>
