
<template>
  <view class="mainBox">
    <uni-nav-bar
      fixed="true"
      statusBar="true"
      left-icon="back"
      title="工序报工"
      @clickLeft="back"
    >
    </uni-nav-bar>
    <view class="report-view">
    <view class="card-list">
        <view class="list-item" v-for="(item, index) in msgList" :key='index'>
            <view class="label">{{item.label}}</view>
            <view v-if="item.type === 'checked'">
                <u-checkbox-group @change="(e) => formData[item.key] = e[0] || ''">
                    <u-checkbox 
                        icon-size='30'
                        size='30'
                        name="1"
                  ></u-checkbox>
                </u-checkbox-group>
            </view>
            <view v-else-if="item.type === 'inp'" >
                <input class="uni-input"  v-model="formData[item.key]" placeholder="请输入" />
            </view>
            <view class="text-primary" v-else-if="item.type === 'trash'" @click='handleClick(item)'>{{formData[item.key] || '点击更新'}}</view>
            <view class="text-primary" v-else-if="item.type=== 'msg'" @click='handleMsg(item)'>{{processesInfo[item.key] || '查看'}}</view>
            <view v-else-if="item.type=== 'show'" >{{formData[item.key]}}</view>
            <view v-else :class="{'warning': item.isWarn && !Number.isNaN(+processesInfo[item.key]) && (processesInfo[item.key] < 0)}">{{processesInfo[item.key]}}</view>
        </view>
    </view>
    </view>
    <view class="footer">
        <button type="primary" @click="handleReport">报工</button>
    </view>
    <editDialog ref='editDialogRef'/>
    
    <msgDialog ref='msgDialogRef' :processesInfo='processesInfo'/>
  </view>
</template>
    
<script>
import { report } from "@/api/report/index"
import editDialog from "./components/editDialog.vue"
import msgDialog from "./components/msgDialog.vue"
import {add} from "@/utils/math"

