<template>
  <el-form
    class="form-no-message"
    ref="formRef"
    :show-message="false"
    label-position="left"
    :model="{
      ...workReport
    }"
  >
    <div class="message-box">
      <ul>
        <li> <span class="label">报工次数</span> {{ countMsg.reportNum }}</li>
        <li>
          <span class="label">累计实际数量</span>{{ countMsg.joinTotalNum }}PCS
        </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="2">{{
          countMsg.surplusStandardNum
        }}</el-descriptions-item>
        <el-descriptions-item label="实际数量（PCS）"
          ><el-input v-model="categoryMsg.number"></el-input
        ></el-descriptions-item>
      </el-descriptions>
      <el-descriptions
        title="设备信息"
        class="mt-16"
        direction="vertical"
        :column="7"
        border
      >
        <el-descriptions-item label="设备编码">{{
          workReportDeviceList.code
        }}</el-descriptions-item>
        <el-descriptions-item label="设备名称">{{
          workReportDeviceList.name
        }}</el-descriptions-item>
        <el-descriptions-item label="规格">{{
          workReportDeviceList.specification
        }}</el-descriptions-item>
        <el-descriptions-item label="型号">{{
          workReportDeviceList.model
        }}</el-descriptions-item>
        <el-descriptions-item label="烧结曲线">
          <el-input
            clearable
            v-model="workReportDeviceList.extraField.sinteringCurve"
            placeholder="请输入"
          />
        </el-descriptions-item>
        <el-descriptions-item label="设备位置">{{
          workReportDeviceList.path
        }}</el-descriptions-item>
        <el-descriptions-item label="操作"
          ><el-link @click="getEquip" type="primary"
            >选择设备</el-link
          ></el-descriptions-item
        >
      </el-descriptions>
    </el-card>
    <equipmentDailog ref="equipmentRef" :taskId="taskInfo.id" />
  </el-form>
</template>

<script>
  import personSelectRemote from '@/components/CommomSelect/person-select-remote';
  import equipmentDailog from '@/components/EquipmentDailog/report-equipment';
  import { reportCount } from '@/api/produceOrder';
  import dayjs from 'dayjs';
  export default {
    components: { personSelectRemote, equipmentDailog },
    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: {
          executorId: '',
          executorJobNum: '',
          executeTime: dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          productInfo: {
            standardNum: '',
            standardWeight: '',
            noStandardNum: '',
            noStandardWeight: ''
          }
        },
        workReportDeviceList: {
          code: '',
          name: '',
          path: '',
          model: '',
          rootCategoryLevelId: '',
          specification: '',
          extraField: {
            sinteringCurve: ''
          }
        },
        countMsg: {}
      };
    },
    watch: {
      taskInfo: {
        immediate: true,
        handler() {
          if (this.taskInfo.code) {
            this.getReportCount();
          }
        }
      },
      formData: {
        immediate: true,
        handler() {
          if (this.formData.dateType == 2) {
            this.$set(
              this.workReport,
              'executeTime',
              this.formData.appointTime
            );
          }
        }
      },
      infoData: {
        immediate: true,
        handler() {
          if (
            this.infoData.id &&
            this.currentInfo.code == this.firstInfo.code &&
            this.infoData.deviceCode
          ) {
            // 设备
            this.workReportDeviceList = Object.assign(
              {},
              this.workReportDeviceList,
              {
                code: this.infoData.deviceCode,
                name: this.infoData.deviceName,
                rootCategoryLevelId: this.infoData.rootCategoryLevelId,
                path: this.infoData.path[0]?.pathName,
                model: this.infoData.model,
                sourceInstanceId: this.infoData.sourceInstanceId,
                specification: this.infoData.specification
              }
            );
          }
        }
      }
    },
    created() {
      this.workReport.executorId = this.$store.state.user.info?.userId;
      this.workReport.executorJobNum = this.$store.state.user.info?.jobNumber;
    },
    methods: {
      async getReportCount() {
        const res = await reportCount({
          taskCode: this.taskInfo.code,
          lastTaskCode: this.taskInfo.lastTaskCode,
          workOrderId: this.infoData.id
        });

        this.countMsg = res;
      },
      getEquip() {
        this.$refs.equipmentRef.openSingle(
          [this.workReportDeviceList],
          (res) => {
            this.workReportDeviceList.code = res.code;
            this.workReportDeviceList.name = res.name;

            this.workReportDeviceList.model = res.category?.category?.modelType;
            this.workReportDeviceList.specification =
              res.category?.category?.specification;
            this.workReportDeviceList.sourceInstanceId = res.id;
            this.workReportDeviceList.rootCategoryLevelId =
              res.rootCategoryLevelId;
            this.workReportDeviceList.path = res.positionList[0]?.pathName;
          }
        );
      },
      report(fun) {
        this.$refs.formRef.validate((value) => {
          if (value) {
            if (!this.workReportDeviceList.code) {
              return this.$message.error('请选择设备');
            }

            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
              });
              // 实际数量 也是 合格品
              this.workReport.productInfo.standardNum = this.categoryMsg.number;
              fun({
                checkState: 1,
                workReport: this.workReport,
                workReportDeviceList: [this.workReportDeviceList],
                workReportCategoryList: [this.categoryMsg]
              }).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>
