<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="测量批次" prop="measurementBatch" label-width="90px">
        <el-input v-model="queryParams.measurementBatch" placeholder="请输入测量批次" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发动机型号" prop="engineModel" label-width="90px">
        <el-input v-model="queryParams.engineModel" placeholder="请输入发动机型号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="发动机状态" prop="engineStatus" label-width="90px">
        <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="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['meas:envData: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="['meas:envData: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:envData: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:envData:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="envDataList" @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" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="checkView(scope.row)"
            v-hasPermi="['meas:envData:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['meas:envData:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['meas:envData: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 :before-close="handleClose">
      <dialogContent ref="dialogContentRef" :form="form" :TIForm="TIForm" :TransmittanceForm="TransmittanceForm"
        :envSaveBtn="true" :radiationForm="radiationForm" :isCheck="isCheck" :addFlag="addFlag" :footShow="footShow" @env-data-save="envDataSave"
        @submit-form="submitForm" @cancel="cancel" />
    </el-dialog>

  </div>
</template>

<script>
import {
  listEnvData,
  getEnvData,
  delEnvData,
  addEnvData,
  updateEnvData,
} from "@/api/meas/envData";
import {
  addRadiation,
  getRadiationByEnvData,
  updateRadiation,
} from "@/api/meas/radiation";
import {
  addThermalImagerTesDdata,
  updateThermalImagerTesDdata,
  getTIDataByEnvData
} from "@/api/meas/thermalImagerTesDdata";
import {
  addTransmissionRate,
  updateTransmissionRate,
  getTRateByEnvData
} from "@/api/meas/transmissionRate";
import { angleOption } from "@/api/meas/angle";
import { statusOption } from "@/api/meas/status";

import dialogContent from "@/views/meas/components/dialogContent";

export default {
  name: "EnvData",
  components: { dialogContent },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测量环境数据表格数据
      envDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        measurementBatch: null,
        engineModel: null,
        engineStatus: null,
        angle: null,
      },
      isCheck: false,
      // 表单参数
      form: {},
      radiationForm: {},
      rules: {},
      TIForm: {},
      TransmittanceForm: {},
      // 发动机状态
      engineStatusOptions: [],
      // 发动机角度
      angleOptions: [],
      // 热像仪测试
      lensFViewOptions: [],
      filterNoOptions: [],
      fpsOptions: [],
      spectralBandOptions: [],
      measureBatchOptions: [],
      engineOptions: [],
      engineModel: "",
      fileShowData: [],
      disabled: false,
      addFlag: false,
      formIsChange: false,
      footShow:true
    };
  },
  created() {
    this.getList();
    this.getAngleOption();
    this.getStatusOption();
  },
  methods: {
    /** 查询测量环境数据列表 */
    getList() {
      this.loading = true;
      listEnvData(this.queryParams).then((response) => {
        this.envDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取角度Option
    getAngleOption() {
      this.loading = true;
      angleOption().then((response) => {
        this.angleOptions = response.data;
        this.loading = false;
      });
    },
    // 获取状态Option
    getStatusOption() {
      this.loading = true;
      statusOption().then((response) => {
        this.engineStatusOptions = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleClose(){
      this.$refs.dialogContentRef.handleCloseBefore()
      this.cancel()
    },
    // 保存环境参数
    envDataSave() {
      if (this.form.id != null) {
        updateEnvData(this.form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        });
      } else {
        addEnvData(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.addFlag = false
          // this.handleUpdate(response.data)
          this.radiationReset();
          this.TIFormReset();
          this.transmittanceReset()
          this.radiationForm.measurementBatchId = response.data.id
          this.TIForm.measurementBatchId = response.data.id
          this.TransmittanceForm.measurementBatchId = response.data.id
          this.getList();
          this.footShow = true
          getEnvData(response.data.id).then((response) => {
            this.form = response.data;
          });
        });
      }
    },
    // 表单重置
    reset() {
      this.formReset();
      this.radiationReset();
      this.TIFormReset();
      this.transmittanceReset();
    },
    // form表单重置
    formReset() {
      this.form =
      {
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        id: null,
        measurementBatchId: null,
        measurementBatch: null,
        engineModel: null,
        geometryFiles: null,
        geometryImage: null,
        engineStatus: null,
        angle: null,
        turbinePostTemp: null,
        testDistance: null,
        time: null,
        location: null,
        atmosPress: null,
        temperature: null,
        windSpeed: null,
        humidity: null,
        concentration: null
      }
      this.resetForm("form");
    },
    // radiation表单重置
    radiationReset() {
      this.radiationForm = {
        measurementBatchId: null,
        model: null,
        lensFView: null,
        gain: null,
        backgroundData: null,
        calTemp: null,
        calCurve: null,
        rawData: null,
        radiance1: null,
        radiance2: null,
      }
      this.resetForm("radiationReset");
    },
    TIFormReset() {
      this.TIForm = {
        measurementBatchId: null,
        model: null,
        lensFView: null,
        filterNo: null,
        fps: null,
        spectralBand: null,
        integrationTime: null,
        tempCal: null,
        radiance: null,
        radIllumination: null,
        radTemp: null,
      };
      this.resetForm("TIForm");
    },
    transmittanceReset() {
      this.TransmittanceForm = {
        measurementBatchId: null,
        rawData: null,
        curve1: null,
        curve2: null
      };
      this.resetForm("TransmittanceForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isCheck = false
      this.addFlag = true
      this.footShow = false
      this.open = true;
      this.title = "添加测量环境数据";
      this.radiationForm = null
      this.TIForm = null
      this.TransmittanceForm = null
    },
    checkView(row) {
      this.handleUpdate(row)
      this.isCheck = true
      this.addFlag = true
      this.footShow = false
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isCheck = false
      this.addFlag = false
      this.footShow = true
      const id = row.id;
      getRadiationByEnvData(id).then((response) => {
        if (response.data) {
          this.$refs.dialogContentRef.getcalTempData(response.data.calTemp)
          this.radiationForm = response.data;
        } else {
          this.radiationReset()
          this.radiationForm.measurementBatchId = id
        }
      });
      getTIDataByEnvData(id).then((response) => {
        if (response.data) {
          this.TIForm = response.data;
        } else {
          this.TIFormReset()
          this.TIForm.measurementBatchId = id
        }
      });
      getTRateByEnvData(id).then((response) => {
        if (response.data) {
          this.TransmittanceForm = response.data;
        } else {
          this.transmittanceReset()
          this.TransmittanceForm.measurementBatchId = id
        }
      });
      getEnvData(row.id).then((response) => {
        if (response.data) {
          this.form = response.data;
          this.open = true;
          this.title = this.isCheck ? '查看测量环境数据' : "修改测量环境数据";
        } else {
          this.formReset()
        }
      });
    },
    /** 提交按钮 */
    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(`光谱辐射数据`);
          }),
        (this.TIForm?.id ? updateThermalImagerTesDdata(this.TIForm) : addThermalImagerTesDdata(this.TIForm))
          .then(() => {
            successMessages.push(`热像仪数据`);
          })
          .catch(() => {
            failureMessages.push(`热像仪数据`);
          }),
        (this.TransmittanceForm?.id ? updateTransmissionRate(this.TransmittanceForm) : addTransmissionRate(this.TransmittanceForm))
          .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.id || this.ids;
      this.$modal
        .confirm('是否确认删除测量环境数据编号为"' + ids + '"的数据项？')
        .then(function () {
          return delEnvData(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "meas/envData/export",
        {
          ...this.queryParams,
        },
        `envData_${new Date().getTime()}.xlsx`
      );
    },

  },
};
</script>
<style scoped lang="scss">
.box {
  border: 1px solid #dcdfe6;
  border-radius: 10px;
  padding: 20px 15px 0px 15px;
  margin-top: 20px;
  position: relative;

  .title {
    padding: 10px;
    background-color: white;
    position: absolute;
    top: -20px;
    font-weight: 700;
    font-size: 1rem;
  }
}
</style>
