<!-- 工种管理 -->
<template>
  <div class="work-type">
    <template v-if="!addOrUpdateVisible && !detailVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryData()">
        <el-form-item label="任务号">
          <el-input v-model="dataForm.orderCode" placeholder="" clearable />
        </el-form-item>
        <el-form-item label="图号">
          <el-input v-model="dataForm.mapNumber" placeholder="" clearable />
        </el-form-item>
        <el-form-item label="产品名称">
          <el-input v-model="dataForm.productName" placeholder="" clearable />
        </el-form-item>
        <el-form-item>
          <el-button @click="queryData()">查询</el-button>
          <el-button type="primary" @click="add()">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%">
        <el-table-column label="序号" type="index" width="100" align="center">
        </el-table-column>
        <el-table-column prop="orderCode" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="任务号">
        </el-table-column>
        <el-table-column prop="mapNumber" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="图号">
        </el-table-column>
        <el-table-column prop="productName" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="产品名称">
        </el-table-column>
        <el-table-column prop="productNumber" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="编号">
        </el-table-column>
        <el-table-column prop="temperature" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="温度">
        </el-table-column>
        <el-table-column prop="humidity" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="湿度">
        </el-table-column>
        <el-table-column prop="deliveryDate" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="备注">
        </el-table-column>
        <el-table-column prop="inspectorName" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="检验员">
        </el-table-column>
        <el-table-column prop="state" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="状态">
          <template slot-scope="scope">
            <span>{{ formatState(scope.row) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="isQualified" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="是否合格">
          <template slot-scope="scope">
            <span>{{ formatIsQualified(scope.row) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="inspectionDate" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="校验日期">
        </el-table-column>
        <el-table-column prop="creatName" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="申请人">
        </el-table-column>
        <el-table-column prop="createTime" header-align="center" align="center" min-width="160"
          :show-tooltip-when-overflow="true" label="申请日期">
        </el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="220" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="detailHandle(scope.row)">查看</el-button>
            <el-button v-if="scope.row.state == 1" type="text" size="small"
              @click="importHandle(scope.row)">导入报告</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </template>
    <!-- 弹窗 -->
    <onlinecheck-add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @onChose="onChose" @refreshDataList="getDataList"/>

    <onlinecheck-detail v-if="detailVisible" ref="detail" @onChose="onChose" />

    <el-dialog title="导入" :visible.sync="importVisible">
      <el-upload class="upload-demo" ref="upload" :on-remove="handleRemove" action="#" accept="" :limit="1"
        :file-list="fileList" :auto-upload="false" :http-request="handleUpload" v-loading="importLoading">
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <el-button size="small" type="success" @click="submitUpload">开始上传</el-button>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import OnlinecheckAddOrUpdate from "@/views/modules/quality/onlinecheck-add-or-update";
import OnlinecheckDetail from "@/views/modules/quality/onlinecheck-detail";
export default {
  name: "plan",
  components: {
    OnlinecheckAddOrUpdate,
    OnlinecheckDetail,
  },
  data() {
    return {
      addOrUpdateVisible: false,
      detailVisible: false,
      importVisible: false,
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      optionsLevel: [],
      importLoading: false,
      importData: {},
      fileList: [],
      stateOption: [
        { label: "待检验", value: "1" },
        { label: "已检验", value: "2" },
      ],
      isQualifiedOption: [
        { label: "待检验", value: "0" },
        { label: "合格", value: "1" },
        { label: "不合格", value: "2" },
      ],
      lastSubmitItem: null,
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    onChose() {
      this.addOrUpdateVisible = false;
      this.detailVisible = false;
    },
    // 查询
    queryData() {
      this.pageIndex = 1;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.addOrUpdateVisible = false;

      this.$http({
        url: this.$http.adornUrl(`/biz-service/onlineDetection/list`),
        method: "get",
        params: this.$http.adornParams({
          current: this.pageIndex,
          size: this.pageSize,
          orderCode: this.dataForm.orderCode,
          mapNumber: this.dataForm.mapNumber,
          productName: this.dataForm.productName,
        }),
      }).then(({ data }) => {
        this.dataListLoading = false;
        if (data && data.code === "200") {
          this.dataList = data.data.records;
          this.totalPage = Number(data.data.total);
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    add() {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init();
      });
    },
    detailHandle(item) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.detail.init(item.onlineDetectionId);
      });
    },
    formatState(row) {
      if (!row.state) return "";

      return this.getStateStr(row.state);
    },
    getStateStr(state) {
      let option = this.stateOption.find((t) => t.value === state);
      if (option != null) {
        return option.label;
      }
      return "";
    },
    formatIsQualified(row) {
      if (!row.isQualified) return "";

      return this.getIsQualifiedStr(row.isQualified);
    },
    getIsQualifiedStr(state) {
      let option = this.isQualifiedOption.find((t) => t.value === state);
      if (option != null) {
        return option.label;
      }
      return "";
    },
    importHandle(item) {
      this.fileList = [];
      this.importVisible = true;
      this.importData = item;
    },
    // 上传
    submitUpload() {
      this.$refs.upload.submit();
      this.importVisible = false
      this.getDataList()
    },
    // 移除
    handleRemove(file, fileList) {
      this.$emit("input", fileList);
    },
    handleUpload(file) {
      if (file == null) {
        this.$message.error("请上传文件");
        return;
      }

      this.importLoading = true;

      const formData = new FormData();
      formData.append("importFile", file.file);
      formData.append("onlineDetectionId", this.importData.onlineDetectionId);
      this.$http({
        url: this.$http.adornUrl(`/biz-service/onlineDetection/importExcel`),
        method: "POST",
        data: formData,
        timeout: 0,
      }).then(({ data }) => {
        this.importLoading = false;
        if (data && data.code === "200") {
          this.$message({
            message: "导入成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.importVisible = false;
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
  },
};
</script>

<style scoped></style>
