<template>
    <el-config-provider :locale="localLanguage">
        <div  style="width: calc(100vw - 300px)">
          <div style="border-radius: 5px; border: #2c3e5033 solid 1px; padding: 10px; calc(100vw - 300px)"><!-- 搜索-->
            <el-row style="width: 100%">
              <el-form :model="searchParams" label-width="120" label-position="left" >
                <el-form-item style="width: 600px" label="查询关键字：">
                  <el-input placeholder="输入 料箱号/库位/任务号 查询..." v-model="searchParams.searchStr" clearable></el-input>
                </el-form-item>
                <el-form-item style="width: 600px" label="任务状态：">
                  <el-select  style="width: 600px" v-model="searchParams.orderStatus" multiple placeholder="请选择需要查询的任务状态">
                    <el-option v-for="item in orderInEnum" :key="item.value" :label="item.label" :value="item.value"/>
                  </el-select>
                </el-form-item>
              </el-form>
            </el-row>
            <el-row>
              <el-button type="primary" @click="queryGoodsOrderIn">查询入库单</el-button>
              <el-button type="success" @click="showBindingGoods = true">下发入库任务</el-button>
            </el-row>
          </div>
          <div> <!-- 表格-->
            <el-row style="width: calc(100vw - 300px)">
              <h5>入库单</h5>
              <el-table :data="goodsOrderInList" border stripe max-height="calc(100vh - 550px)">
                <!-- <el-table-column fixed prop="rowId" label="识别号" width="130px" align="center" show-overflow-tooltip/>
                <el-table-column fixed prop="guid" label="GUID" width="200px" align="center" show-overflow-tooltip/>
                <el-table-column prop="inType" label="入库类型" width="100px" align="center" show-overflow-tooltip/> -->
                <el-table-column fixed type="index" label="序号" width="100px" align="center" show-overflow-tooltip/>
                <el-table-column prop="wareHouse" label="库别" width="150px" align="center" show-overflow-tooltip/>
                <el-table-column prop="batchNo" label="序列号" width="150px" align="center" show-overflow-tooltip/>
                <!-- <el-table-column prop="model" label="机型" width="100px" align="center" show-overflow-tooltip/> -->
                <el-table-column prop="vehicleNo" label="载具号" width="150px" align="center" show-overflow-tooltip/>
                <el-table-column prop="goodsId" label="物料号" width="150px" align="center" show-overflow-tooltip/>
                <el-table-column prop="goodsNum" label="物料数量" width="150px" align="center" show-overflow-tooltip/>
                <el-table-column prop="orderStatus" label="入库单状态" width="150px" align="center">
                  <template #default="scope">
                    <el-tag class="ml-2" :type=formatterOrderInEnum(scope.row.orderStatus).type>
                      {{formatterOrderInEnum(scope.row.orderStatus).label }}</el-tag>
                  </template>
                </el-table-column>
                <el-table-column prop="createPerson" label="创建人" width="100px" align="center" show-overflow-tooltip/>
                <el-table-column prop="createTime" label="创建时间" :formatter="formatCellValueTime" width="180px"
                                 align="center" show-overflow-tooltip/>
                <!-- <el-table-column prop="updateTime" label="更新时间" :formatter="formatCellValueTime" width="180px" align="center" show-overflow-tooltip/> -->
                <el-table-column prop="remark" label="备注" show-overflow-tooltip align="center" min-width="120px"/>
                <el-table-column fixed="right" label="操作" align="center" width="150">
                  <template #default="scope">
                    <el-button-group class="ml-4">
                      <el-tooltip content="更新数量" placement="top" effect="light">
                        <el-button type="success" size="small" @click="updateGoodsNum(scope.row)">
                          <el-icon><Edit/></el-icon>
                        </el-button>
                      </el-tooltip>
                      <el-tooltip content="删除" placement="top" effect="light">
                        <el-button type="danger" size="small" @click="deleteOrder(scope.row)">
                          <el-icon><Delete/></el-icon>
                        </el-button>
                      </el-tooltip>
                    </el-button-group>
                  </template>
                </el-table-column>
              </el-table>
            <!-- 对话框部分 -->
            <el-dialog v-model="dialogVisible" title="更新数量" width="40%" @close="resetForm">
              <el-form ref="updateFormRef" :model="updateFormEntity" :rules="rules" status-icon>
                <!-- <el-form-item label="识别号" prop="rowId">
                  <el-input v-model="updateFormEntity.rowId" readonly />
                </el-form-item> -->
                <el-form-item label="实际数量" prop="realNum">
                  <el-input-number v-model.number="updateFormEntity.realNum" controls-position="right" :min="0" clearable />
                </el-form-item>
                <el-form-item label="日期" prop="createTime">
                  <el-input v-model="updateFormEntity.createTime" clearable/>
                </el-form-item>
                <!-- 其他表单项 -->
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submitUpdate">确认</el-button>
              </span>
            </el-dialog>
            </el-row>
          </div>
        </div>
<!--      绑定物料框-->
      <BindingGoods v-model="showBindingGoods"></BindingGoods>
    </el-config-provider>
</template>


<script>
import { Delete, Close, Edit } from "@element-plus/icons-vue";
import apiOrderIn from '@/api/order.in';
import {ElMessage, ElMessageBox} from 'element-plus'
import stock from '@/layout/stock.vue'
import {formatterOrderInEnum, orderInEnum} from "@/enum/order.in.enum";
import {formatCellValueTime} from "@/utils/formatter";
import zhCn from 'element-plus/dist/locale/zh-cn.mjs';
import en from 'element-plus/dist/locale/en.mjs';
import {ref} from "vue";
import BindingGoods from "@/components/BindingGoods.vue";
const language = ref('zh-cn');

