<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="异常类型" prop="abnormalityType">
        <el-select v-model="queryParams.abnormalityType" placeholder="请选择异常类型" clearable size="small">
          <el-option
            v-for="dict in abnormalityTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属企业" prop="beyondEnterpriseId">
        <el-select v-model="queryParams.beyondEnterpriseId" placeholder="请选择所属企业" clearable size="small">
          <el-option
            v-for = "dict in enterpriseList"
            :key = "dict.enterpriseId"
            :label = "dict.enterpriseName"
            :value = "dict.enterpriseId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="气瓶编号" prop="bottleCode">
        <el-input
          v-model="queryParams.bottleCode"
          placeholder="请输入气瓶编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核实状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择核实状态" clearable size="small">
          <el-option label="未核实" value="0" />
          <el-option label="无需处理" value="1" />
          <el-option label="已处理" value="2" />
        </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"-->
        <!--&gt;新增</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="['gasBottleTrack:abnormality:edit']"-->
        <!--&gt;修改</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="['gasBottleTrack:abnormality:remove']"-->
        <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['gasBottleTrack:abnormality:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="abnormalityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="气瓶编号" align="center" prop="bottleCode" />
      <el-table-column label="异常类型" align="center" prop="abnormalityType" :formatter="abnormalityTypeFormat" />
      <el-table-column label="所属企业" align="center" prop="enterpriseName" />
      <el-table-column label="异常信息" align="center" prop="message" />
      <el-table-column label="核实状态" align="center" prop="status"  >
        <template slot-scope="scope">
          <span :style="scope.row.status=='0'?'color: #ff0000':'color: rgb(48, 180, 107)'">{{abnormalityStatusFormat(scope.row,null)}}</span>
        </template>
        :formatter="abnormalityStatusFormat"
      </el-table-column>
      <el-table-column label="处理方式" align="center" prop="content" >
        <template slot-scope="scope">
          <span v-if="scope.row.content">{{scope.row.content}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="处理人" align="center" prop="people" >
        <template slot-scope="scope">
          <span v-if="scope.row.people">{{scope.row.people}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>/
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status=='0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gasBottleTrack:abnormality:edit']"
          >处置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gasBottleTrack:abnormality: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="abnormalityType">
          <el-select v-model="form.abnormalityType" placeholder="请选择异常类型" disabled>
            <el-option
              v-for="dict in abnormalityTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属企业" prop="beyondEnterpriseId">
          <el-input v-model="form.enterpriseName" placeholder="请输入所属企业" disabled/>
        </el-form-item>
        <el-form-item label="气瓶编号" prop="bottleCode">
          <el-input v-model="form.bottleCode" placeholder="请输入气瓶编号" disabled/>
        </el-form-item>
        <el-form-item label="异常信息" prop="message">
          <el-input v-model="form.message" placeholder="请输入异常信息" disabled/>
        </el-form-item>
        <el-form-item label="核实状态">
          <el-select v-model="form.status" placeholder="请选择核实状态"  size="small">
            <el-option
              v-for="dict in abnormalityStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理方式">
          <el-input type="textarea" v-model="form.content" placeholder="请输入处理方式" />
        </el-form-item>
        <el-form-item label="处理人" prop="people">
          <el-input v-model="form.people" 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 { listAbnormality, getAbnormality, delAbnormality, addAbnormality, updateAbnormality, exportAbnormality } from "@/api/gasBottleTrack/abnormality";
import Editor from '@/components/Editor';
import {enterpriseList } from "@/api/system/planInfo";

export default {
  name: "Abnormality",
  components: {
    Editor,
  },
  data() {
    return {
      statuslist:[
        {
          label:'未处理',
          value:'0'
        },
        {
          label:'无需处理',
          value:'1'
        },
        {
          label:'已处理',
          value:'2'
        }
      ],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 气瓶异常分析表格数据
      abnormalityList: [],
      enterpriseList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 异常类型字典
      abnormalityTypeOptions: [],
      abnormalityStatusOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        abnormalityType: null,
        beyondEnterpriseId: null,
        bottleCode: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getEnterpriseList()
    this.getDicts("t_abnormality_type").then(response => {
      this.abnormalityTypeOptions = response.data;
    });

    this.getDicts("t_abnormality_status").then(response => {
      this.abnormalityStatusOptions = response.data;
  });


  },
  methods: {
    //公司列表
    getEnterpriseList() {
      enterpriseList({pageNum: 1,
        pageSize: 100}).then(response => {
        this.enterpriseList = response.data;
    });
    },
    /** 查询气瓶异常分析列表 */
    getList() {
      this.loading = true;
      listAbnormality(this.queryParams).then(response => {
        this.abnormalityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 异常类型字典翻译
    abnormalityTypeFormat(row, column) {
      return this.selectDictLabel(this.abnormalityTypeOptions, row.abnormalityType);
    },

    abnormalityStatusFormat(row, column) {
      return this.selectDictLabel(this.abnormalityStatusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        abnormalityId: null,
        abnormalityType: null,
        beyondEnterpriseId: null,
        bottleCode: null,
        message: null,
        status: "0",
        content: null,
        people: null,
        createTime: 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.abnormalityId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加气瓶异常分析";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const abnormalityId = row.abnormalityId || this.ids
      getAbnormality(abnormalityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改气瓶异常分析";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.abnormalityId != null) {
            updateAbnormality(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAbnormality(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const abnormalityIds = row.abnormalityId || this.ids;
      this.$confirm('是否确认删除气瓶异常分析编号为"' + abnormalityIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAbnormality(abnormalityIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有气瓶异常分析数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportAbnormality(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style lang="scss" scoped>

  ::v-deep .el-select{
    width: 100%;
  }
</style>
