<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--仓库数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
              v-model="nickName"
              placeholder="请输入用户名称"
              clearable
              size="small"
              prefix-icon="el-icon-search"
              style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
              :data="userList"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              node-key="userId"
              default-expand-all
              highlight-current
              @node-click="handleNodeClick"
          />
        </div>
      </el-col>

      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="98px">

          <el-form-item label="销售订单号" prop="saleorderCode">
            <el-input
                v-model="queryParams.saleorderCode"
                placeholder="请输入销售订单号"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="计划编号" prop="planCode">
            <el-input
                v-model="queryParams.planCode"
                placeholder="请输入计划编号"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="计划明细编号" prop="planDetailCode">
            <el-input
                v-model="queryParams.planDetailCode"
                placeholder="请输入计划明细编号"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="成品编码" prop="productCode">
            <el-input
                v-model="queryParams.productCode"
                placeholder="请输入成品编码"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="成品名称" prop="productName">
            <el-input
                v-model="queryParams.productName"
                placeholder="请输入成品名称"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="成品规格" prop="productSpec">
            <el-input
                v-model="queryParams.productSpec"
                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-table v-loading="loading" :data="produceStatisticsDetailList" @selection-change="handleSelectionChange">
          <el-table-column label="销售订单号" align="center" prop="saleorderCode"/>
<!--          <el-table-column label="生产任务编号" align="center" prop="orderCode"/>-->
          <el-table-column label="计划编号" align="center" prop="planCode"/>
          <el-table-column label="计划明细编号" align="center" prop="planDetailCode"/>
          <el-table-column label="成名编码" align="center" prop="productCode"/>
          <el-table-column label="成品名称" align="center" prop="productName"/>
          <el-table-column label="成品规格" align="center" prop="productSpec"/>
          <el-table-column label="操作人员" align="center" prop="nickName"/>
          <el-table-column label="工序名称" align="center" prop="processName"/>
          <el-table-column label="计划开始时间" align="center" prop="planBeginTime" width="180">
          </el-table-column>
          <el-table-column label="计划结束时间" align="center" prop="planEndTime" width="180">
          </el-table-column>
          <el-table-column label="开始时间" align="center" prop="realBeginTime" width="180">
          </el-table-column>
          <el-table-column label="完成时间" align="center" prop="realEndTime" width="180">
          </el-table-column>
          <el-table-column label="生产用时(小时)" align="center" prop="produceTime"/>
          <!--      <el-table-column label="工艺路线" align="center" prop="dispatchName" />-->
        </el-table>

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

  </div>
</template>

<script>
import {
  listProduceStatisticsDetail,
  getProduceStatisticsDetail,
  delProduceStatisticsDetail,
  addProduceStatisticsDetail,
  updateProduceStatisticsDetail,
  getUserList
} from "@/api/mes/produceStatisticsDetail";
import toggleMenuLeftImg from "@/assets/images/togglemenu-left.png";
import toggleMenuRightImg from "@/assets/images/togglemenu-right.png";

export default {
  name: "ProduceStatisticsDetail",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产完成明细表格数据
      produceStatisticsDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productOrderId: null,
        saleOrderId: null,
        saleorderCode: null,
        planId: null,
        planCode: null,
        planDetailId: null,
        planDetailCode: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpec: null,
        userId: null,
        nickName: null,
        processId: null,
        processName: null,
        planBeginTime: null,
        planEndTime: null,
        realBeginTime: null,
        realEndTime: null,
        produceTime: null,
        dispatchType: null,
        dispatchId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productOrderId: [
          {required: true, message: "生产工单ID不能为空", trigger: "blur"}
        ],
        planId: [
          {required: true, message: "生产计划ID不能为空", trigger: "blur"}
        ],
        planCode: [
          {required: true, message: "计划编号,关联mes_product_plan_info的plan_code不能为空", trigger: "blur"}
        ],
        planDetailId: [
          {required: true, message: "生产计划明细ID不能为空", trigger: "blur"}
        ],
        planDetailCode: [
          {required: true, message: "生产计划明细编号不能为空", trigger: "blur"}
        ],
        productId: [
          {required: true, message: "生产成品ID不能为空", trigger: "blur"}
        ],
      },

      toggleMenuLeftImg: toggleMenuLeftImg,
      toggleMenuRightImg: toggleMenuRightImg,
      toggleLeftDisplay: true,
      toggleRightDisplay: false,
      // 用户昵称
      nickName: undefined,
      userList: [],
      defaultProps: {
        children: "children",
        label: "nickName"
      },
    };
  },
  created() {
    this.getUserList();
    this.getList();
  },
  methods: {
    /** 查询生产完成明细列表 */
    getList() {
      this.loading = true;
      listProduceStatisticsDetail(this.queryParams).then(response => {
        this.produceStatisticsDetailList = response.rows;
        this.produceStatisticsDetailList.forEach(psd => {
             if(psd.produceTime && psd.produceTime!==''){
               psd.produceTime = (psd.produceTime/1000/60/60).toFixed(2);
             }
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        statisticsDetailId: null,
        productOrderId: null,
        saleOrderId: null,
        saleorderCode: null,
        planId: null,
        planCode: null,
        planDetailId: null,
        planDetailCode: null,
        productId: null,
        productCode: null,
        productName: null,
        productSpec: null,
        userId: null,
        nickName: null,
        processId: null,
        processName: null,
        planBeginTime: null,
        planEndTime: null,
        realBeginTime: null,
        realEndTime: null,
        produceTime: null,
        createTime: null,
        dispatchType: null,
        dispatchId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.statisticsDetailId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产完成明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const statisticsDetailId = row.statisticsDetailId || this.ids
      getProduceStatisticsDetail(statisticsDetailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产完成明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.statisticsDetailId != null) {
            updateProduceStatisticsDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduceStatisticsDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const statisticsDetailIds = row.statisticsDetailId || this.ids;
      this.$modal.confirm('是否确认删除生产完成明细编号为"' + statisticsDetailIds + '"的数据项？').then(function () {
        return delProduceStatisticsDetail(statisticsDetailIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/produceStatisticsDetail/export', {
        ...this.queryParams
      }, `produceStatisticsDetail_${new Date().getTime()}.xlsx`)
    },

    toggleClick() {
      if (this.toggleLeftDisplay) {
        this.toggleRightDisplay = true;
        this.toggleLeftDisplay = false;
      } else if (this.toggleRightDisplay) {
        this.toggleRightDisplay = false;
        this.toggleLeftDisplay = true;
      }
    },

    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.nickName.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.userId = data.userId;
      this.handleQuery();
    },


    getUserList() {
      this.loading = true;
      getUserList().then(response => {
        this.userList = response.data;
        this.loading = false;
      });
    },

  }
};
</script>
