<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-select v-model="queryParams.acceptRuleId" filterable placeholder="请搜索并选择">
        <el-option v-for="item in allRulesList"
                   :value="item.acceptRuleId"
                   :key="item.acceptRuleId"
                   :label="item.ruleStr">
        </el-option>
      </el-select>

<!--      <el-form-item label="创建时间" prop="createAt">-->
<!--        <el-input-->
<!--          v-model="queryParams.createAt"-->
<!--          placeholder="请输入创建时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="更新时间" prop="updateAt">-->
<!--        <el-input-->
<!--          v-model="queryParams.updateAt"-->
<!--          placeholder="请输入更新时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </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="['mall:mallAcceptRule: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="['mall:mallAcceptRule: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="['mall:mallAcceptRule: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="['mall:mallAcceptRule:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallAcceptRuleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="录取原则" align="center" prop="ruleStr" width="180" />
      <el-table-column label="投档公式" align="center" prop="ruleCountStr" width="180" />
      <el-table-column label="文化分满分" align="center" prop="fullCulture" width="180" />
      <el-table-column label="专业分满分" align="center" prop="fullMajor" width="180" />
      <el-table-column label="该录取原则满分" align="center" prop="fullScore" width="180" />
      <el-table-column label="转换系数" align="center" prop="changeRate" width="120" />
      <el-table-column label="创建时间" align="center" prop="createAt" width="180" />
      <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="['mall:mallAcceptRule:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:mallAcceptRule: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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="分类" prop="ruleClass">
          <el-radio-group v-model="form.ruleClass" >
            <el-radio v-for="(item, index) in ruleClassOptions"
                      :key="index"
                      :label="item.value">
              {{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="录取原则" prop="ruleStr">
          <el-input v-model="form.ruleStr" placeholder="请输入录取原则" />
          <span style="font-size: 13px">(格式举例:文化/750*300+专业/300*700)</span>
          <span style="font-size: 13px">(注:如果存在百分比数值需替换成小数类型)</span>
        </el-form-item>

        <el-form-item label="投档公式" prop="ruleCountStr" v-if="form.acceptRuleId !== null">
          <el-input v-model="form.ruleCountStr" placeholder="投档公式" :disabled="form.acceptRuleId !== null" />
        </el-form-item>

        <el-form-item label="文化分满分" prop="fullCulture" >
          <el-input-number v-model="form.fullCulture" :min="0" :precision="0" placeholder="文化满分"  />
        </el-form-item>
        <el-form-item label="专业分满分" prop="fullMajor" >
          <el-input-number v-model="form.fullMajor" :min="0" :precision="0" placeholder="专业满分"  />
        </el-form-item>
        <el-form-item label="该录取规则满分" prop="fullScore" v-if="form.acceptRuleId !== null" >
          <el-input-number v-model="form.fullScore"  placeholder="该录取规则满分" disabled  />
        </el-form-item>

        <el-form-item label="转换系数" prop="changeRate" v-if="form.acceptRuleId !== null">
          <el-input v-model="form.changeRate" placeholder="投档公式" :disabled="form.acceptRuleId !== null" />
        </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 {
  listMallAcceptRule,
  getMallAcceptRule,
  delMallAcceptRule,
  addMallAcceptRule,
  updateMallAcceptRule,
  selectNewAcceptRuleList
} from "@/api/mall/mallAcceptRule";

export default {
  name: "MallAcceptRule",
  data() {
    return {
      // 全部原则列表
      allRulesList: [],
      ruleClassOptions: [
        {
          value: 0,
          label: "本科"
        },
        {
          value: 1,
          label: "专科"
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 录取原则表格数据
      mallAcceptRuleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cultureRate: null,
        artRate: null,
        artBase: null,
        createAt: null,
        updateAt: null,
        delFlag: null,
        acceptRuleId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cultureRate: [
          { required: true, message: "文化分占比不能为空", trigger: "blur" }
        ],
        artRate: [
          { required: true, message: "艺术分占比不能为空", trigger: "blur" }
        ],
        artBase: [
          { required: true, message: "艺术分基础值不能为空", trigger: "blur" }
        ],
        createAt: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateAt: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "删除标记(0-正常,1-删除)不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getRuleList();
  },
  methods: {
    /** 查询录取原则列表 */
    getList() {
      this.loading = true;
      listMallAcceptRule(this.queryParams).then(response => {
        this.mallAcceptRuleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        acceptRuleId: null,
        cultureRate: null,
        artRate: null,
        artBase: null,
        createAt: null,
        updateAt: null,
        delFlag: null,
        changeRate: null,
        ruleCountStr: null,
        ruleStr: null,
        fullCulture: null,
        fullMajor: null,
        fullScore: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.acceptRuleId = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.acceptRuleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加录取原则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const acceptRuleId = row.acceptRuleId || this.ids
      getMallAcceptRule(acceptRuleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改录取原则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.acceptRuleId != null) {
            updateMallAcceptRule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMallAcceptRule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const acceptRuleIds = row.acceptRuleId || this.ids;
      this.$modal.confirm('是否确认删除录取原则编号为"' + acceptRuleIds + '"的数据项？').then(function() {
        return delMallAcceptRule(acceptRuleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mall/mallAcceptRule/export', {
        ...this.queryParams
      }, `mallAcceptRule_${new Date().getTime()}.xlsx`)
    },
    /** 所有录取原则列表 */
    getRuleList(){
      selectNewAcceptRuleList().then(response=>{
        this.allRulesList = response.data
      })
    },
  }
};
</script>
<style lang="scss">
.el-input.is-disabled .el-input__inner{
  color: #606266
}
.el-radio__input.is-checked.is-disabled .el-radio__inner {
  border-color: #1890ff;
  background: #1890ff;
}
.el-radio__input.is-disabled + span.el-radio__label {
  color: #313132;
}
</style>
