<template>
  <el-form
    class="form-no-message"
    ref="formRef"
    :show-message="false"
    label-position="left"
    :model="{
      workReportArea,
      categoryMsg,
      ...workReport
    }"
  >
    <div class="message-box">
      <ul>
        <li> <span class="label">报工次数</span>{{ countMsg.reportNum }}</li>
        <li>
          <span class="label">累计合格品数量</span
          >{{ countMsg.standardTotalNum }}</li
        >
        <li>
          <span class="label">累计合格品重量</span
          >{{ countMsg.standardTotalWeight }}KG</li
        >
        <li>
          <span class="label">累计投料产品数量</span
          >{{ countMsg.feedProductWeight }}</li
        >
      </ul>
      <div class="right">
        <el-form-item
          label="执行人工号"
          prop="executorId"
          required
          label-width="100px"
          ><personSelectRemote
            v-model="workReport.executorId"
            placeholder="请输入"
            @selfChange="
              (val, item) => (workReport.executorJobNum = item.jobNumber)
            "
        /></el-form-item>
        <el-form-item
          label="执行日期"
          required
          prop="executeTime"
          label-width="100px"
          ><el-date-picker
            v-model="workReport.executeTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择"
            :disabled="formData.dateType == 2"
          ></el-date-picker
        ></el-form-item>
      </div>
    </div>
    <el-card>
      <el-descriptions title="报工信息" direction="vertical" :column="7" border>
        <el-descriptions-item label="">
          <span class="label-required after" slot="label">投料数量（PCS）</span>
          <el-form-item
            label=""
            label-width="0"
            class="w100"
            required
            prop="categoryMsg.number"
          >
            <el-input
              class="w100"
              v-model="categoryMsg.number"
              @input="
                (value) =>
                  (categoryMsg.number = value ? parseInt(value) : value)
              "
              clearable
            ></el-input>
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item label="">
          <span class="label-required after" slot="label">投料重量（KG）</span>
          <el-form-item
            label=""
            label-width="0"
            class="w100"
            prop="categoryMsg.totalWeight"
            ><el-input
              readonly
              class="w100"
              :value="
                (categoryMsg.totalWeight =
                  categoryMsg.number === ''
                    ? ''
                    : categoryMsg.number * (infoData.productUnitWeight || 1))
              "
              clearable
            ></el-input>
            <!-- 	:value="
				  (categoryMsg.totalWeight =
					categoryMsg.number * (infoData.productUnitWeight || 1))
				" -->
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item label="舟皿编号/名称" :span="2"
          ><el-input
            :value="`${boatMsg.code}/${boatMsg.name}`"
            @click.native="getCategory('8', 'boatMsg')"
          ></el-input
        ></el-descriptions-item>
        <el-descriptions-item label="舟皿数量" :span="3"
          ><el-input
            placeholder="请输入"
            v-model="boatMsg.extraField.boatMsg"
          ></el-input
        ></el-descriptions-item>
        <el-descriptions-item label="质检项" :span="2">
          <el-input
            v-model="workReport.qualityItem"
            placeholder="请输入"
          ></el-input>
        </el-descriptions-item>
        <el-descriptions-item label="质检标准" :span="3">
          <el-input
            v-model="workReport.qualityStandard"
            placeholder="请输入"
          ></el-input
        ></el-descriptions-item>
        <el-descriptions-item label="">
          <span class="label-required after" slot="label">合格品数量(PCS)</span>
          <el-form-item
            label=""
            required
            label-width="0"
            class="w100"
            prop="productInfo.standardNum"
            ><el-input
              class="w100"
              v-model="workReport.productInfo.standardNum"
              @input="
                (value) =>
                  (workReport.productInfo.standardNum = value
                    ? parseInt(value)
                    : value)
              "
              clearable
            ></el-input> </el-form-item
        ></el-descriptions-item>
        <el-descriptions-item label=""
          ><span class="label-required after" slot="label">合格品重量(KG)</span>
          <el-form-item
            label=""
            label-width="0"
            class="w100"
            prop="productInfo.standardWeight"
            ><el-input
              readonly
              class="w100"
              :value="
                (workReport.productInfo.standardWeight =
                  workReport.productInfo.standardNum === ''
                    ? ''
                    : workReport.productInfo.standardNum *
                      (infoData.productUnitWeight || 1))
              "
              clearable
            ></el-input> </el-form-item
        ></el-descriptions-item>
        <el-descriptions-item label="不合格品数量(PCS)">
          <el-input
            class="w100"
            v-model="workReport.productInfo.noStandardNum"
            @input="
              (value) =>
                (workReport.productInfo.noStandardNum = value
                  ? parseInt(value)
                  : value)
            "
            clearable
          ></el-input>
        </el-descriptions-item>
        <el-descriptions-item label="不合格品重量(KG)"
          ><el-input
            class="w100"
            readonly
            :value="
              (workReport.productInfo.noStandardWeight =
                workReport.productInfo.noStandardNum === ''
                  ? ''
                  : workReport.productInfo.noStandardNum *
                    (infoData.productUnitWeight || 1))
            "
            clearable
          ></el-input
        ></el-descriptions-item>
        <el-descriptions-item label="副产品重量(KG)"
          ><el-input
            class="w100"
            v-model="workReport.productInfo.byProductWeight"
            @input="
              (value) =>
                (workReport.productInfo.byProductWeight = value
                  ? parseInt(value)
                  : value)
            "
            clearable
          ></el-input
        ></el-descriptions-item>
        <el-descriptions-item label="备注">
          <el-input v-model="workReport.remark"></el-input
        ></el-descriptions-item>
      </el-descriptions>
      <el-descriptions
        class="mt-16"
        title="干燥区域信息"
        direction="vertical"
        :column="7"
        border
      >
        <el-descriptions-item label="区域编码">{{
          workReportArea.code
        }}</el-descriptions-item>
        <el-descriptions-item label="区域名称">{{
          workReportArea.name
        }}</el-descriptions-item>
        <el-descriptions-item label="">
          <span slot="label" class="label-required after">干燥时长(分钟)</span>
          <el-form-item prop="workReportArea.extraField.dryTime" required>
            <el-input
              placeholder="请输入"
              v-model="workReportArea.extraField.dryTime"
            ></el-input>
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item label="区域位置">{{
          workReportArea.path
        }}</el-descriptions-item>
        <el-descriptions-item label="操作"
          ><el-link type="primary" @click="getEquip"
            >更改信息</el-link
          ></el-descriptions-item
        >
      </el-descriptions>
    </el-card>
    <workshopDailog ref="workshopRef" :taskId="taskInfo.id" />
    <catogaryDialog ref="catogaryDialogRef" />
  </el-form>
