<template>
  <div class="app-container">
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('车间异常单编号')" prop="abnormalNumber" label-width="110px">
        <el-input
          v-model="queryParams.abnormalNumber"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('工序编号')" prop="processCode">
        <el-input
          v-model="queryParams.processCode"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('common.workunit')" prop="workstationName">
        <el-input
          v-model="queryParams.workstationName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('异常原因')" prop="abnormalReason">
        <el-input
          v-model="queryParams.abnormalReason"
         
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="异常时间" prop="abnormalTime">
        <el-input
          v-model="queryParams.abnormalTime"
         
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item :label="$t('异常时间')">
        <el-date-picker
          v-model="daterangePurchaseDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          :start-placeholder="$t('common.start_date')"
          :end-placeholder="$t('common.end_date')"
        ></el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="单据状态" prop="abnormalStatus">
        <el-select v-model="queryParams.abnormalStatus" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </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="['qc:report: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="['qc:report: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="['qc:report: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="['qc:report: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="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('车间异常单编号')" align="center" prop="abnormalNumber" />
      <el-table-column :label="$t('工序编号')" align="center" prop="processCode" />
      <el-table-column :label="$t('工序名称')" align="center" prop="processName" />
      <el-table-column :label="$t('作业单元')" align="center" prop="workstationName" />
      <!-- <el-table-column :label="$t('common.workunit_name')" align="center" prop="workstationName" /> -->
      <el-table-column :label="$t('异常类型')" align="center" prop="abnormalType" >
         <template slot-scope="scope">
          <dict-tag :options="dict.type.pc_abnormal_type" :value="scope.row.abnormalType"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('异常原因')" align="center" prop="abnormalReason" />
      <el-table-column :label="$t('异常时间')" align="center" prop="abnormalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.abnormalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('备注')" align="center" prop="remarks" />
        <el-table-column :label="$t('单据状态')" align="center" prop="abnormalStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.qc_submit_status" :value="scope.row.abnormalStatus"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.option')" align="center" width="300" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
           <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleComplete(scope.row)"
            :disabled="scope.row.abnormalStatus === 'CLOSE' || scope.row.abnormalStatus === 'NOT' || scope.row.abnormalStatus === 'FINISH'"
            v-hasPermi="['qc:report:edit']"
          >{{$t('完成')}}</el-button>
           <!-- <el-button
           size="mini"
           type="text"
           icon="el-icon-upload2"
           @click="0"
           >{{ $t('common.submit') }}</el-button> -->
          <el-button
            size="mini"
            type="text"
            @click="handleStart(scope.row)"
            :disabled="scope.row.abnormalStatus === 'CLOSE' || scope.row.abnormalStatus === 'FINISH' || scope.row.abnormalStatus === 'SUBMIT'"
            v-hasPermi="['qc:report:edit']"
          >{{ $t('开始') }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            :disabled="scope.row.abnormalStatus === 'CLOSE' || scope.row.abnormalStatus === 'FINISH'"
            @click="handleClose(scope.row)"
            v-hasPermi="['qc:report:remove']"
          >{{ $t('关闭') }}</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="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('备注')" prop="remarks">
              <el-input v-model="form.remarks" />
            </el-form-item>
          </el-col>
        </el-row>
      </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 { listReport, getReport, delReport, addReport, updateReport,completeReport, startReport, closeReport } from "@/api/mes/qc/report";
import dayjs from 'dayjs'
export default {
  dicts: ['qc_submit_status','pc_abnormal_type'],
  name: "Report",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车间异常单表格数据
      reportList: [],

      daterangePurchaseDate: [dayjs().startOf('month').format('YYYY-MM-DD'), dayjs().endOf('month').format('YYYY-MM-DD')],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        batchNumber: null,
        processId: null,
        processCode: null,
        processName: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        abnormalType: null,
        abnormalReason: null,
        abnormalTime: null,
        abnormalStatus: null,      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleStart(row){
      startReport(row.abnormalReportId).then(res=>{
        this.getList();
      })
    },
    handleClose(row){
      this.form = {
        abnormalReportId: row.abnormalReportId,
        remarks: ""
      }
      this.open = true;
      this.title = this.$t('关闭');
    },
    /** 查询车间异常单列表 */
    getList() {
      this.loading = true;
      if (null != this.daterangePurchaseDate && '' != this.daterangePurchaseDate) {
        this.queryParams["beginPurchaseDate"] = this.daterangePurchaseDate[0];
        this.queryParams["endPurchaseDate"] = this.daterangePurchaseDate[1];
      } else {
        delete this.queryParams["beginPurchaseDate"]
        delete this.queryParams["endPurchaseDate"]
      }
      listReport(this.queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        abnormalReportId: null,
        batchNumber: null,
        processId: null,
        processCode: null,
        processName: null,
        workstationId: null,
        workstationCode: null,
        workstationName: null,
        abnormalType: null,
        abnormalReason: null,
        abnormalTime: null,
        abnormalStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.daterangePurchaseDate = [dayjs().startOf('month').format('YYYY-MM-DD'), dayjs().endOf('month').format('YYYY-MM-DD')];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.abnormalReportId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('添加车间异常单');
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.title === this.$t('完成')) {
            completeReport(this.form).then(response => {
              this.open = false;
              this.getList();
            });
          } else {
            closeReport(this.form).then(response => {
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
       /** 完成按钮操作 */
    handleComplete(row) {
      this.form = {
        abnormalReportId: row.abnormalReportId,
        remarks: ""
      }
      this.open = true;
      this.title = this.$t('完成');
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('qc/report/export', {
        ...this.queryParams
      }, `report_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
