<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="规则名称" prop="settingsName">
        <el-input
          v-model="queryParams.settingsName"
          placeholder="请输入规则名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规则编码" prop="settingsCode">
        <el-input
          v-model="queryParams.settingsCode"
          placeholder="请输入规则编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规则类型" prop="settingsType">
        <el-select
          style="width: 220px"
          clearable
          v-model="queryParams.settingsType"
          placeholder="请选择"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="累计签到天数，签到设置使用" prop="totalSignInDays">
        <el-input
          v-model="queryParams.totalSignInDays"
          placeholder="请输入累计签到天数，签到设置使用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="积分数量，累计签到、邀请好友、积分抵扣等系统设置使用" prop="integralCount">
        <el-input
          v-model="queryParams.integralCount"
          placeholder="请输入积分数量，累计签到、邀请好友、积分抵扣等系统设置使用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="积分抵扣金额，积分抵扣设置使用" prop="creditAmount">
        <el-input
          v-model="queryParams.creditAmount"
          placeholder="请输入积分抵扣金额，积分抵扣设置使用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="显示顺序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          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="['system:settings: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="['system:settings: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="['system:settings: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="['system:settings:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="settingsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column
        label="规则名称"
        align="center"
        prop="settingsName"
        show-overflow-tooltip
      />
      <el-table-column label="规则编码" align="center" prop="settingsCode" />
      <!-- <el-table-column
        label="系统设置内容"
        align="center"
        prop="settingsContent"
        show-overflow-tooltip
      /> -->
      <el-table-column label="规则类型" align="center" prop="settingsType">
        <template slot-scope="scope">
          <!-- {{
            scope.row.settingsType == "INTEGRAL_EXCHANGE_RULE" ? "积分兑换" : ""
          }} -->
          {{ scope.row.settingsType == "SIGN_IN_RULE" ? "签到" : "" }}
          {{ scope.row.settingsType == "INTEGRAL_DEDUCTION" ? "积分抵扣" : "" }}
          {{ scope.row.settingsType == "INVITE_FRIENDS" ? "邀请好友" : "" }}
        </template>
      </el-table-column>
      <el-table-column
        label="累计签到天数"
        align="center"
        prop="totalSignInDays"
      />

      <el-table-column label="积分数量" align="center" prop="integralCount" />
      <el-table-column
        label="积分抵扣金额"
        align="center"
        prop="creditAmount"
      />
      <!-- <el-table-column
        label="积分有效期"
        align="center"
        prop="integralExpirationDate"
      /> -->
      <el-table-column label="显示顺序" align="center" prop="sort" />
      <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="['system:settings:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:settings: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="
        !isEditFlag
          ? '添加泉医到家平台系统业务设置信息'
          : '修改泉医到家平台系统业务设置信息'
      "
      :visible.sync="open"
      width="900px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        :inline="true"
      >
        <el-form-item label="规则名称" prop="settingsName">
          <el-input
            maxlength="100"
            v-model="form.settingsName"
            placeholder="请输入规则名称"
            style="width: 250px"
          />
        </el-form-item>

        <el-form-item label="规则类型" prop="settingsType">
          <el-select
            style="width: 250px"
            v-model="form.settingsType"
            placeholder="请选择"
            :disabled="isEditFlag"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="累计签到天数"
          prop="totalSignInDays"
          v-if="form.settingsType == 'SIGN_IN_RULE'"
        >
          <el-input
            style="width: 250px"
            onkeyup="this.value=this.value.replace(/[^[0-9]/g,'').replace(/^0[0-9]*/g, '')"
            onafterpaste="this.value=this.value.replace(/[^[0-9]/g,'').replace(/^0[0-9]*/g, '')"
            maxlength="9"
            oninput="value=value.replace(/[^\d]/g,'')"
            v-model.number="form.totalSignInDays"
            placeholder="请输入累计签到天数"
          />
        </el-form-item>

        <el-form-item
          prop="integralCount"
          :label="
            form.settingsType == 'INVITE_FRIENDS'
              ? '赠送积分数量'
              : form.settingsType == 'SIGN_IN_RULE'
              ? '赠送积分数量'
              : '积分数量'
          "
        >
          <el-input
            style="width: 250px"
            maxlength="9"
            onkeyup="this.value=this.value.replace(/[^[0-9]/g,'').replace(/^0[0-9]*/g, '')"
            onafterpaste="this.value=this.value.replace(/[^[0-9]/g,'').replace(/^0[0-9]*/g, '')"
            oninput="value=value.replace(/[^\d]/g,'')"
            v-model.number="form.integralCount"
            placeholder="请输入积分数量"
          />
        </el-form-item>
        <el-form-item
          label="积分抵扣金额"
          prop="creditAmount"
          v-if="form.settingsType == 'INTEGRAL_DEDUCTION'"
        >
          <el-input
            maxlength="8"
            style="width: 250px"
            v-model="form.creditAmount"
            onkeyup="this.value=this.value.replace(/[^[0-9]/g,'').replace(/^0[0-9]*/g, '')"
            onafterpaste="this.value=this.value.replace(/[^[0-9]/g,'').replace(/^0[0-9]*/g, '')"
            oninput="value=value.replace(/[^\d]/g,'')"
            placeholder="请输入积分抵扣金额"
          />
        </el-form-item>
        <!-- <el-form-item
          label="积分有效期"
          prop="integralExpirationDate"
          v-if="form.settingsType == 'INTEGRAL_EXCHANGE_RULE'||form.settingsType == 'INTEGRAL_DEDUCTION'"
        >
          <el-input
            style="width: 250px"
            maxlength="9"
            oninput="value=value.replace(/[^\d]/g,'')"
            v-model.number="form.integralExpirationDate"
            placeholder="请输入积分有效期"
          />
        </el-form-item> -->
        <el-form-item label="显示顺序" prop="sort">
          <el-input
            maxlength="5"
            style="width: 250px"
            v-model.number="form.sort"
            oninput="value=value.replace(/[^\d]/g,'')"
            placeholder="请输入显示顺序"
          />
        </el-form-item>
        <el-form-item label="系统设置内容" prop="settingsContent">
          <el-input
            type="textarea"
            :rows="5"
            style="width: 540px"
            placeholder="请输入系统设置内容"
            v-model="form.settingsContent"
          ></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 {
  listSettings,
  getSettings,
  delSettings,
  addSettings,
  updateSettings,
} from "@/api/system/settings";
import editor from "@/components/Editor";
export default {
  components: { editor },
  name: "Settings",
  data() {
    return {
      imgsurl: { pictureUrlList: [] },
      options: [
        // {
        //   value: "INTEGRAL_EXCHANGE_RULE",
        //   label: "积分兑换",
        // },
        {
          value: "SIGN_IN_RULE",
          label: "签到",
        },
        {
          value: "INTEGRAL_DEDUCTION",
          label: "积分抵扣",
        },
        {
          value: "INVITE_FRIENDS",
          label: "邀请好友",
        },
      ],
      value: "",
      // 弹出层判断新增还是修改
      isEditFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 泉医到家平台系统业务设置信息表格数据
      settingsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        settingsName: null,
        settingsCode: null,
        settingsContent: null,
        settingsType: null,
        totalSignInDays: null,
        integralExpirationDate: null,
        integralCount: null,
        creditAmount: null,
        sort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        integralExpirationDate: [
          {
            required: true,
            message: "请输入积分有效期",
            trigger: "blur",
          },
        ],
        settingsName: [
          {
            required: true,
            message: "请输入系统设置名称",
            trigger: "blur",
          },
        ],
        settingsContent: [
          {
            required: true,
            message: "请输入系统设置内容",
            trigger: "blur",
          },
        ],
        settingsType: [
          {
            required: true,
            message: "请选择系统设置类型",
            trigger: "blur",
          },
        ],
        totalSignInDays: [
          {
            required: true,
            message: "请输入累计签到天数",
            trigger: "blur",
          },
        ],

        integralCount: [
          {
            required: true,
            message: "请输入积分数量",
            trigger: "blur",
          },
        ],
        creditAmount: [
          {
            required: true,
            message: "请输入积分抵扣金额",
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            message: "请输入显示顺序",
            trigger: "blur",
          },
        ],
      },
    };
  },
  // computed: {
  //   label() {
  //     let label = "积分数量";
  //     if (this.form.settingsType == "INVITE_FRIENDS") label = "邀请好友次数";
  //     if (this.form.settingsType == "SIGN_IN_RULE") label = "赠送积分数量";
  //     return label;
  //   },
  // },

  created() {
    this.getList();
  },
  methods: {
    imgs(item) {
      console.log(item);
      this.imgsurl.pictureUrlList.push(item);
    },
    /** 查询泉医到家平台系统业务设置信息列表 */
    getList() {
      this.loading = true;
      listSettings(this.queryParams).then((response) => {
        this.settingsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        settingsName: null,
        settingsCode: null,
        settingsContent: null,
        settingsType: null,
        totalSignInDays: null,
        integralExpirationDate: null,
        integralCount: null,
        creditAmount: null,
        sort: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: 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.isEditFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加泉医到家平台系统业务设置信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isEditFlag = true;
      this.reset();
      const id = row.id || this.ids;
      getSettings(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改泉医到家平台系统业务设置信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateSettings(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSettings(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const settingsNames = row.settingsName || this.settingsNames;
      this.$modal
        .confirm('是否确认删除规则名称为"' + settingsNames + '"的数据项？')
        .then(function () {
          return delSettings(settingsNames);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/settings/export",
        {
          ...this.queryParams,
        },
        `settings_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
