<template>
  <div class="app-container">
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
          <el-form-item :label="$t('工序任务编码')" prop="taskCode">
            <el-input
              v-model="queryParams.taskCode"
             
              clearable
              style="width:220px;"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('上机状态')" prop="type">
            <el-select v-model="queryParams.type" style="width:220px;" multiple @change="handleType" clearable>
              <el-option :label="$t('上机')" value="1" />
              <el-option :label="$t('下机')" value="2" />
              <el-option :label="$t('待上机')" value="3" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('是否入库')" prop="warehouseStr">
            <el-select v-model="queryParams.warehouseStr" style="width:220px;" clearable>
              <el-option :label="$t('是')" value="2" />
              <el-option :label="$t('否')" value="1" />
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('common.workunit_code')" prop="workunitCode">
            <el-input
              v-model="queryParams.workunitCode"
             
              style="width:220px;"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('common.workunit_name')" prop="workunitName">
            <el-input
              v-model="queryParams.taskid"
             
              style="width:220px;"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('上机时间')" prop="upDate">
            <el-date-picker clearable
              v-model="queryParams.upDate"
              type="date"
              value-format="yyyy-MM-dd"
             >
            </el-date-picker>
          </el-form-item>
          <el-form-item :label="$t('下机时间')" prop="downDate">
            <el-date-picker clearable
              v-model="queryParams.downDate"
              type="date"
              value-format="yyyy-MM-dd"
             >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('common.search') }}</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.reset') }}</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="['tm:toolmachinesrecord:add']"
        >{{ $t('common.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']"
        >{{ $t('common.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']"
        >{{ $t('common.del') }}</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']"
        >{{ $t('common.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="$t('上下机编码')" min-width="150" align="center" prop="toolMachinesCode" />
      <el-table-column :label="$t('申请单编码')" min-width="130" align="center" prop="toolRequestCode" />
      <el-table-column :label="$t('上机状态')" align="center" prop="typeName" width="120"/>
      <el-table-column :label="$t('是否入库')" align="center" prop="warehouse" width="180">
        <!-- # 1 未入库,2 已入库 -->
        <template slot-scope="scope">
          <span>{{ scope.row.warehouse == '2'? $t('是'): $t('否') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('入库方式')" align="center" prop="warehouseDesc" width="120"/>
      <el-table-column :label="$t('上机时间')" width="170" align="center" prop="upDate" />
      <el-table-column :label="$t('下机时间')" width="170" align="center" prop="downDate" />
      <el-table-column :label="$t('物料名称')" width="150" align="center" prop="itemName" />
      <el-table-column :label="$t('物料编码')" width="150" align="center" prop="sapItemCode" />
      <el-table-column :label="$t('台账编码')" width="180" align="center" prop="toolCode" />
      <el-table-column :label="$t('台账名称')" width="180" align="center" prop="toolName" />
      <el-table-column :label="$t('领用单编码')" min-width="150" align="center" prop="toolRequestUseCode" />
      <el-table-column :label="$t('工序任务编码')" width="120" align="center" prop="taskCode" />
      <el-table-column :label="$t('common.workunit_name')" min-width="100" align="center" prop="workunitName" />
      <el-table-column :label="$t('common.workunit_code')" min-width="100" align="center" prop="workunitCode" />

      <!-- <el-table-column :label="$t('common.option')" 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']"
          >{{ $t('common.del') }}</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="$t('入库方式')" prop="warehouseDesc">
          <el-select
            v-model="form.warehouseDesc"
            clearable
            style="width:100%;"
           
          >
            <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">{{ $t('common.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('common.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: this.$t('请选择入库方式'), 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 = this.$t('添加刀模版上下机记录');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = Object.assign(row, {})
      this.open = true;
      this.title = this.$t('申请入库');
    },
    /** 提交按钮 */
    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.$t('common.edit_success'));
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.add_success'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const toolMachinesRecordIds = row.toolMachinesRecordId || this.ids;
      this.$modal.confirm(this.$t('common.confirm_del_data')).then(function() {
        return delRecord(toolMachinesRecordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('common.del_success'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
