<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="工序任务编码" prop="taskCode">
            <el-input
              v-model="queryParams.taskCode"
              placeholder="请输入工序任务编码"
              clearable
              style="width:220px;"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="上机状态" prop="type">
            <el-select v-model="queryParams.type" style="width:220px;" multiple placeholder="请选择上机状态" @change="handleType" clearable>
              <el-option label="上机" value="1" />
              <el-option label="下机" value="2" />
              <el-option label="待上机" value="3" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="是否入库" prop="warehouseStr">
            <el-select v-model="queryParams.warehouseStr" style="width:220px;" placeholder="请选择是否入库" clearable>
              <el-option label="是" value="2" />
              <el-option label="否" value="1" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工作单元id" prop="workunitId">
            <el-input
              v-model="queryParams.workunitId"
              placeholder="请输入工作单元id"
              style="width:220px;"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="工作单元名称" prop="workunitName">
            <el-input
              v-model="queryParams.taskid"
              placeholder="请输入工作单元名称"
              style="width:220px;"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="上机时间" prop="upDate">
            <el-date-picker clearable
              v-model="queryParams.upDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择上机时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="下机时间" prop="downDate">
            <el-date-picker clearable
              v-model="queryParams.downDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择下机时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <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-col>
      </el-row>
    </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="['tm:toolmachinesrecord: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="['tm:toolmachinesrecord: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="['tm:toolmachinesrecord:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tm:toolmachinesrecord:export']"
        >导出</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="上下机编码" min-width="150" align="center" prop="toolMachinesCode" />
      <el-table-column label="申请单编码" min-width="130" align="center" prop="toolRequestCode" />
      <el-table-column label="上机状态" align="center" prop="typeName" width="120"/>
      <el-table-column label="是否入库" align="center" prop="warehouse" width="180">
        <!-- # 1 未入库,2 已入库 -->
        <template slot-scope="scope">
          <span>{{ scope.row.warehouse == '2'? '是': '否' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库原因" align="center" prop="warehouseDesc" width="120"/>
      <el-table-column label="上机时间" width="120" align="center" prop="upDate" />
      <el-table-column label="下机时间" width="120" align="center" prop="downDate" />
      <el-table-column label="物料名称" width="150" align="center" prop="itemName" />
      <el-table-column label="物料编码" width="150" align="center" prop="itemCode" />
      <el-table-column label="台账编码" width="180" align="center" prop="toolCode" />
      <el-table-column label="台账名称" width="180" align="center" prop="toolName" />
      <el-table-column label="领用单编码" min-width="150" align="center" prop="toolRequestUseCode" />
      <el-table-column label="工序任务编码" width="120" align="center" prop="taskCode" />
      <el-table-column label="工作单元名称" min-width="100" align="center" prop="workunitName" />
      <el-table-column label="工作单元id" min-width="100" align="center" prop="workunitId" />

      <!-- <el-table-column label="操作" align="center" width="100" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :disabled="!(scope.row.type == '2' && scope.row.warehouse == '1')"
            icon="el-icon-bottom-right"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tm:toolmachinesrecord:edit']"
          >入库</el-button> -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tm:toolmachinesrecord: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="入库原因" prop="warehouseDesc">
          <el-select value="['','']"
                    v-model="form.warehouseDesc"
                    clearable
                    style="width:100%;"
                    placeholder="请选择入库原因"
          >
            <el-option
              v-for="dict in dict.type.warehouse_desc"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecordWarehouse } from "@/api/mes/tm/toolmachinesrecord";

export default {
  name: "toolmachinesrecord",
  dicts: [
    "warehouse_desc"
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 刀模版上下机记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskCode: null,
        workunitId: null,
        workunitName: null,
        type: null,
        typeStr: null,
        warehouseStr: null,
        toolRequestUseItemId: null,
        upDate: null,
        downDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warehouseDesc:[
          { required: true, message: '请选择入库原因', trigger: 'change' }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleType(e) {
      console.log(e, 'type')
      if (e.length > 0) {
        this.$set(this.queryParams, 'typeStr', e.join(',') )
      } else {
        this.$set(this.queryParams, 'typeStr', '' )
      }
      console.log(this.queryParams, 'this.queryParams')
    },
    handleInWareHouse(e) {
      console.log(e, 'House')
    },
    /** 查询刀模版上下机记录列表 */
    getList() {
      this.loading = true;
      const params = Object.assign({}, this.queryParams)
      delete params.type
      listRecord(params).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskCode: null,
        workunitId: null,
        workunitName: null,
        type: null,
        typeStr: null,
        warehouseStr: null,
        toolRequestUseItemId: null,
        upDate: null,
        downDate: 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.toolMachinesRecordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加刀模版上下机记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = Object.assign(row, {})
      this.open = true;
      this.title = "申请入库";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.toolMachinesRecordId != null) {
            const params = {
              "type": this.form.type,
              "warehouse": this.form.warehouse,
              "toolMachinesRecordId": this.form.toolMachinesRecordId,
              "warehouseDesc": this.form.warehouseDesc
            }
            updateRecordWarehouse(params).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const toolMachinesRecordIds = row.toolMachinesRecordId || this.ids;
      this.$modal.confirm('是否确认删除刀模版上下机记录编号为"' + toolMachinesRecordIds + '"的数据项？').then(function() {
        return delRecord(toolMachinesRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
