<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="测量批次" prop="measurementBatch">
        <el-input v-model="queryParams.measurementBatch" placeholder="请输入测量批次" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发动机型号" prop="engineModel">
        <el-input v-model="queryParams.engineModel" placeholder="请输入发动机型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发动机状态" prop="engineStatus">
        <el-select v-model="queryParams.engineStatus" placeholder="请选择发动机状态" clearable>
          <el-option v-for="(item, index) in engineStatusOptions" :key="index" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="角度" prop="angle">
        <el-select v-model="queryParams.angle" placeholder="请选择角度" clearable>
          <el-option v-for="item in angleOptions" :key="item" :label="item" :value="item" />
        </el-select>
      </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="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['meas:radiation: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="['meas:radiation:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['meas:radiation:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="radiationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="测量批次" align="center" prop="measurementBatch" />
      <el-table-column label="发动机型号" align="center" prop="engineModel" />
      <el-table-column label="发动机状态" align="center" prop="engineStatus" />
      <el-table-column label="发动机角度" align="center" prop="angle" />
      <el-table-column label="型号" align="center" prop="engineModel" />
      <el-table-column label="镜头视场角" align="center" prop="lensFView" />
      <el-table-column label="增益" align="center" prop="gain" />
      <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" v-hasPermi="['meas:envData:edit']"
            @click="checkView(scope.row)">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['meas:radiation:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['meas:radiation: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 :close-on-click-modal="false" :title="title" :visible.sync="open" width="70%" append-to-body>
      <dialogContent ref="radiationContent" :radiationForm="radiationForm" @submit-form="submitForm" :form="form" @cancel="cancel"
        :isCheck="isCheck" />
    </el-dialog>
  </div>
</template>

<script>
import { listRadiation, getRadiation, delRadiation, updateRadiation, addRadiation, getRadiationByEnvData } from "@/api/meas/radiation";
import { angleOption } from "@/api/meas/angle";
import { statusOption } from "@/api/meas/status";
import dialogContent from "@/views/meas/components/dialogContent";
import { getEnvData, updateEnvData } from "@/api/meas/envData";

export default {
  name: "Radiation",
  components: { dialogContent },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      measurementBatchIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 光谱辐射表格数据
      radiationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        measurementBatch: null,
        engineModel: null,
        engineStatus: null,
        angle: null,
      },
      // 表单参数
      form: {},
      radiationForm: {},
      // 发动机状态
      engineStatusOptions: [],
      // 发动机角度
      angleOptions: [],
      isCheck: false,
    };
  },
  created() {
    this.getList();
    this.getAngleOption();
    this.getStatusOption();
  },
  methods: {
    /** 查询光谱辐射列表 */
    getList() {
      this.loading = true;
      listRadiation(this.queryParams).then(response => {
        this.radiationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getAngleOption() {
      this.loading = true;
      angleOption().then((response) => {
        this.angleOptions = response.data;
        this.loading = false;
      });
    },
    getStatusOption() {
      this.loading = true;
      statusOption().then((response) => {
        this.engineStatusOptions = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.radiationForm = {
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        id: null,
        measurementBatchId: null,
        engineModel: null,
        geometryFiles: null,
        geometryImage: null,
        measurementBatch: null,
        engineStatus: null,
        angle: null,
        model: null,
        lensFView: null,
        gain: null,
        backgroundData: null,
        calTemp: null,
        calCurve: null,
        rawData: null,
        radiance1: null,
        radiance2: null
      };
      this.resetForm("radiationForm");
    },
    checkView(row) {
      this.handleUpdate(row)
      this.isCheck = true
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.measurementBatchIds = selection.map(item => item.measurementBatchId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isCheck = false
      const id = row.measurementBatchId || this.measurementBatchIds
      getEnvData(id).then((response) => {
        this.form = response.data;
      });
      getRadiationByEnvData(id).then(response => {
        this.radiationForm = response.data;
        this.open = true;
        this.title = this.isCheck ? '查看光谱辐射数据' : "修改光谱辐射数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      const promises = [];
      let successMessages = [];
      let failureMessages = [];
      promises.push(
        (this.form.id ? updateEnvData(this.form) : addEnvData(this.form))
          .then(() => {
            successMessages.push(`测量环境数据`);
          })
          .catch(() => {
            failureMessages.push(`测量环境数据`);
          }),
        (this.radiationForm.id ? updateRadiation(this.radiationForm) : addRadiation(this.radiationForm))
          .then(() => {
            successMessages.push(`光谱辐射数据`);
          })
          .catch(() => {
            failureMessages.push(`光谱辐射数据`);
          })
      );
      Promise.all(promises)
        .then(() => {
          let message = ''
          if (successMessages.length > 0) {
            message += `${successMessages.join('、')}操作成功； `
            this.$modal.msgSuccess(message);
          }
          if (failureMessages.length > 0) {
            message += `${failureMessages.join('、')}操作失败`
            this.$modal.msgWarning(message);
          }
          this.open = false;
          this.getList();
        })
        .catch(() => {
          this.$modal.msgError("操作过程中发生了错误");
          this.open = false;
        });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.measurementBatchId || this.measurementBatchIds;
      this.$modal.confirm('是否确认删除光谱辐射编号为"' + ids + '"的数据项？').then(function () {
        return delRadiation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('meas/radiation/export', {
        ...this.queryParams
      }, `radiation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
::v-deep .el-form-item .el-select {
  width: 200px;
}

::v-deep .el-form-item .el-input {
  width: 200px;
}

::v-deep .el-form-item .upload-file {
  width: 200px;
}
</style>
