<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>
            <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-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="toolWarehouseCode" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="更新人" align="center" prop="updateBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="更新时间" align="center" prop="updateTime"/>
      <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"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tm:toolmachinesrecord:edit']"
          >详情</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="950px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="入库单号" prop="toolWarehouseCode">
              <el-input v-model="form.toolWarehouseCode" disabled placeholder="自动生成" />
            </el-form-item>
          </el-col>
       </el-row>
      </el-form>
      <el-divider content-position="center">入库申请详细</el-divider>
      <el-card shadow="always" class="box-card">
        <itemline v-if="open" ref="line" :quantity="form.requestNum" :id="form.toolWarehouseId" :optType="optType"></itemline>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="optType !== 'view'" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { lisWoreHouse, addWoreHouse } from "@/api/mes/tm/warehouse";
import itemline from './itemIndex'
import { genCode } from "@/api/system/autocode/rule";

export default {
  components: {
    itemline
  },
  name: "warehouse",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 刀模板上下机记录表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      optType: null,
      // 查询参数
      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' }
        ]
      },
      itemCode: null
    };
  },
  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
      lisWoreHouse(params).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 生成编码 */
    gCode() {
      genCode("TM_TOOL_WAREHOUSE_CODE").then((response) => {
        console.log(response, 'toolWarehouseCode')
        this.form.toolWarehouseCode = response;
      });
    },
    /** 生成编码 */
    gItemCode() {
      genCode("TM_TOOL_WAREHOUSE_ITEM_CODE").then((response) => {
        this.itemCode = response;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        toolWarehouseCode: 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.gCode()
      this.gItemCode()
      this.open = true;
      this.optType = 'add'
      this.title = "添加入库申请单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = Object.assign(row, {})
      this.open = true;
      this.optType = 'view'
      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 {
            let flag = true
            let message = []
            this.$refs.line.tmToolRequestItemList.forEach((value,index) => {
              const length = index+1
              if (!value.local) {
                message.push('请填写第'+length+'行入库位置')
                flag = false
              } else if (!value.warehouseDesc) {
                message.push('请填写第'+length+'行入库方式')
                flag = false
              }
            });
            if (!flag) {
              this.$message.error(message[0])
              return
            }
            const params = {
              ...this.form,
              warehouseItemList: this.$refs.line.tmToolRequestItemList.map((item)=> {
                return {
                  ...item,
                  toolWarehouseItemCode:this.itemCode
                }
              })
            }
            addWoreHouse(params).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>
