<template>
  <view>
    <u-popup v-model="isShow" mode="center" :border-radius="10" :closeable="true" @close="fnCancel" width="100%" height="100%">
      <u-row gutter="12">
		  <u-col span="12">
		    <CustomNavbar />
		  </u-col>
        <u-col span="12">
          <view :style="{ textAlign: 'center', height: '60px', lineHeight: '60px' }">
            <text :style="{ fontSize: '40rpx', fontWeight: 500, color: '#1D2129' }">报工作业</text>
          </view>
        </u-col>
		<u-col span="12">
        <u-form :model="dataItem" ref="dataItem" :errorType="['toast']" label-position="left" label-width="250" label-align="center">
          <u-form-item label="合格数量" prop="qualifiedNum" label-width="250" required>
            <u-input v-model="dataItem.qualifiedNum" type="text" :border="true" placeholder="请输入合格数量" />
          </u-form-item>
          <u-form-item label="次品数量" prop="defectiveNum" label-width="250" required>
            <u-input v-model="dataItem.defectiveNum" type="text" :border="true"  placeholder="请输入次品数量"/>
          </u-form-item>
		  <u-form-item label="二等品数量" prop="secondNum" label-width="250" required>
		    <u-input v-model="dataItem.secondNum" type="text" :border="true" placeholder="请输二等品数量" />
		  </u-form-item>
          <u-form-item label="剩余半成品" prop="semiFinishedNum" label-width="250" >
            <u-input v-model="dataItem.semiFinishedNum" type="text" :border="true" placeholder="请输剩余半成品数量" />
          </u-form-item>
          <u-form-item label="班组" prop="groupName" label-width="250" required>
            <u-input v-model="dataItem.groupName" type="text" :border="true" placeholder="请输班组" />
          </u-form-item>
          <u-form-item label="报工人员" prop="reportMember" label-width="250">
           <jnpf-select v-model="reportMember" placeholder="请选择报工人员" :options="reportMemberList" :props='props'></jnpf-select>
          </u-form-item>
        </u-form>
		</u-col>
        <view class="buttom-actions">
          <u-button class="buttom-btn" @click="jnpf.goBack">取消</u-button>
          <u-button class="buttom-btn" type="primary" @click="fnOk">保存</u-button>
          <!-- <u-button class="buttom-btn" type="warning" @click="submit">保存草稿</u-button> -->
        </view>
      </u-row>
    </u-popup>
    <u-top-tips ref="uTips"></u-top-tips>
  </view>
</template>

