<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">排放因子</el-col>
    </el-row>
    <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="['basecfg:emissionFactor: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="['basecfg:emissionFactor: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="['basecfg:emissionFactor: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="['basecfg:emissionFactor:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="factorList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="地区码" align="center" prop="regionCode" />
      <el-table-column label="地区名称" align="center" prop="regionName" />
      <el-table-column label="地区类型" align="center" prop="regionType" :formatter="getRegionType"/>
      <el-table-column label="因子值" align="center" prop="factorValue" />
      <el-table-column label="因子类型" align="center" prop="factorType" :formatter="getFactorType"/>
      <el-table-column label="数据版本" align="center" prop="version" />
      <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-edit" @click="handleUpdate(scope.row)" v-hasPermi="['basecfg:emissionFactor:edit']">
            修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" class="deleteBtn" @click="handleDelete(scope.row)" v-hasPermi="['basecfg:emissionFactor: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="地区码" prop="regionCode">
          <el-input v-model="form.regionCode" placeholder="请输入地区码" />
        </el-form-item>
        <el-form-item label="地区名称" prop="regionName">
          <el-input v-model="form.regionName" placeholder="请输入地区名称" />
        </el-form-item>
        <el-form-item label="因子值" prop="factorValue">
          <el-input v-model="form.factorValue" placeholder="请输入因子值 单位: kgCO2/kWh" />
        </el-form-item>
        <el-form-item label="数据版本" prop="version">
          <el-input v-model="form.version" placeholder="请输入数据版本" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFactor, getFactor, delFactor, addFactor, updateFactor } from "@/api/basecfg/cacfg";

export default {
  name: "Factor",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排放因子维表格数据
      factorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        regionCode: null,
        regionName: null,
        regionType: null,
        factorValue: null,
        factorType: null,
        version: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        regionCode: [
          { required: true, message: "地区码不能为空", trigger: "blur" }
        ],
        regionName: [
          { required: true, message: "地区名称不能为空", trigger: "blur" }
        ],
        regionType: [
          { required: true, message: "地区类型不能为空", trigger: "change" }
        ],
      },
      regionTypeData: [
        { regionType:0 },
        { regionType:1 },
        { regionType:2 },
        { regionType:3 }
      ],
      factorTypeData: [
        { factorType:1 },
        { factorType:2 },
        { factorType:3 }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询排放因子维列表 */
    getList() {
      this.loading = true;
      listFactor(this.queryParams).then(response => {
        this.factorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        regionCode: null,
        regionName: null,
        regionType: null,
        factorValue: null,
        factorType: null,
        version: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    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.open = true;
      this.title = "添加排放因子维";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFactor(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排放因子维";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFactor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFactor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除排放因子维编号为"' + ids + '"的数据项？').then(function() {
        return delFactor(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ems/basecfg/emissionFactor/export', {
        ...this.queryParams
      }, `factor_${new Date().getTime()}.xlsx`)
    },
    getRegionType(row, column, cellValue, index) {
      const regionTypeMap = {
        0: '国家',
        1: '地区',
        2: '省份',
        3: '城市'
      };
      return regionTypeMap[cellValue] || '未知';
    },
    getFactorType(row, column, cellValue, index) {
      const factorType = {
        1: '电力平均二氧化碳排放因子',
        2: '电力平均二氧化碳排放因子（不含市场化交易的非化石能源电量）',
        3: '化石能源电力二氧化碳排放因子'
      };
      return factorType[cellValue] || '未知';
    }
  }
};
</script>