export default {
    name: 'goodsIn',
  computed: {
    orderInEnum() {
      return orderInEnum
    },
    localLanguage: () => (language.value === 'zh-cn' ? zhCn : en)
  },
    components: {BindingGoods, Delete, Close, Edit },
    data() {
        return {
          // 查询条件
          searchParams: {
            searchStr: '',
            orderStatus: [0,1,2]
          },
          // 展示绑定物料的弹窗
          showBindingGoods: false,
          // 绑定物料数据
          dialogVisible: false,
          updateFormEntity: {
          rowId: '',
          realNum: 0,
          createTime: ''
          // 其他字段初始化
          },
          rules: {
          // 表单验证规则
          },
          tableData: [], // 你需要根据实际数据填充
          // 入库任务单表格
          goodsOrderInList: []

        }
    },
    mounted() {},
    methods: {
      formatCellValueTime,
      formatterOrderInEnum,
      // 查询入库单
      queryGoodsOrderIn() {
        this.goodsOrderInList = [];
        apiOrderIn.getOrderInList(this.searchParams).then(res => {
          const responseData = res.data
          if (responseData.code === 0) {
            // 正确请求
            ElMessage({
              message: '查询成功',
              type: 'success',
            });
            this.goodsOrderInList = responseData['returnData'];
          } else {
            // 服务报错
            ElMessageBox.alert(`服务器返回失败：${responseData.msg}`, '查询失败', {
              type: 'warning',
              confirmButtonText: '确定',
              showClose: false
            })
          }

        }).catch(err => {
          // 通讯报错
          ElMessageBox.alert(`请求服务器失败：：${err}`, '任务添加失败', {
            type: 'warning',
            confirmButtonText: '确定'
          })
        })
      },
      // 删除入库单
      deleteOrder(row) {
        ElMessageBox.confirm('确定执行删除吗?','提示')
          .then(() => {
              apiOrderIn.deleteOrderIn(row.rowId).then(res => {
                const responseData = res.data;
                if (responseData.code === 0) {
                  // 正确请求
                  ElMessage({
                    message: '删除成功',
                    type: 'success',
                  });
                  this.queryGoodsOrderIn();
                } else {
                  // 服务报错
                  ElMessageBox.alert(`服务器返回失败：${responseData.message}`, '删除失败', {
                   type: 'warning',
                   confirmButtonText: '确定',
                   showClose: false
                  })
                }
              }).catch(err => {
                // 通讯报错
                ElMessageBox.alert(`请求服务器失败：：${err}`, '任务添加失败', {
                  type: 'warning',
                  confirmButtonText: '确定'
                })
              })
          })
          .catch(() => {
          })

      },
      // 更新入库单
      updateGoodsNum(row) {
        this.dialogVisible = true;
        this.updateFormEntity = row;
      },
      test() {
        console.log({rowId:this.updateFormEntity.rowId,realNum:this.updateFormEntity.realNum,orderStatus:this.updateFormEntity.orderStatus});
    },
      // 提交更新入库单
      submitUpdate(){
        if(this.updateFormEntity.realNum === 0){
          ElMessage({
            message: '数量不能为0',
            type: 'error',
          });
          return;
        }
        if(this.updateFormEntity.orderStatus === 1){
          ElMessage({
            message: '正在入库的订单不能更新数量',
            type: 'error',
          });
          return;
        }
        apiOrderIn.updateForNum({rowId:this.updateFormEntity.rowId,realNum:this.updateFormEntity.realNum,createTime:this.updateFormEntity.createTime}).then(res => {
          const responseData = res.data
          if (responseData.code === 0) {
            // 正确请求
            ElMessage({
              message: '更新成功',
              type: 'success',
            });
            this.queryGoodsOrderIn();
          }else {
            // 服务报错
            ElMessageBox.alert(`服务器返回失败：${responseData.message}`, '更新失败', {
              type: 'warning',
              confirmButtonText: '确定',
              showClose: false
            })
          }
        }).catch(err => {
          // 通讯报错
          ElMessageBox.alert(`请求服务器失败：：${err}`, '任务添加失败', {
            type: 'warning',
            confirmButtonText: '确定'
          })
        })
      },
      // 解绑入库单
      unBindingOrder(row) {
        ElMessageBox.confirm('你确定要解绑该吗','提示')
    .then(() => {
      apiOrderIn.unbindingVehicle(row.rowId).then(res => {
          const responseData = res.data
          if (responseData.code === 0) {
            // 正确请求
            ElMessage({
              message: '解绑成功',
              type: 'success',
            });
            this.queryGoodsOrderIn();
          } else {
            // 服务报错
            ElMessageBox.alert(`服务器返回失败：${responseData.message}`, '解绑失败', {
              type: 'warning',
              confirmButtonText: '确定',
              showClose: false
            })
          }
        }).catch(err => {
          // 通讯报错
          ElMessageBox.alert(`请求服务器失败：：${err}`, '任务添加失败', {
            type: 'warning',
            confirmButtonText: '确定'
          })
        })
    })
    .catch(() => {
    })
    }   
  }
}

</script>

<style scoped>

</style>