<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="会员卡名称" prop="cardName">
        <el-input
          v-model="queryParams.cardName"
          placeholder="请输入会员卡名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select v-model="queryParams.userType" placeholder="请选择会员卡所属用户类型" clearable>
          <el-option
            v-for="dict in dict.type.dl_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="是否系统内置" prop="isSystem">-->
<!--        <el-select v-model="queryParams.isSystem" placeholder="请选择是否系统内置" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.true_or_false"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </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="['base:card: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="['base:card: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="['base:card: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="['base:card:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会员卡名称" align="center" prop="cardName" />
      <el-table-column label="用户类型" align="center" prop="userType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dl_user_type" :value="scope.row.userType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否系统内置" align="center" prop="isSystem">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.true_or_false" :value="scope.row.isSystem"/>
        </template>
      </el-table-column>
      <el-table-column label="会员卡描述" align="center" prop="content" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="logo" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="月付金额(元)" align="center" prop="monthPrice" />
      <el-table-column label="季付金额(元)" align="center" prop="quarterPrice" />
      <el-table-column label="年付金额(元)" align="center" prop="yearPrice" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="是否显示" align="center" prop="isShow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.true_or_false" :value="scope.row.isShow"/>
        </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="['base:card:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateRights(scope.row)"
          >权益分配</el-button>
          <el-button
            v-if="scope.row.isSystem==0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:card: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="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员卡名称" prop="cardName">
              <el-input v-model="form.cardName" placeholder="请输入会员卡名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户类型" prop="userType">
              <el-select v-model="form.userType" placeholder="请选择会员卡所属用户类型">
                <el-option
                  v-for="dict in dict.type.dl_user_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="系统内置" prop="isSystem">
              <el-select v-model="form.isSystem" disabled placeholder="请选择是否系统内置">
                <el-option
                  v-for="dict in dict.type.true_or_false"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否显示" prop="isShow">
              <el-select v-model="form.isShow" placeholder="请选择是否显示">
                <el-option
                  v-for="dict in dict.type.true_or_false"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" type="number" placeholder="请输入排序" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="月付金额(元)" prop="monthPrice">
              <el-input v-model="form.monthPrice" type="number" placeholder="请输入月付金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="季付金额(元)" prop="quarterPrice">
              <el-input v-model="form.quarterPrice" type="number" placeholder="请输入季付金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年付金额(元)" prop="yearPrice">
              <el-input v-model="form.yearPrice" type="number" placeholder="请输入年付金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="描述">
              <el-input v-model="form.content" type="textarea" placeholder="请输入会员卡描述" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="logo" prop="image">
              <image-upload v-model="form.image"/>
            </el-form-item>
          </el-col>
        </el-row>
      </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>
    <!-- 添加或修改会员卡权益对话框 -->
    <el-dialog :title="titleRights" :visible.sync="openRights" width="800px" append-to-body>
      <el-table v-loading="loading" :data="rightsList" @selection-change="handleSelectionChange">
        <el-table-column label="权益名称" align="center" prop="name" />
        <el-table-column label="权益值" align="center" >
          <template slot-scope="scope">
            <el-checkbox v-if="scope.row.rightsType=='01' && 1==scope.row.rightsValue" v-model="scope.row.rightsValue">支持</el-checkbox>
            <el-checkbox v-if="scope.row.rightsType=='01' && (0==scope.row.rightsValue || null==scope.row.rightsValue)" v-model="scope.row.rightsValue">不支持</el-checkbox>
            <div v-if="scope.row.rightsType=='02'" style="display: flex;justify-content: center;align-items: center;">
              <el-tooltip class="item" effect="dark" content="填0代表不限制" placement="top-end">
                <i class="el-icon-warning-outline"></i>
              </el-tooltip>
              <el-input v-model="scope.row.rightsValue" type="number" style="width: 200px" placeholder="请输入限制数量" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormRights">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCard, getCard, delCard, addCard, updateCard } from "@/api/base/card";
import { getCardRights ,addCardRights} from "@/api/base/cardRights";

export default {
  name: "Card",
  dicts: ['dl_user_type', 'true_or_false'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员卡表格数据
      cardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 弹出层标题-会员卡权益
      titleRights: "",
      // 是否显示弹出层-会员卡权益
      openRights: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cardName: null,
        userType: null,
        isSystem: null,
      },
      // 表单参数
      form: {},
      //权益列表
      rightsList:[],
      // 表单校验
      rules: {
        cardName: [
          { required: true, trigger: "blur", message: "请输入会员卡名称" }
        ],
        userType: [
          { required: true, trigger: "blur", message: "请选择权益所属用户类型" }
        ],
        isSystem: [
          { required: true, trigger: "blur", message: "请选择是否系统内置" }
        ],
        isShow: [
          { required: true, trigger: "blur", message: "请选择是否显示" }
        ],
        sort: [
          { required: true, trigger: "blur", message: "请输入排序" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员卡列表 */
    getList() {
      this.loading = true;
      listCard(this.queryParams).then(response => {
        this.cardList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openRights = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cardName: null,
        userType: null,
        content: null,
        sort: null,
        isSystem: 0,
        image: null,
        monthPrice: null,
        quarterPrice: null,
        yearPrice: null,
        remark: null,
        isShow: null,
        creator: null,
        createTime: null,
        updater: null,
        updateTime: 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.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
      getCard(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员卡";
      });
    },
    /** 修改权益操作 */
    handleUpdateRights(row) {
      let params = {
        id:row.id,
        userType:row.userType,
      }
      getCardRights(params).then(response => {
        this.rightsList = response.data;
        this.rightsList.map(item=>{
          if("01"==item.rightsType){
            //是否支持
            item.rightsValue= 1 ==item.rightsValue?true:false
          }
        })
        this.openRights = true;
        this.titleRights = "修改会员卡权益";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCard(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitFormRights() {
      let postData = JSON.parse(JSON.stringify(this.rightsList))
      postData.map(item=>{
        if("01"==item.rightsType){
          //是否支持
          item.rightsValue= true ==item.rightsValue?1:0
        }
      })
      addCardRights(postData).then(response => {
        this.$modal.msgSuccess("保存成功");
        this.openRights = false;
      });
      console.log(this.rightsList,"rightsList")
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员卡编号为"' + ids + '"的数据项？').then(function() {
        return delCard(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/card/export', {
        ...this.queryParams
      }, `card_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
