<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="type" label-width="110">
        <el-select v-model="queryParams.deptId" placeholder="请选择综合保税区" clearable filterable class="se">
          <el-option v-for="item in deptList" :key="item.id" :label="item.deptName" :value="item.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="年份" prop="type" label-width="110">
        <!-- <el-select v-model="queryParams.year" placeholder="请选择年份" clearable filterable class="se">
          <el-option v-for="item in nearYearList" :key="item" :label="item" :value="item" />
        </el-select> -->
        <el-date-picker
          v-model="queryParams.year"
          type="year"
          value-format="yyyy"
          placeholder="选择年">
        </el-date-picker>
      </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="['rank:RANK: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="['rank:RANK: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="['rank:RANK: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="['rank:RANK:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  :data="RANKList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="综合保税区" align="center" prop="deptName">
        <template slot-scope="scope">
          {{scope.row.deptName}}
        </template>
      </el-table-column>
      <el-table-column label="年份" align="center" prop="year">
        <template slot-scope="scope">
          {{scope.row.year}}
        </template>
      </el-table-column>
      <el-table-column label="全国进出口值排名" align="center" prop="ranking">
        <template slot-scope="scope">
          {{scope.row.ranking == 0 || scope.row.ranking==null?'-':scope.row.ranking}}
        </template>
      </el-table-column>
      <el-table-column label="中西部绩效排名" align="center" prop="midwestRanking">
        <template slot-scope="scope">
          {{scope.row.midwestRanking==0 || scope.row.midwestRanking==null?'-':scope.row.midwestRanking}}
        </template>
      </el-table-column>
      <el-table-column label="全国绩效排名" align="center" prop="nationalPerformanceRanking">
        <template slot-scope="scope">
          {{scope.row.nationalPerformanceRanking==0|| scope.row.nationalPerformanceRanking==null?'-':scope.row.nationalPerformanceRanking}}
        </template>
      </el-table-column>
      <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="['rank:RANK:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['rank:RANK: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item prop="deptId" label="综合保税区名称">
          <el-select v-model="form.deptId" filterable default-first-option clearable placeholder="请选择综合保税区">
            <el-option
              v-for="item in deptList"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="year" label="年份">
          <el-select v-model="form.year" filterable default-first-option clearable placeholder="请选择年份">
            <el-option
              v-for="(item,i) in nearYearList"
              :key="i"
              :label="item"
              :value="item">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop="ranking" label="全国进出口值排名">
          <el-input  v-model="form.ranking" placeholder="请输入排名" ></el-input>
        </el-form-item>

        <el-form-item prop="midwestRanking" label="中西部绩效排名">
          <el-input  v-model="form.midwestRanking" placeholder="请输入排名" ></el-input>
        </el-form-item>

        <el-form-item prop="nationalPerformanceRanking" label="全国绩效排名">
          <el-input  v-model="form.nationalPerformanceRanking" placeholder="请输入排名" ></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 { listRANK, getRANK, delRANK, addRANK, updateRANK } from "@/api/rank/RANK";
import { listDept } from '@/api/system/dept'

export default {
  name: "RANK",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广西保税区排名表格数据
      RANKList: [],
      // 保税区列表
      deptList: [],
      // 近年年份列表
      nearYearList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        deptId: null,
        year: null,
        ranking: null,
        createTime: null,
        createBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptId: [
          { required: true, message: "综合保税区ID不能为空", trigger: "blur" }
        ],
        year: [
          { required: true, message: "年份不能为空", trigger: "blur" }
        ],
        ranking: [
          { required: true, message: "全国进出口值排名不能为空", trigger: "blur" },
          { trigger: 'blur', validator: this.validateNumber }
        ],
        midwestRanking: [
          { required: true, message: "中西部绩效排名不能为空", trigger: "blur" },
          { trigger: 'blur', validator: this.validateNumber }
        ],
        nationalPerformanceRanking: [
          { required: true, message: "全国绩效排名不能为空", trigger: "blur" },
          { trigger: 'blur', validator: this.validateNumber }
        ]
      }
    };
  },
  created() {
    this.getList();

  },
  methods: {
    /** 查询广西保税区排名列表 */
    getList() {
      this.loading = true;
      listRANK(this.queryParams).then(response => {
        this.RANKList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.getDeptList();
      this.getNearYear();
    },
    // 获取保税区列表
    getDeptList() {
      this.loading = true;
      listDept({parentId: '100'}).then(response => {
        this.deptList = response.data
        this.loading = false;
      });
    },
    // 获取近年年份
    getNearYear() {
      // let currentDate = new Date();
      // let curYear = currentDate.getFullYear();

      // // 获取近三年
      // let nearYearArr = [];
      // nearYearArr.push(curYear)
      // for (let i = 1; i <= 10; i++) {
      //   nearYearArr.push(curYear - i + '');
      // }

      // this.nearYearList = nearYearArr

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        year: null,
        ranking: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.deptId = '';
      this.queryParams.year = '';

      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
      getRANK(id).then(response => {
        this.form = response.data;
        this.form.deptId = Number(this.form.deptId)
        this.open = true;
        this.title = "修改广西保税区排名";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRANK(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRANK(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 delRANK(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rank/RANK/export', {
        ...this.queryParams
      }, `RANK_${new Date().getTime()}.xlsx`)
    },
    // 校验参数
    validateNumber(rule, value, callback) {
      if (value === '') {
        callback(new Error('请输入整数'));
      } else if (!Number.isInteger(Number(value))) {
        callback(new Error('请输入整数'));
      } else if (Number(value) < 0) {
        callback(new Error('不能小于0'));
      } else {
        callback();
      }
    }
  }
};
</script>
