<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="name">
        <el-input
          v-model="queryParams.name"
          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="['hezhi:config: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="['hezhi:config: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="['hezhi:config: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="['hezhi:config:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="相关设置主键ID" align="center" prop="configId" />-->
      <el-table-column label="设置标识" align="center" prop="identifying" />
      <el-table-column label="设置名称" align="center" prop="name" />
<!--      <el-table-column label="设置解释" align="center" prop="configExplain" />-->
<!--      <el-table-column label="设置内容" align="center" prop="configInt" />-->
<!--      <el-table-column label="设置内容" align="center" prop="configString" />-->
<!--      <el-table-column label="设置内容" align="center" prop="configDecimal" />-->
<!--      <el-table-column label="设置类型" align="center" prop="type" />-->
<!--      <el-table-column label="排序" align="center" prop="sort" />-->
<!--      <el-table-column label="备注" align="center" prop="remarks" />-->
<!--      <el-table-column label="状态" align="center" prop="status" />-->
      <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="['hezhi:config:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['hezhi:config: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
<!--        <el-form-item label="设置标识" prop="identifying">-->
<!--          <el-input v-model="form.identifying" placeholder="请输入设置标识" />-->
<!--        </el-form-item>-->
        <el-form-item label="设置名称" prop="name">
          <el-input v-model="form.name" readonly />
        </el-form-item>
<!--        <el-form-item label="设置解释" prop="configExplain">-->
<!--          <el-input v-model="form.configExplain" placeholder="请输入设置解释" readonly />-->
<!--        </el-form-item>-->
        <el-form-item label="设置内容" prop="configInt" v-if="form.type === 1">
          <el-input v-model="form.configInt" placeholder="请输入设置内容" />
        </el-form-item>
        <el-form-item label="设置内容" prop="configString" v-if="form.type === 2">
          <el-input v-model="form.configString" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="设置内容" prop="configString" v-if="form.type === 4">
          <editor v-model="form.configString" :min-height="192"/>
        </el-form-item>
        <el-form-item label="设置内容" prop="configDecimal" v-if="form.type === 3" >
          <el-input v-model="form.configDecimal" placeholder="请输入设置内容" />
        </el-form-item>
        <el-form-item label="设置内容" prop="configString" v-if="form.type === 5" >
          <image-upload v-model="form.configString" :limit="1"/>
        </el-form-item>

        <el-form-item label="每日签到获取积分" prop="configDecimal" v-if="form.identifying === 'aboutIntegral'">
          <el-input v-model="form.configDecimal" placeholder="请输入每日签到获取积分" :min="0" step-strictly />
        </el-form-item>
        <el-form-item label="一积分抵扣X元" prop="configDecimal2" v-if="form.identifying === 'aboutIntegral'" >
          <el-input v-model="form.configDecimal2" placeholder="请输入" type="number" :min="0" :step="0.01" />
        </el-form-item>
<!--        下拉-->
        <el-form-item label="积分抵扣百分比" prop="configString2" v-if="form.identifying === 'aboutIntegral'">
          <el-select v-model="form.configString2" placeholder="请选择" clearable>
            <el-option
              v-for="dict in dict.type.tb_rate"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
<!--        每支付X元赠送1积分-->
        <el-form-item label="每支付X元赠送1积分" prop="configString3" v-if="form.identifying === 'aboutIntegral'">
          <el-input v-model="form.configString3" placeholder="请输入" type="number" :min="0.01" :step="0.01"   />
        </el-form-item>
<!--        邀请人获得积分数量-->
        <el-form-item label="邀请人获得积分数量" prop="configString4" v-if="form.identifying === 'aboutIntegral'">
          <el-input v-model="form.configString4" placeholder="请输入" type="number" :min="0" :step="1"   />
        </el-form-item>
        <el-form-item label="积分规则" prop="configString" v-if="form.identifying === 'aboutIntegral'">
          <editor v-model="form.configString" :min-height="192"/>
        </el-form-item>

        <el-form-item label="微信号" prop="configString" v-if="form.identifying === 'wxConfig'">
          <el-input v-model="form.configString" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="微信二维码" prop="configString2" v-if="form.identifying === 'wxConfig'" >
          <image-upload v-model="form.configString2" :limit="1"/>
        </el-form-item>

        <el-form-item label="logo1" prop="configString" v-if="form.identifying === 'PCRight'">
          <image-upload v-model="form.configString" :limit="1"/>
        </el-form-item>
        <el-form-item label="logo1弹出图片" prop="configString2" v-if="form.identifying === 'PCRight'" >
          <image-upload v-model="form.configString2" :limit="1"/>
        </el-form-item>
        <el-form-item label="logo2" prop="configString3" v-if="form.identifying === 'PCRight'">
          <image-upload v-model="form.configString3" :limit="1"/>
        </el-form-item>
        <el-form-item label="logo2弹出图片" prop="configString4" v-if="form.identifying === 'PCRight'" >
          <image-upload v-model="form.configString4" :limit="1"/>
        </el-form-item>
        <el-form-item label="logo3" prop="configString5" v-if="form.identifying === 'PCRight'">
          <image-upload v-model="form.configString5" :limit="1"/>
        </el-form-item>
        <el-form-item label="logo3弹出图片" prop="configString6" v-if="form.identifying === 'PCRight'" >
          <image-upload v-model="form.configString6" :limit="1"/>
        </el-form-item>

        <el-form-item label="大会情况" prop="configString" v-if="form.identifying === 'meetingSituation'">
          <image-upload v-model="form.configString" :limit="1"/>
        </el-form-item>

        <el-form-item label="关于我们图片" prop="configString2" v-if="form.identifying === 'aboutUs'">
          <image-upload v-model="form.configString2" :limit="1"/>
        </el-form-item>
        <el-form-item label="关于我们标题" prop="configString3" v-if="form.identifying === 'aboutUs'">
          <el-input v-model="form.configString3" placeholder="请输入关于我们标题" />
        </el-form-item>
        <el-form-item label="关于我们详情" prop="configString" v-if="form.identifying === 'aboutUs'">
          <editor v-model="form.configString" :min-height="192"/>
        </el-form-item>

        <el-form-item label="学信考试简介" prop="configString" v-if="form.identifying === 'PC-aboutUs'">
          <editor v-model="form.configString" :min-height="192"/>
        </el-form-item>
        <el-form-item label="法律申明" prop="configString2" v-if="form.identifying === 'PC-aboutUs'">
          <editor v-model="form.configString2" :min-height="192"/>
        </el-form-item>
        <el-form-item label="服务协议" prop="configString3" v-if="form.identifying === 'PC-aboutUs'">
          <editor v-model="form.configString3" :min-height="192"/>
        </el-form-item>
        <el-form-item label="隐私协议" prop="configString4" v-if="form.identifying === 'PC-aboutUs'">
          <editor v-model="form.configString4" :min-height="192"/>
        </el-form-item>

        <el-form-item label="如何注册" prop="configString" v-if="form.identifying === 'PC-serviceCentre'">
          <editor v-model="form.configString" :min-height="192"/>
        </el-form-item>
        <el-form-item label="如何报名" prop="configString2" v-if="form.identifying === 'PC-serviceCentre'">
          <editor v-model="form.configString2" :min-height="192"/>
        </el-form-item>

        <el-form-item label="报名表模版" prop="configString" v-if="form.identifying === 'template'">
          <image-upload v-model="form.configString" :limit="1"/>
        </el-form-item>
        <el-form-item label="准考证模版" prop="configString2" v-if="form.identifying === 'template'">
          <image-upload v-model="form.configString2" :limit="1"/>
        </el-form-item>
        <el-form-item label="证书模版" prop="configString3" v-if="form.identifying === 'template'">
          <image-upload v-model="form.configString3" :limit="1"/>
        </el-form-item>

        <el-form-item label="主考机构合作" prop="configString" v-if="form.identifying === 'PC-cooperationCentre'">
          <editor v-model="form.configString" :min-height="192"/>
        </el-form-item>
        <el-form-item label="评价中心合作" prop="configString2" v-if="form.identifying === 'PC-cooperationCentre'">
          <editor v-model="form.configString2" :min-height="192"/>
        </el-form-item>
        <el-form-item label="实训基地合作" prop="configString3" v-if="form.identifying === 'PC-cooperationCentre'">
          <editor v-model="form.configString3" :min-height="192"/>
        </el-form-item>

        <el-form-item label="商务合作" prop="configString" v-if="form.identifying === 'wxzx'">
          <image-upload v-model="form.configString" :limit="1"/>
        </el-form-item>
        <el-form-item label="商务合作弹出" prop="configString2" v-if="form.identifying === 'wxzx'">
          <image-upload v-model="form.configString2" :limit="1"/>
        </el-form-item>
        <el-form-item label="平台咨询" prop="configString3" v-if="form.identifying === 'wxzx'">
          <image-upload v-model="form.configString3" :limit="1"/>
        </el-form-item>
        <el-form-item label="平台咨询弹出" prop="configString4" v-if="form.identifying === 'wxzx'">
          <image-upload v-model="form.configString4" :limit="1"/>
        </el-form-item>
        <el-form-item label="客服" prop="configString5" v-if="form.identifying === 'wxzx'">
          <image-upload v-model="form.configString5" :limit="1"/>
        </el-form-item>
        <el-form-item label="客服弹出" prop="configString6" v-if="form.identifying === 'wxzx'">
          <image-upload v-model="form.configString6" :limit="1"/>
        </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 { listConfig, getConfig, delConfig, addConfig, updateConfig } from "@/api/hezhi/config";

export default {
  name: "Config",
  dicts: ['tb_rate'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 相关设置表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        identifying: null,
        name: null,
        configExplain: null,
        configInt: null,
        configString: null,
        configString2: null,
        configString3: null,
        configString4: null,
        configString5: null,
        configString6: null,
        configDecimal: null,
        configDecimal2: null,
        type: null,
        sort: null,
        remarks: null,
        status: null,
        createTime: null,
        deleteTime: null,
        deleteBy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询相关设置列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then(response => {
        this.configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        configId: null,
        identifying: null,
        name: null,
        configExplain: null,
        configInt: null,
        configString: null,
        configString2: null,
        configString3: null,
        configString4: null,
        configString5: null,
        configString6: null,
        configDecimal: null,
        configDecimal2: null,
        type: null,
        sort: null,
        remarks: null,
        status: null,
        publicationTime: null,
        publisher: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        deleteTime: null,
        deleteBy: null,
        delFlag: 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.configId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加相关设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const configId = row.configId || this.ids
      getConfig(configId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改相关设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.configId != null) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.ids;
      this.$modal.confirm('是否确认删除相关设置编号为"' + configIds + '"的数据项？').then(function() {
        return delConfig(configIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hezhi/config/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
