<template>
  <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
        <el-form-item label="指标名称" prop="quotaName">
          <el-input v-model="queryParams.quotaName"></el-input>
        </el-form-item>
        <el-form-item label="指标分类" prop="normName">
          <el-select v-model="queryParams.normName" clearable filterable>
            <el-option value="加分项">加分项</el-option>
            <el-option value="减分项">减分项</el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            v-hasPermi="['business:QUOTAFEE:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['business:QUOTAFEE:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['business:QUOTAFEE:remove']"
          >删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table :data="QUOTAFEEList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column label="指标分类" width="85" align="center" prop="normName">
        </el-table-column>
        <!--<el-table-column label="保税区id" align="center" prop="bondedId">
        </el-table-column>-->
        <el-table-column label="指标名称" width="250" align="center" prop="quotaName">
        </el-table-column>
        <el-table-column label="指标内容" width="400" align="center" prop="content">
        </el-table-column>
        <el-table-column label="得分" width="85" align="center" prop="score">
        </el-table-column>
        <el-table-column label="确认方式" width="200" align="center" prop="confirmWay">
        </el-table-column>
        <el-table-column label="项次" width="85" align="center" prop="sort">
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['business:QUOTAFEE:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['business:QUOTAFEE: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" :close-on-click-modal="false" width="900px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">

          <el-form-item label-width="100px" prop="normId" label="指标分类">
            <el-radio-group v-model="form.normId">
              <el-radio label="1">加分项</el-radio>
              <el-radio label="2">减分项</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label-width="100px" prop="sort" label="项次">
            <el-input v-model="form.sort"></el-input>
          </el-form-item>
          <el-form-item label-width="100px" prop="quotaName" label="指标名称">
            <el-input v-model="form.quotaName"></el-input>
          </el-form-item>
          <el-form-item label-width="100px" prop="content" label="指标内容">
            <!--<editor v-model="form.content" :min-height="20"/>-->
            <el-input type="textarea" v-model="form.content" rows="5"></el-input>
          </el-form-item>
          <el-form-item label-width="100px" prop="score" label="分值">
            <el-input v-model="form.score"></el-input>
          </el-form-item>
          <el-form-item label-width="100px" prop="confirmWay" label="确认方式">
            <el-input v-model="form.confirmWay"></el-input>
          </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 { listQUOTAFEE, getQUOTAFEE, delQUOTAFEE, addQUOTAFEE, updateQUOTAFEE } from "@/api/norm/QUOTAFEE";

export default {
  name: "QUOTAFEE",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 辅助指标表格数据
      QUOTAFEEList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        quotaName: null,
        normName: null
      },
      // 表单参数
      form: {},
      statusArr: [
        { value: '加分项', label: '加分项' },
        { value: '减分项', label: '减分项' }
      ],
      // 表单校验
      rules: {
        normId: [
          { required: true, message: "指标分类不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "项次为数值", trigger: "blur" }
        ],
        quotaName: [
          { required: true, message: "指标名称不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "指标内容不能为空", trigger: "blur" }
        ],
        score: [
          { required: true, message: "得分为数值", trigger: "blur" }
        ],
        confirmWay: [
          { required: true, message: "确认方式不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询辅助指标明细列表 */
    getList() {
      this.loading = true;
      listQUOTAFEE(this.queryParams).then(response => {
        response.rows.forEach((item, index) => {
          response.rows[index].status = item.status == 0 ? false : true;
        });
        this.QUOTAFEEList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        deptId: null,
        normId: null,
        bondedId: null,
        quotaName: null,
        content: null,
        score: null,
        confirmWay: null,
        sort: null,
        status: "0",
        startTime: null,
        stopTime: 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
      getQUOTAFEE(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改辅助指标明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!(parseInt(this.form.sort) == this.form.sort)) {
            this.$message.error("项次必须为整数");
            return;
          }
          if (!(parseInt(this.form.score) == this.form.score)) {
            this.$message.error("得分必须为整数");
            return;
          }

          this.form.status = 1;
          if (this.form.id != null) {
            updateQUOTAFEE(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQUOTAFEE(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 delQUOTAFEE(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/QUOTAFEE/export', {
        ...this.queryParams
      }, `QUOTAFEE_${new Date().getTime()}.xlsx`)
    },
    // 关闭Dialog窗口
    closeHandler() {
      this.$emit("closeHandler");
    }
  }
};
</script>