<script>
  import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js'
  import { mapGetters } from 'vuex'
  import { toProd } from '@/api/yys/materialProcurementOrder.js'
  import { userList, materialInformationList, sure } from '@/api/yys/dayWork.js'
  import { getDataInterfaceRes } from '@/api/common'
  import dayjs from 'dayjs'
  export default {
    components: {},
    mixins: [],
    data() {
      return {
		  props: {
		  					label: "realName",
		  					value: "id",
		  				},
        isShow: false,
        productionNumber: undefined,
        dataItem: {},
        productionTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        isShowPickerA: false,
        isShowReportMember: false,
        isShowMaterialCode: false,
        pickerParams: {
          year: true,
          month: true,
          day: true,
          hour: true,
          minute: true,
          second: true,
        },
        reportMemberList: [],
		  reportMember: '',
        materialList: [],
        prodReportItemO: {
          reportMember: undefined,
          reportMemberIndex: [],
          planNum: undefined,
          reportMemberShow: undefined,
          compensatoryHour: undefined,
        },
        packageReportItemO: {
          reportMemberShow: undefined,
          reportMemberIndex: [],
          planNum: undefined,
          acquisitionNum: undefined,
          reportMember: undefined,
          compensatoryHour: undefined,
        },
        requisitionReportItemO: {
          materialCode: undefined,
          materialCodeIndex: [],
          materialName: undefined,
          acquisitionNum: undefined,
          duty: undefined,
          classBalance: undefined,
        },
      }
    },
    watch: {},
    computed: {},
    onLoad(params) {},
    onUnload() {},
    methods: {
      init(dataItem) {
        dataItem.prodReportList = []
        dataItem.packageReportList = [] // temp.packageReportList;
        //dataItem.requisitionReportList =[];//  temp.requisitionReportList;
        this.dataItem = dataItem
        if (this.dataItem.defectiveNum) {
          this.dataItem.defectiveNum = ''
        }
        if (this.dataItem.qualifiedNum) {
          this.dataItem.qualifiedNum = ''
        }
		if (this.dataItem.secondNum) {
		  this.dataItem.secondNum = ''
		}
        this.fnReportMemberList()
        this.fnMaterialInformationList()

        this.dataItem.acquisitionNum = this.dataItem.isConfirmNums
        this.productionNumber = undefined
        this.productionTime = dayjs().format('YYYY-MM-DD HH:mm:ss')
        this.isShow = true
        console.log(this.dataItem)
      },
      fnCancel() {
        this.dataItem = {}
        this.isShow = false
		this.reportMember = ''
      },
      fnOk() {
        if (!this.dataItem.defectiveNum || !this.dataItem.qualifiedNum||!this.dataItem.semiFinishedNum) {
          uni.showToast({
            title: '请输入合格数量和次品数量以及剩余半成品数量',
            icon: 'none',
          })
          return
        }
        if (!this.dataItem.groupName) {
          uni.showToast({
            title: '请输入班组',
            icon: 'none',
          })
          return
        }
		this.dataItem.reportMember = this.reportMember
        let _params = {
          ...this.dataItem,
        }
        console.log(_params)
        sure(_params)
          .then(res => {
            console.log("res"+res)
              uni.showModal({
                title: '报工确认',
                content: res.msg,
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                  }
                }
              });
            this.$emit('evtRefresh')
            this.fnCancel()
          })
          .catch(err => {
            uni.showModal({
              title: '报工错误',
              content: err || "发生错误", // 确保有默认消息
              showCancel: false,
              success: function (res) {
                console.log("Modal success callback executed in catch."); // 添加日志
                if (res.confirm) {
                  // 这里可以添加一些逻辑
                }
              },
            });
            this.$emit('evtRefresh')
            this.fnCancel()
          })

      },
      fnShowPickerA() {
        this.isShowPickerA = true
      },
      fnProductionTime(timeObj) {
        this.productionTime = timeObj.year + '-' + timeObj.month + '-' + timeObj.day + ' ' + timeObj.hour + ':' + timeObj.minute + ':' + timeObj.second
      },
      fnReportMemberList() {
        userList()
          .then(res => {
            if (res.code == 200) {
              this.reportMemberList = res.data
            }
          })
          .catch(err => {
            console.log(err)
          })
      },
      fnProdReportItemAdd() {
        this.prodReportItemO.planNum = this.dataItem.planNumber
        this.dataItem.prodReportList.push({
          ...this.prodReportItemO,
        })
      },
      fnProdReportItemDel(index) {
        this.dataItem.prodReportList.splice(index, 1)
      },
      fnProReportMemberSltd(row, index) {
        this.dataItem.prodReportList[index]['reportMember'] = row[0]['value']
        this.fnProReportMemberFind(this.dataItem.prodReportList[index]['reportMember'], index)
      },
      fnProReportMemberFind(reportMember, index) {
        let _list = [...this.reportMemberList]
        let _reportMember = _list.find(o1 => {
          return o1.id == reportMember
        })
        let _reportMemberIndex = _list.indexOf(_reportMember)
        this.dataItem.prodReportList[index]['reportMemberIndex'] = [_reportMemberIndex]
        this.dataItem.prodReportList[index]['reportMemberShow'] = _reportMember.realName
      },
      fnPackageReportItemAdd() {
        this.packageReportItemO.planNum = this.dataItem.planNumber
        this.dataItem.packageReportList.push({
          ...this.packageReportItemO,
        })
      },
      fnPackageReportItemDel(index) {
        this.dataItem.packageReportList.splice(index, 1)
      },
      fnPackageReportMemberSltd(row, index) {
        this.dataItem.packageReportList[index]['reportMember'] = row[0]['value']
        this.fnPackageReportMemberFind(this.dataItem.packageReportList[index]['reportMember'], index)
      },
      fnPackageReportMemberFind(reportMember, index) {
        let _list = [...this.reportMemberList]
        let _reportMember = _list.find(o1 => {
          return o1.id == reportMember
        })
        let _reportMemberIndex = _list.indexOf(_reportMember)
        this.dataItem.packageReportList[index]['reportMemberIndex'] = [_reportMemberIndex]
        this.dataItem.packageReportList[index]['reportMemberShow'] = _reportMember.realName
      },
      fnRequisitionReportItemAdd() {
        this.dataItem.requisitionReportList.push({
          ...this.requisitionReportItemO,
        })
      },
      fnRequisitionReportItemDel(index) {
        this.dataItem.requisitionReportList.splice(index, 1)
      },
      fnMaterialInformationList() {
        const query = {
          paramList: [
            {
              dataType: 'varchar',
              defaultValue: this.dataItem.id,
              field: 'id',
              fieldName: '',
              id: '111',
              isSubTable: false,
              relationField: null,
              required: 0,
              sourceType: 0,
            },
          ],
        }
        getDataInterfaceRes('606211842770993989', query)
          .then(res => {
            if (res.code == 200) {
              this.materialList = res.data
              var list = JSON.parse(JSON.stringify(res.data))
              list.forEach(it => {
                it.classBalance = undefined
                it.duty = undefined
              })
              this.dataItem.requisitionReportList = list
              uni.hideLoading()
            }
          })
          .catch(err => {
            uni.hideLoading()
          })

        //    materialInformationList()
        //      .then((res) => {
        //        if (res.code == 200) {
        //          console.log(res);
        //          this.materialList = res.data;
        // //var list = res.data.filter(it=>it.materialCode.includes(''))
        // this.dataItem.requisitionReportList=JSON.parse(JSON.stringify(res.data))
        //          //this.reportMemberList = res.data;
        //        }
        //      })
        //      .catch((err) => {
        //        console.log(err);
        //      });
      },
      fnMaterialCodeSltd(row, index) {
        this.dataItem.requisitionReportList[index]['materialCode'] = row[0]['value']
        this.fnMaterialCodeFind(this.dataItem.requisitionReportList[index]['materialCode'], index)
      },
      fnMaterialCodeFind(materialCode, index) {
        let _list = [...this.materialList]
        let _material = _list.find(o1 => {
          return o1.materialCode == materialCode
        })
        let _materialCodeIndex = _list.indexOf(_material)
        this.dataItem.requisitionReportList[index]['materialCodeIndex'] = [_materialCodeIndex]
        this.dataItem.requisitionReportList[index]['materialName'] = _material.materialName
      },
    },
  }
</script>

<style lang="scss" scoped>
  /deep/.u-th {
    width: 100%;
    height: 32px;
  }

  /deep/.u-td {
    width: 100%;
    height: 32px;
  }
</style>