</template>

<script>
  import workshopDailog from '@/components/WorkshopDailog/workshop-dailog';
  import personSelectRemote from '@/components/CommomSelect/person-select-remote';
  import { reportCount } from '@/api/produceOrder';
  import catogaryDialog from '../catogaryDialog.vue';
  import dayjs from 'dayjs';
  export default {
    components: { personSelectRemote, workshopDailog, catogaryDialog },
    props: {
      infoData: {
        type: Object,
        default: () => ({})
      },
      taskInfo: {
        type: Object,
        default: () => ({})
      },
      formData: {
        type: Object,
        default: () => ({})
      },
      firstInfo: {
        type: Object,
        default: () => ({})
      },
      currentInfo: {
        type: Object,
        default: () => ({})
      }
    },
    data () {
      return {
        categoryMsg: {
          batchNo: '',
          number: '',
          totalWeight: '',
          brandNum: '',
          sourceCategoryId: '',
          rootCategoryLevelId: '9',
          name: '',
          code: ''
        },
        workReport: {
          qualityStandard: '',
          qualityItem: '',
          executorId: '',
          executorJobNum: '',
          executeTime: dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          remark: '',
          productInfo: {
            byProductWeight: '',
            standardNum: '',
            standardWeight: '',
            noStandardNum: '',
            noStandardWeight: ''
          }
        },
        workReportArea: {
          areaType: '',
          code: '',
          name: '',
          path: '',
          sourceAreaId: '',
          specification: '',
          extraField: {
            dryTime: ''
          }
        },
        countMsg: {},
        // 舟皿信息
        boatMsg: {
          code: '',
          name: '',
          path: '',
          model: '',
          rootCategoryLevelId: '',
          specification: '',
          extraField: {
            boatMsg: ''
          }
        }
      };
    },
    created () {
      this.workReport.executorId = this.$store.state.user.info?.userId;
      this.workReport.executorJobNum = this.$store.state.user.info?.jobNumber;
    },
    watch: {
      formData: {
        immediate: true,
        handler () {
          if (this.formData.dateType == 2) {
            this.$set(
              this.workReport,
              'executeTime',
              this.formData.appointTime
            );
          }
        }
      }
    },
    methods: {
      getCategory (id, memo) {
        this.$refs.catogaryDialogRef.open(id, this[memo], (res) => {
          this[memo].rootCategoryLevelId = res.categoryLevelId;
          this[memo].code = res.code;
          this[memo].name = res.name;
          this[memo].specification = res.specification;
          this[memo].sourceInstanceId = res.id;
          if (memo === 'moduleMsg') {
            this[memo].model = res.modelType;
            this[memo].extraField.stampingTimes = '';
          } else {
            this[memo].extraField.num = '';
          }
        });
      },
      async getReportCount () {
        const res = await reportCount({
          taskCode: this.taskInfo.code,
          workOrderId: this.infoData.id
        });

        this.countMsg = res;
      },
      getEquip () {
        this.$refs.workshopRef.openSingle([this.workReportArea], (res) => {
          this.workReportArea.code = res.code;
          this.workReportArea.name = res.name;
          this.workReportArea.path = res.workshopName;
          this.workReportArea.sourceAreaId = res.id;
          this.workReportArea.id = res.id;
        });
      },
      report (fun) {
        this.$refs.formRef.validate((value) => {
          if (value) {
            this.$confirm('是否确定要报工?', '提示').then(() => {
              this.categoryMsg = Object.assign(this.categoryMsg, {
                brandNum: this.infoData.brandNo,
                sourceCategoryId: this.infoData.categoryId,
                name: this.infoData.productName,
                code: this.infoData.productCode
              });

              const workReportCategoryList = [this.categoryMsg];
              if (this.boatMsg.code) {
                workReportCategoryList.push(this.boatMsg);
              }
              fun({
                checkState: 1,
                workReportArea: this.workReportArea,
                workReport: this.workReport,
                workReportDeviceList: this.workReportDeviceList
                  ? [this.workReportDeviceList]
                  : [],
                workReportCategoryList
              }).then((res) => {
                // this.$message.success('报工成功！');
                // this.getReportCount();
              });
            });
          } else {
            this.$message.error('请输入必填项！');
          }
        });
      }
    }
  };
</script>

<style lang="scss" scoped>
  .message-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    .label {
      margin-right: 5px;
    }
    ul {
      list-style: none;
      display: flex;
      align-items: center;
      justify-content: flex-start;

      li {
        margin-right: 20px;
      }
    }

    .right {
      padding-top: 22px;
      display: flex;
      align-items: center;
      .el-form-item {
        margin-left: 10px;
      }
    }
  }
</style>
