<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="报工类型" prop="feedbackType">
        <el-select v-model="queryParams.feedbackType" placeholder="请选择报工类型" clearable>
          <el-option v-for="dict in dict.type.mes_feedback_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工作中心名称" prop="workstationName">
        <el-input v-model="queryParams.workstationName" placeholder="请输入工作中心名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="外协单号" prop="assistProcessCode">
        <el-input v-model="queryParams.assistProcessCode" placeholder="请输入外协单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="生产工单编号" prop="workorderCode">
        <el-input v-model="queryParams.workorderCode" placeholder="请输入生产工单编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品物料编码" prop="sapItemCode">
        <el-input v-model="queryParams.sapItemCode" placeholder="请输入产品物料编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品物料名称" prop="itemName">
        <el-input v-model="queryParams.itemName" placeholder="请输入产品物料名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="报工人" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入报工人名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="记录人" prop="recordUser">
        <el-input v-model="queryParams.recordUser" placeholder="请输入记录人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['mes:pro:feedback:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['mes:pro:feedback:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mes:pro:feedback:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-printer" size="mini" :disabled="single" @click="handleQrcode"
          v-hasPermi="['mes:pro:feedback:edit']">打印二维码</el-button>
      </el-col>
       -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报工类型" align="center" width="80" prop="feedbackType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_feedback_type" :value="scope.row.feedbackType" />
        </template>
      </el-table-column>
      <el-table-column label="工作中心" width="120" align="center" prop="workstationName" />
      <el-table-column label="工作单元" width="120" align="center" prop="workunitName" />
      <el-table-column label="外协单号" width="150" align="center" prop="assistProcessCode" />
      <el-table-column label="生产工单编号" width="150" align="center" prop="workorderCode" />
      <el-table-column label="产品物料编码" width="120" align="center" prop="sapItemCode" />
      <el-table-column label="产品物料名称" min-width="250" show-overflow-tooltip align="center" prop="itemName" />
      <el-table-column label="规格型号" width="100" align="center" prop="specification" />
      <el-table-column label="指令号" width="120" align="center" prop="clientCmd" />
      <el-table-column label="标准SIZE" width="120" align="center" prop="standardSize" />
      <el-table-column label="报工数量" align="center" prop="feedbackTime" width="180">
        <template slot-scope="scope">
          <span>{{ getSumNumber(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合格数量" width="100" align="center" prop="quantityQualify" />
      <el-table-column label="不合格数量" width="100" align="center" prop="quantityUnqualify" />
      <el-table-column label="报工人" width="100" align="center" prop="nickName" />
      <el-table-column label="报工时间" align="center" prop="feedbackTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.feedbackTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" width="100" align="center" prop="recordNick" />
      <el-table-column label="操作" align="center" fixed="right" width="160px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-query" @click="handleView(scope.row)"
            v-hasPermi="['mes:pro:feedback:query']">查看</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:pro:feedback:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mes:pro:feedback:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改生产报工记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="报工类型" prop="feedbackType">
              <el-select v-model="form.feedbackType" placeholder="请选择报工类型" @change="handleFeedbackTypeChange">
                <el-option v-for="dict in dict.type.mes_feedback_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="form.feedbackType == 'ASSIST_PROCESS'" :span="8">
            <el-form-item label="外协单号" prop="assistProcessCode">
              <el-input v-model="form.assistProcessCode" placeholder="请选择外协单号">
                <el-button slot="append" icon="el-icon-search"
                  @click="$refs.assistProcessSelect.showFlag = true"></el-button>
              </el-input>
            </el-form-item>
            <assistProcessSelect v-if="open" ref="assistProcessSelect" @onSelected="onAssistProcessSelect">
            </assistProcessSelect>
          </el-col>
          <el-col v-if="form.feedbackType == 'ASSIST_PROCESS'" :span="8">
            <el-form-item label="是否关单" prop="closeType">
              <el-radio-group v-model="form.closeType">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
            <assistProcessSelect v-if="open" ref="assistProcessSelect" @onSelected="onAssistProcessSelect">
            </assistProcessSelect>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产工单" prop="workorderCode">
              <el-input :disabled="form.feedbackType == 'ASSIST_PROCESS'" v-model="form.workorderCode"
                placeholder="请选择生产工单">
                <el-button :disabled="form.feedbackType == 'ASSIST_PROCESS'" slot="append" icon="el-icon-search"
                  @click="handleWorkorderSelect"></el-button>
              </el-input>
            </el-form-item>
            <WorkorderSelect v-if="open" ref="woSelect" @onSelected="onWorkorderSelected"></WorkorderSelect>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产任务" prop="taskCode">
              <el-input :disabled="form.feedbackType == 'ASSIST_PROCESS'" v-model="form.taskCode" placeholder="请选择生产任务">
                <el-button :disabled="form.feedbackType == 'ASSIST_PROCESS'" slot="append" icon="el-icon-search"
                  @click="handleTaskSelect"></el-button>
              </el-input>
            </el-form-item>
            <ProtaskSelect v-if="open" ref="taskSelect" :workorderId="form.workorderId" @onSelected="onTaskSelected">
            </ProtaskSelect>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品编码" prop="sapItemCode">
              <el-input v-model="form.sapItemCode" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品名称" prop="itemName">
              <el-input v-model="form.itemName" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位" prop="unitOfMeasure">
              <el-input v-model="form.unitOfMeasure" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="规格型号" prop="specification">
              <el-input v-model="form.specification" type="textarea" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="报工数量" prop="quantityFeedback">
              <el-input disabled v-model="form.quantityFeedback" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="良品报工" prop="quantityQualify">
              <el-input-number :min="0" @change="handleQuantityChanged" v-model="form.quantityQualify"
                placeholder="请输入良品报工" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="不良品报工" prop="quantityUnqualify">
              <el-input-number :min="0" @change="handleQuantityChanged" v-model="form.quantityUnqualify"
                placeholder="请输入不良品报工" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="加工工时" prop="machineTime">
              <el-input-number :min="0" v-model="form.machineTime" placeholder="请输入加工工时" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="报工人" prop="nickName">
              <el-input v-model="form.nickName" placeholder="请选择报工人">
                <el-button slot="append" @click="handleUserSelect" icon="el-icon-search"></el-button>
              </el-input>
            </el-form-item>
            <UserSingleSelect v-if="open" ref="userSelect" @onSelected="onUserSelected"></UserSingleSelect>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报工时间" prop="feedbackTime">
              <el-date-picker clearable v-model="form.feedbackTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择报工时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="审核人" prop="recordNick">
              <el-input v-model="form.recordNick" placeholder="请选择审核人">
                <el-button slot="append" @click="handleUser2Select" icon="el-icon-search"></el-button>
              </el-input>
            </el-form-item>
            <UserSingleSelect ref="user2Select" @onSelected="onUser2Selected"></UserSingleSelect>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="optType == 'view'">返回</el-button>
        <el-button type="primary" @click="submitForm" v-if="optType != 'view'">保 存</el-button>
        <!-- <el-button type="primary" @click="handleSubmit" v-if="form.status =='PREPARE' && optType !='view' && form.recordId !=null ">提交审批</el-button>
        <el-button type="success" @click="handleExecute" v-if="form.status =='APPROVING'  && form.recordId !=null">审批通过</el-button>
        <el-button type="danger" @click="handleReject" v-if="form.status =='APPROVING'  && form.recordId !=null">审批不通过</el-button> -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeedback, getFeedback, delFeedback, addFeedback, updateFeedback, execute, listNextProcess } from "@/api/mes/pro/feedback";
import WorkorderSelect from "@/components/workorderSelect/single.vue"
import WorkstationSelect from "@/components/workstationSelect/simpletableSingle.vue"
import UserSingleSelect from "@/components/userSelect/single.vue"
import ProtaskSelect from "@/components/TaskSelect/taskSelectSingle.vue"
import assistProcessSelect from "./components/assistProcessSelect.vue"
import { postPrintPdf } from '@/utils/ruoyi'
import dayjs from 'dayjs'
export default {
  name: "Feedback",
  components: { WorkorderSelect, WorkstationSelect, UserSingleSelect, ProtaskSelect, assistProcessSelect },
  dicts: ['mes_order_status', 'mes_feedback_type'],
  data() {
    return {
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产报工记录表格数据
      feedbackList: [],

      selectRow: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        feedbackType: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        workorderId: null,
        assistProcessCode: null,
        workorderCode: null,
        workorderName: null,
        taskId: null,
        taskCode: null,
        itemId: null,
        itemCode: null,
        sapItemCode: null,
        itemName: null,
        unitOfMeasure: null,
        specification: null,
        quantity: null,
        quantityFeedback: null,
        quantityQualify: null,
        quantityUnqualify: null,
        feedbackTime: null,
        userName: null,
        nickName: null,
        feedbackChannel: null,
        machineTime: 0,
        recordUser: null,
        recordNick: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        feedbackType: [
          { required: true, message: "报工类型不能为空", trigger: "change" }
        ],
        taskCode: [
          { required: true, message: "请选择生产任务", trigger: ['blur', 'change'] }
        ],
        workorderCode: [
          { required: true, message: "生产工单不能为空", trigger: ['blur', 'change'] }
        ],
        assistProcessCode: [
          { required: true, message: "外协工单不能为空", trigger: ['blur', 'change'] }
        ],
        closeType: [
          { required: true, message: "是否关单不能为空", trigger: ['blur', 'change'] }
        ],
        nickName: [
          { required: true, message: "请选择报工人", trigger: ['blur', 'change'] }
        ],
        feedbackTime: [
          { required: true, message: "请选择报工时间", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getSumNumber(row) {
      const first = row.quantityQualify ? Number(row.quantityQualify) : 0
      const last = row.quantityUnqualify ? Number(row.quantityUnqualify) : 0
      return first + last
    },
    /** 查询生产报工记录列表 */
    getList() {
      this.loading = true;
      listFeedback(this.queryParams).then(response => {
        this.feedbackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleFeedbackTypeChange(value) {
      console.log(value)
      if (value !== 'ASSIST_PROCESS') {
        this.form.assistProcessCode = null
        this.form.closeType = 0
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        feedbackType: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        routeId: null,
        routeCode: null,
        processId: null,
        processCode: null,
        processName: null,
        workorderId: null,
        workorderCode: null,
        workorderName: null,
        taskId: null,
        taskCode: null,
        itemId: null,
        itemCode: null,
        sapItemCode: null,
        itemName: null,
        specification: null,
        assistProcessCode: null,
        closeType: 0,
        quantity: 0,
        machineTime: 0,
        quantityFeedback: 0,
        quantityQualify: 0,
        quantityUnqualify: 0,
        userName: null,
        nickName: null,
        feedbackChannel: null,
        feedbackTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
        recordUser: null,
        recordNick: null,
        status: "PREPARE",
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    handleQuantityChanged() {
      this.form.quantityFeedback = this.form.quantityQualify + this.form.quantityUnqualify;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.selectRow = selection
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产报工记录";
      this.optType = "add";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getFeedback(recordId).then(response => {
        this.form = response.data;
        if (dayjs(response.data.feedbackTime, 'YYYY-MM-DD', true).isValid()) {
          this.form.feedbackTime = response.data.feedbackTime + ' 00:00:00'
        }
        this.open = true;
        this.title = "修改生产报工记录";
        this.optType = "edit";
      });
    },
    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      const recordId = row.recordId || this.ids;
      getFeedback(recordId).then(response => {
        this.form = response.data;
        if (dayjs(response.data.feedbackTime, 'YYYY-MM-DD', true).isValid()) {
          this.form.feedbackTime = response.data.feedbackTime + ' 00:00:00'
        }
        this.open = true;
        this.title = "查看生产报工单信息";
        this.optType = "view";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateFeedback(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();

            });
          } else {
            addFeedback(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleSubmit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.status = "APPROVING";
          if (this.form.recordId != null) {
            updateFeedback(this.form).then(response => {
              this.$modal.msgSuccess("提交成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //审批通过执行
    handleExecute() {
      const recordIds = this.form.recordId;
      this.$modal.confirm('确认执行报工？').then(function () {
        return execute(recordIds)//执行报工
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("执行成功");
        this.open = false;
      }).catch(() => { });
    },
    //审批不通过，拒绝
    handleReject() {
      this.form.status = "PREPARE";
      if (this.form.recordId != null) {
        updateFeedback(this.form).then(response => {
          this.$modal.msgSuccess("提交成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm('是否确认删除生产报工记录编号为"' + recordIds + '"的数据项？').then(function () {
        return delFeedback(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },

    /** 二维码打印 */
    handleQrcode() {
      let workunitName = '';
      let detail = []
      if(this.selectRow.some(v => v.qrcode!=null)){
        this.$message.warning('该数据是包装工序的报工，不能打印二维码')
        return
      }
      this.selectRow.forEach((s, i) => {
        
        listNextProcess({ taskId: s.taskId }).then(workunit => {
          workunitName = workunit.data.workunitName;
          detail.push({
            "barcode": s.workorderCode,
            "showText": '编排单号' + s.arrangeCode + '\n工单号' + s.workorderCode + '\n数量' + s.quantityQualify + '\n下道工序机台:' + workunitName,
            "gp": i + 1
          })
          detail.push({
            "barcode": s.workorderCode,
            "showText": '编排单号' + s.arrangeCode + '\n工单号' + s.workorderCode + '\n数量' + s.quantityQualify + '\n下道工序机台:' + workunitName,
            "gp": i + 1
          })
          postPrintPdf(
            "qrcode-50*50-2",
            {
              detail
            }
          );
        });
      });

    },




    /** 导出按钮操作 */
    handleExport() {
      this.download('pro/feedback/export', {
        ...this.queryParams
      }, `feedback_${new Date().getTime()}.xlsx`)
    },
    //选择生产工单
    handleWorkorderSelect() {
      this.$refs.woSelect.showFlag = true;
    },
    onWorkorderSelected(row) {
      if (row != undefined && row != null) {
        this.form.workorderId = row.workorderId;
        this.form.workorderCode = row.workorderCode;
        this.form.workorderName = row.workorderName;
        this.form.itemId = row.productId;
        this.form.itemCode = row.productCode;
        this.form.itemName = row.productName;
        this.form.specification = row.productSpc;
        this.form.unitOfMeasure = row.unitOfMeasure;
      }
    },
    onAssistProcessSelect(row) {
      if (row != undefined && row != null) {
        this.form.workorderId = row.workorderId;
        this.form.workorderCode = row.workorderCode;
        this.form.taskId = row.taskId;
        this.form.taskCode = row.taskCode;
        this.form.assistProcessCode = row.assistProcessCode;
      }
    },
    handleTaskSelect() {
      this.$refs.taskSelect.showFlag = true;
      this.$refs.taskSelect.getList();
    },
    onTaskSelected(row) {
      debugger;
      if (row != undefined && row != null) {
        this.form.taskId = row.taskId;
        this.form.taskCode = row.taskCode;
        this.form.taskName = row.taskName;
        this.form.workstationId = row.workstationId;
        this.form.workstationCode = row.workstationCode;
        this.form.workstationName = row.workstationName;
        this.form.processId = row.processId;
        this.form.processCode = row.processCode;
        this.form.processName = row.processName;
        this.form.routeId = row.routeId;
        this.form.routeCode = row.routeCode;
        this.form.isCheck = row.isCheck;
        if (row.isCheck == 'N') {
          this.form.quantityUncheck = 0;
        } else {
          this.form.quantityQualify = 0;
          this.form.quantityUnqualify = 0;
        }
      }
    },
    //点击人员选择按钮
    handleUserSelect() {
      this.$refs.userSelect.showFlag = true;
    },
    //人员选择返回
    onUserSelected(row) {
      this.form.nickName = row.nickName;
      this.form.userName = row.userName;
    },
    //点击人员选择按钮
    handleUser2Select() {
      this.$refs.user2Select.showFlag = true;
    },
    //人员选择返回
    onUser2Selected(row) {
      this.form.recordUser = row.userName;
      this.form.recordNick = row.nickName;
    },
  }
};
</script>
