<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="菜单名称" prop="menuName">
        <el-input
          v-model="queryParams.menuName"
          placeholder="请输入菜单名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="小程序" prop="appId">
        <el-select v-model="queryParams.appId" placeholder="请选择所属小程序" clearable size="small">
          <el-option
            v-for="dict in appMallOptions"
            :key="dict.appid"
            :label="dict.name + '(' + dict.appid + ')'"
            :value="dict.appid"
          />
        </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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['store:menu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['store:menu:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['store:menu:remove']"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-tabs type="card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="全部" name="00"></el-tab-pane>
      <el-tab-pane v-for="(item,index) in menuTypeOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
    </el-tabs>
    <el-table  height="500" border v-loading="loading" :data="menuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="menuId" />
      <el-table-column label="菜单名称" align="center" prop="menuName" />
      <el-table-column label="小程序" align="center" prop="appId" >
        <template slot-scope="scope">
          <el-tag prop="appId" v-for="(item, index) in appMallOptions"    v-if="scope.row.appId==item.appid">{{item.name}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图标" align="center" width="120">
        <template slot-scope="scope">
          <el-popover
            placement="right"
            title=""
            trigger="hover"
          >
            <img slot="reference" :src="scope.row.icon" width="60">
            <img :src="scope.row.icon" style="max-width: 120px;">
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="是否显示" align="center" prop="isShow"   >
          <template slot-scope="scope">
              <el-tag prop="isShow" v-for="(item, index) in statusOptions"    v-if="scope.row.isShow==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="跳转方式" align="center" prop="linkType" width="150px" >
          <template slot-scope="scope">
              <el-tag prop="linkType" v-for="(item, index) in menuLinkTypeOptions"    v-if="scope.row.linkType==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="跳转地址" align="center" prop="linkUrl" />
      <el-table-column label="排序" sortable 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="['store:menu:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:menu: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <Material v-model="imageArr" type="image" :num="1" :width="100" :height="100" />
        </el-form-item>
        <el-form-item label="跳转方式" prop="linkType">
          <el-select style="width: 200px" v-model="form.linkType" placeholder="请选择跳转方式" clearable size="small" >
              <el-option
                      v-for="item in menuLinkTypeOptions"
                      :key="item.dictValue"
                      :label="item.dictLabel"
                      :value="item.dictValue"
                    />
            </el-select>
        </el-form-item>
        <el-form-item label="跳转地址" prop="linkUrl">
          <el-input v-model="form.linkUrl" placeholder="请输入跳转地址" />
        </el-form-item>
        <el-form-item label="排序号" prop="sort">
          <el-input-number v-model="form.sort"  :min="1" :max="100" />
        </el-form-item>
        <el-form-item label="菜单类型" prop="menuType">
          <el-select style="width: 200px" v-model="form.menuType" placeholder="请选择菜单类型" clearable size="small" >
            <el-option
              v-for="item in menuTypeOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="小程序" prop="appId">
          <el-select style="width: 200px" v-model="form.appId" placeholder="请选择所属小程序" clearable size="small" >
            <el-option
              v-for="dict in appMallOptions"
              :key="dict.appid"
              :label="dict.name + '(' + dict.appid + ')'"
              :value="dict.appid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="isShow">
              <el-radio-group v-model="form.isShow">
                <el-radio v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictValue">{{dict.dictLabel}}</el-radio>
              </el-radio-group>
        </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 { listMenu, getMenu, delMenu, addMenu, updateMenu, exportMenu } from "@/api/hisStore/menu";
import {list as getAppMallOptions} from '@/api/course/coursePlaySourceConfig';
import Material from '@/components/Material'
export default {
  name: "Menu",
  components: {
    Material,
  },
  watch: {
    imageArr: function(val) {
      this.form.icon = val.join(',')
    },
  },
  data() {
    return {
      appMallOptions:[],
      activeName:"00",
      menuLinkTypeOptions:[],
      menuTypeOptions:[
        {"dictLabel": "主菜单",
          "dictValue": "1"},
        {"dictLabel": "养生有道",
          "dictValue": "2"},
        {"dictLabel": "个人中心",
          "dictValue": "3"},
        {"dictLabel": "主菜单-热销榜",
          "dictValue": "4"}
      ],
      statusOptions:[],
      imageArr:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户端菜单管理表格数据
      menuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        menuName: null,
        menuType: null,
        icon: null,
        isShow: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        menuName: [
          { required: true, message: "标题不能为空" }
        ],
        icon: [
          { required: true, message: "图标不能为空" }
        ],

      }
    };
  },
  created() {
    this.getDicts("menu_link_type").then((response) => {
      this.menuLinkTypeOptions = response.data;
    });
    this.getDicts("common_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getAppMallOptions();
    this.getList();

  },
  methods: {
    // 获取小程序选项列表
    getAppMallOptions() {
      getAppMallOptions({pageNum:1,pageSize:100,isMall:1}).then(response => {
        this.appMallOptions = response.rows;
      })
    },
    handleClick(tab,event){
      this.activeName=tab.name;
      if(tab.name=="00"){
        this.queryParams.menuType=null;
      }else{
        this.queryParams.menuType=tab.name;
      }
      this.getList()
    },
    /** 查询用户端菜单管理列表 */
    getList() {
      this.loading = true;
      listMenu(this.queryParams).then(response => {
        this.menuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        menuId: null,
        menuName: null,
        icon: null,
        isShow: "1",
        linkType: "1",
        linkUrl:null,
        createTime: null,
        updateTime: null,
        sort:1,
        menuType:null,
      };
      this.imageArr=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.menuId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加菜单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const menuId = row.menuId || this.ids
      getMenu(menuId).then(response => {
        this.form = response.data;
        if(response.data.isShow!=null){
          this.form.isShow = response.data.isShow.toString();
        }
        if(response.data.linkType!=null){
          this.form.linkType = response.data.linkType.toString();
        }
        if(response.data.menuType!=null){
          this.form.menuType = response.data.menuType.toString();
        }
        if(this.form.icon!=null){
          this.imageArr=this.form.icon.split(",");
        }
        this.open = true;
        this.title = "修改菜单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.menuId != null) {
            updateMenu(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addMenu(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const menuIds = row.menuId || this.ids;
      this.$confirm('是否确认删除用户端菜单管理编号为"' + menuIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMenu(menuIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户端菜单管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMenu(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
