<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="工单编码" prop="workorderCode">
        <el-input
          v-model="queryParams.workorderCode"
          placeholder="请输入工单编码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单名称" prop="workorderName">
        <el-input
          v-model="queryParams.workorderName"
          placeholder="请输入工单名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品编码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="machineryName">
        <el-input
          v-model="queryParams.machineryName"
          placeholder="请输入设备名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产状态" prop="jobStatus">
        <el-select
          v-model="queryParams.jobStatus"
          placeholder="生产状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in jobStatusOptions"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="需求日期" prop="recptDate">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:pro:machineryJob:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="序号"
        type="index"
        width="80"
        align="center"
        :index="indexMethod"
      />
      <el-table-column label="作业编码" prop="jobCode" align="center" width="150" />
      <el-table-column
        label="工单编码"
        prop="workorderCode"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="工单名称"
        prop="workorderName"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="设备编码"
        prop="machineryCode"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="设备名称"
        prop="machineryName"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="生产状态" prop="jobStatus" align="center">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.jobStatus === 'WORKING'">进行中</el-tag>
          <el-tag type="success" v-if="scope.row.jobStatus === 'FINISHED'">已完成</el-tag>
          <el-tag type="warning" v-if="scope.row.jobStatus === 'CANCEL'">已撤销</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="天然气用量"
        prop="gasCost"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="产品编码"
        prop="productCode"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="产品名称"
        prop="productName"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="需求日期"
        prop="requestDate"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['mes:pro:machineryJob:view']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:pro:machineryJob: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="1080px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备名称">
              <el-input
                v-model="form.machineryName"
                placeholder="设备名称"
                :readonly="operable"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当前作业进度">
              <el-input
                v-model="form.jobProgressName"
                placeholder="当前作业进度"
                :readonly="operable"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工单编码">
              <el-input
                v-model="form.workorderCode"
                placeholder="工单编码"
                :readonly="operable"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工单名称">
              <el-input
                v-model="form.workorderName"
                placeholder="工单名称"
                :readonly="operable"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品编码">
              <el-input
                v-model="form.productCode"
                placeholder="产品编码"
                :readonly="operable"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品名称">
              <el-input
                v-model="form.productName"
                placeholder="产品名称"
                :readonly="operable"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户编码">
              <el-input
                v-model="form.clientCode"
                placeholder="客户编码"
                :readonly="operable"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称">
              <el-input
                v-model="form.clientName"
                placeholder="客户名称"
                :readonly="operable"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产状态">
              <el-select
                v-model="form.jobStatus"
                placeholder="生产状态"
                clearable
                style="width: 100%"
                :readonly="operable"
              >
                <el-option
                  v-for="dict in jobStatusOptions"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :readonly="operable"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">生产流程</el-divider>
        <el-row>
          <el-col :span="24">
            <el-table :data="form.progressList">
              <el-table-column label="序号" type="index" width="80" align="center" />
              <el-table-column
                label="流程名称"
                prop="progressName"
                align="center"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="流程编码"
                prop="progressCode"
                align="center"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="开始时间"
                prop="startTime"
                align="center"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="完成时间"
                prop="endTime"
                align="center"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="操作人"
                prop="operBy"
                align="center"
                :show-overflow-tooltip="true"
              />
              <el-table-column
                label="操作时间"
                prop="operTime"
                align="center"
                :show-overflow-tooltip="true"
              />
              <el-table-column label="结果" prop="progressResult" align="center">
                <template slot-scope="scope">
                  <el-tag type="danger" v-if="scope.row.progressResult == '0'"
                    >不合格</el-tag
                  >
                  <el-tag type="success" v-if="scope.row.progressResult == '1'"
                    >合格</el-tag
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProgress,
  getProgressByJobId,
  deleteProgress,
} from "@/api/mes/pro/machineryJob";

export default {
  name: "Index",
  dicts: [],
  data() {
    return {
      operable: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 生产状态字典
      jobStatusOptions: [
        {
          value: "WORKING",
          label: "进行中",
        },
        {
          value: "FINISHED",
          label: "已完成",
        },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workorderCode: undefined,
        workorderName: undefined,
        productCode: undefined,
        productName: undefined,
        machineryName: undefined,
        jobStatus: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listProgress({
        ...this.queryParams,

        startTime: this.dateRange[0],
        endTime: this.dateRange[1],
      }).then((response) => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        machineryName: undefined,
        jobProgressName: undefined,
        workorderCode: undefined,
        workorderName: undefined,
        productCode: undefined,
        productName: undefined,
        clientCode: undefined,
        clientName: undefined,
        jobStatus: undefined,
        remark: undefined,
        progressList: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.jobId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum;
      let pageSize = this.queryParams.pageSize;
      return index + 1 + (pageNum - 1) * pageSize;
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const jobId = row.jobId || this.ids;
      getProgressByJobId(jobId).then((response) => {
        this.form = response.data;
        this.operable = true;
        this.open = true;
        this.title = "查看详情";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const jobId = row.jobId || this.ids;
      this.$modal
        .confirm('是否确认删除作业编码为"' + row.jobCode + '"的数据项？')
        .then(function () {
          return deleteProgress(jobId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "mes/pro/machineryJob/exportExcel",
        {
          ...this.queryParams,
          startTime: this.dateRange[0],
          endTime: this.dateRange[1],
        },
        `生产记录.xlsx`
      );
    },
  },
};
</script>