export default {
    components:{editDialog, msgDialog},
    data(){
      return {
        msgList:[
          { label: '序号', key: 'vornr'},
          { label: '工序名称', key: 'processes'},
          { label: '已合格', key: 'lmnga'},
          { label: '已废品', key: 'xmnga', type: 'msg'},
          { label: '在制品', key: 'workProgress', isWarn: true},
          { label: '无成本报工', key: 'notGs', type:'checked'},
          { label: '本次合格', key: 'lmnga', type: 'inp'},
          { label: '设备号', key: 'equipId', type: 'inp'},
          { label: '报工批次', key: 'reciverBatch', type: 'inp'},
          { label: '混合料密度', key: 'matnrDensity', type: 'inp'},
          { label: '混炼TEP', key: 'workTemp', type: 'inp'},
          { label: '操作工', key: 'actionName', type: 'inp'},
          { label: '废品', key: 'xmnga', type: 'trash'},
          { label: '检查工', key: 'checkName', type: 'inp'},
          { label: '设备次数', key: 'equipNum', type: 'inp'},
          { label: '工艺曲线', key: 'processCurve', type: 'inp'},
          { label: '返修', key: 'upmnga', type: 'trash'},
          { label: '让步', key: 'cmnga', type: 'trash'},
          { label: '改型', key: 'umnga', type: 'trash'},
          { label: '返回料', key: 'remnga', type: 'inp'},
          { label: '反工料', key: 'rmnga', type: 'inp'},
          { label: '备注', key: 'workDesc', type: 'inp'},
          { label: '已返修', key: 'upmnga', type: 'msg'},
          { label: '已让步', key: 'cmnga', type: 'msg'},
          { label: '已改型', key: 'umnga', type: 'msg'},
          { label: '加工要求', key: 'ltxa1Desc'},
          // { label: '工序描述', key: 'ltxa1', type: 'inp'},
          { label: '工序控制码', key: 'steus'},
          { label: '废品JSON', key: 'xmngaJson', type: 'show'},
          { label: '让步JOSN', key: 'cmngaJson', type: 'show'},
          { label: '返修JOSN', key: 'upmngaJson', type: 'show'},
          { label: '改型JOSN', key: 'umngaJson', type: 'show'},
          { label: '模孔数', key: 'dieHole', type: 'inp'},
          // { label: '批量收货数', key: 'shsl', type: 'inp'},
          { label: '压力', key: 'yl', type: 'inp'},
          { label: '料浆粘度', key: 'ljnd', type: 'inp'},
        ],
        processesInfo:{},
        formData:{
            workType:'1',
            omnga:0,
            vornrId:"",
            notGs:"",
            lmnga:"",
            equipId:"",
            reciverBatch:"",
            matnrDensity:"",
            workTemp:"",
            actionName:"",
            xmnga:"",
            checkName:"",
            equipNum:"",
            processCurve:"",
            upmnga:"",
            cmnga:"",
            umnga:"",
            remnga:"",
            rmnga:"",
            workDesc:"",


            ltxa1Desc:"",
            ltxa1:"",
            steus:"",
            xmngaJson:"",
            cmngaJson:"",
            umngaJson:"",
            upmngaJson:"",
            dieHole:"",
            shsl:"",
            yl:"",
            ljnd:"",

        }

      }
    },
    onLoad({data}){
        data = JSON.parse(data)
        this.processesInfo = data
        this.formData.vornrId = data.id
        this.formData.vornr = data.vornr
        this.formData.ltxa1 = data.processesDesc
        this.formData.aufnr = data.aufnr
    },
    methods:{
     handleClick(item){
           this.$refs.editDialogRef.open(item.key, this.formData, (data) => {
                this.formData.xmngaJson = data.xmngaList.length ? JSON.stringify(data.xmngaList.map(item => ({
                    first: item.first,
                    last: item.last.trim()
                }))) : ''
                this.formData.cmngaJson = data.cmngaList.length ? JSON.stringify(data.cmngaList.map(item => ({
                    first: item.first,
                    last: item.last.trim()
                }))) : ''
                this.formData.umngaJson = data.umngaList.length ? JSON.stringify(data.umngaList.map(item => ({
                    first: item.first,
                    last: item.last.trim()
                }))) : ''
                this.formData.upmngaJson = data.upmngaList.length ? JSON.stringify(data.upmngaList.map(item => ({
                    first: item.first,
                    last: item.last.trim()
                }))) : ''

                this.formData.xmnga = data.xmngaList.reduce((sum, cur) => sum + Number(cur.first), 0) || ''
                this.formData.cmnga = data.cmngaList.reduce((sum, cur) => sum + Number(cur.first), 0)|| ''
                this.formData.umnga = data.umngaList.reduce((sum, cur) => sum + Number(cur.first), 0)|| ''
                this.formData.upmnga = data.upmngaList.reduce((sum, cur) => sum + Number(cur.first), 0)|| ''

           })
      },
      handleMsg(item){
           this.$refs.msgDialogRef.open(item.key, this.processesInfo)
      },
        handleReport(){
            if(this.formData.lmnga === '0' || this.formData.lmnga === 0){
               return uni.showToast({
                icon:'none',
                title:'本次合格不能为0'
               })
            }
          if(this.formData.xmnga === '0' || this.formData.xmnga === 0){
               return uni.showToast({
                icon:'none',
                title:'废品不能为0'
               })
            }
            if(this.formData.lmnga + this.formData.xmnga + this.formData.upmnga + this.formData.cmnga + this.formData.umnga <= 0){
                return uni.showToast({
                    icon:'none',
                    title: '不满足报工条件，请完善数据！'
                })
            }
            const _this = this
           uni.showModal({
                title: `确认报工？`,
                content: '',
                confirmText: '确认',
                success: function (res) {
                if (res.confirm) {
                    
                    const map = {
                      KG:1,
                      G: 0.001,
                      TON: 1000
                    }
                    let sum =add(add(_this.formData.lmnga || 0 ,  _this.formData.cmnga || 0), _this.formData.umnga || 0)
                    if(['KG', 'G', 'TON'].includes(_this.processesInfo.meins)){
                        _this.formData.totalNtgew = map[_this.processesInfo.meins] * sum
                    }else{
                        _this.formData.totalNtgew = map[_this.processesInfo.gewei] * sum
                    }


                    if(_this.formData.upmnga && !_this.formData.lmnga && !_this.formData.xmnga  && !_this.formData.cmnga && !_this.formData.umnga){
                    _this.formData.repair = 1
                    }else{
                    _this.formData.repair = 0

                    }
                    
                    const userInfo = uni.getStorageSync('userInfo')

                    if(userInfo?.id){
                      _this.formData.crtUser = userInfo.id
                      _this.formData.crtName = userInfo.name
                    }
                    // 默认值/、
                    _this.formData.xmngaJson = _this.formData.xmngaJson || JSON.stringify([{first:'', last:''}])
                    _this.formData.cmngaJson = _this.formData.cmngaJson || JSON.stringify([{first:'', last:''}])
                    _this.formData.umngaJson = _this.formData.umngaJson || JSON.stringify([{first:'', last:''}])
                    _this.formData.upmngaJson = _this.formData.upmngaJson || JSON.stringify([{first:'', last:''}])

                    _this.formData.xmnga = _this.formData.xmnga || 0
                    _this.formData.upmnga = _this.formData.upmnga || 0
                    _this.formData.cmnga = _this.formData.cmnga || 0
                    _this.formData.umnga = _this.formData.umnga || 0
                    _this.formData.lmnga = _this.formData.lmnga || 0


                    report(_this.formData).then(res => {
                      if(res === 0){
                        uni.showModal({
                          title: `当前为结账期间，禁止报工！`,
                          content: '',
                          confirmText: '确认',
                          showCancel: false
                        })
                        return
                      }
                      uni.showToast({
                        title:'报工成功！'
                      })
                      setTimeout(() => {
                        uni.navigateBack({
                          delta:1
                        })
                      }, 1500);
                    }).catch(err => {
                      uni.showToast({
                        title: err,
                        icon: 'none'
                      })
                    })
                }
                }
            })

        },
    },
};
</script>
    
    <style lang='scss' scoped>
  .mainBox{
    background: $page-bg;
    height: 100vh;
    overflow: auto;
  }
    .report-view{
       padding: 16rpx;
          font-size: $uni-font-size-lg;

    }
    .warning{
      color: red
    }
    .card-list{
        padding: 30rpx;
        background: #fff;
        border-radius: 16rpx;
       /deep/.list-item{
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 16rpx 0;
          border-bottom: 1rpx solid #ccc;
          font-size: $uni-font-size-lg; 

          input{
          font-size: $uni-font-size-lg;
          text-align: right;
          }

          /deep/.uni-data-checklist .checkbox__inner{
            width: 40rpx;
            height: 40rpx;
          }
       }
    }
.footer{
    height: 100rpx;

    button{
        font-size: $uni-font-size-lg;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
    }
}
</style>