<!--app菜单管理-->
<template>
  <div class="app-container" style="background-color: #fff">
    <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="['ops:product:add']"
        >新增
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table :data="productList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="菜单id" align="center" prop="amenuId"/>
      <el-table-column label="默认图标" align="center" prop="amenuIcon">
        <template slot-scope="scope">
          <image-preview :src="scope.row.amenuIcon" :width="100" :height="100"/>
        </template>
      </el-table-column>

      <el-table-column label="菜单名称" align="center" prop="amenuName"/>
      <el-table-column label="跳转链接" align="center" prop="amenuUrl"/>
      <!-- 0首页菜单1个人中心菜单 2底部菜单 -->
      <el-table-column label="类型" align="center" prop="amenuType">
        <template slot-scope="scope">
          <DictTag :options="dict.type.sys_app_menu" :value="scope.row.amenuType"></DictTag>
        </template>
      </el-table-column>
      <!-- 0正常1停用 -->
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          {{ scope.row.status | switchStatus }}
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="orderNum"/>

      <el-table-column label="详情" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="next(scope.row)"
            v-hasPermi="['ops:product:edit']"
          >查看详情
          </el-button
          >
        </template>

      </el-table-column>
      <!-- <el-table-column label="显示顺序" align="center" prop="orderNum" /> -->
      <el-table-column label="跳转类型" align="center" prop="amenuContent"/>
      <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="['ops:product:edit']"
          >修改
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ops:product: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="dialogFormVisible" width="700px" >
      <el-form :model="form" label-width="98px">
        <el-form-item label="图片">
          <image-upload v-model="form.amenuIcon"/>
        </el-form-item>

        <el-form-item label="菜单名称" prop="amenuName">
          <el-input
            v-model="form.amenuName"
            placeholder="请输入菜单名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="跳转链接" prop="amenuUrl">
          <el-input
            type="textarea"
            v-model="form.amenuUrl"
            placeholder="请输入跳转链接"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input
              type="textarea"
              v-model="form.orderNum"
              placeholder="请输入"
              clearable

          />
        </el-form-item>
        <el-form-item label="菜单类别" prop="amenuType">
          <el-radio-group v-model="form.amenuType">
            <el-radio
              v-for="dict in dict.type.sys_app_menu"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="跳转类型" prop="amenuContent">
          <el-input
            type="textarea"
            v-model="form.amenuContent"
            placeholder="请输入跳转类型"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
<!--        <Editor :value="form.title" @onChange="htmlchange"/>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
import {
  getmenu,
  getmenuone,
  putmenu,
  delmenu,
  addmenu,
  exportmenu
} from '@/api/ops/app/caidan.js'
import { getDicts } from '@/api/system/dict/data'
import { getToken } from '@/utils/auth'
import { addProduct, getProductone, putProduct } from '@/api/ops/vip/product'

export default {
  // components: { Editor, Toolbar },
  name: 'Product',
  dicts: ['sys_app_menu'],
  data() {
    return {
      // editor: null,
      // html: '',
      // toolbarConfig: {},
      // editorConfig: { placeholder: '请输入内容...' },
      // mode: 'default', // or 'simple'
      //类型
      amenuType: [],
      // 状态
      status: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      productList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      dialogFormVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        amenuName: null,
        amenuUrl: null,
        amenuType: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // prizeMany: [
        //   { required: true, message: "买一送N不能为空", trigger: "blur" }
        // ],
      }
    }
  },
  mounted() {
    // 模拟 ajax 请求,异步渲染编辑器
    // setTimeout(() => {
    //   this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
    // }, 1500)
  },
  created() {
    this.getdict()
    this.getList()
  },

  filters: {
    switchType(val) {
      let res
      if (val == 0) res = '首页菜单'
      if (val == 1) res = '个人中心菜单'
      if (val == 2) res = '底部菜单菜单'

      return res
    },
    switchStatus(val) {
      let res
      if (val == 0) res = '正常'
      if (val == 1) res = '停用'
      return res
    }
  },
  beforeDestroy() {
    // const editor = this.editor
    // if (editor == null) return
    // editor.destroy() // 组件销毁时,及时销毁编辑器
  },
  methods: {
    htmlchange(i) {
      this.form.amenuContent = i
    },

    onCreated(editor) {
      this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
    },
    /** 查询产品列表 */
    getList() {
      // this.loading = true;
      getmenu(this.queryParams).then((response) => {
        this.productList = response.rows

        // console.log(this.productList);
        this.total = response.total
        this.loading = false
      })
      // ;
    },
    // 跳转下一级
    next(row) {
      let data = {
        id: row.amenuId
      }
      console.log(row)
      this.$router.push({ path: '/app/homesub', query: data })
    },

    // 获取字典信息
    getdict() {
      getDicts('sys_app_menu').then(res => {
        console.log(res.data)
        this.amenuType = res.data
      })
    },
    // 取消按钮

    cancel() {
      this.dialogFormVisible = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        bankName: null,
        bankNum: null,
        openBank: null,
        phoneNum: null,
        status: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      getmenu(this.queryParams).then((response) => {
        // console.log(response);
        this.productList = response.rows
        this.total = response.total
        this.loading = false
      })
      // this.queryParams.pageNum = 1;
      // this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      console.log(210)
      this.reset()
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(216)
      // this.ids = selection.map(item => item.productId)
      // this.single = selection.length!==1
      // this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.dialogFormVisible = true
      this.title = '添加app菜单管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      getmenuone(row.amenuId).then((res) => {
        this.form = res.data
        this.dialogFormVisible = true
        this.title = '修改app菜单管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.amenuId != null) {
        putmenu(this.form).then((response) => {
          this.$modal.msgSuccess('修改成功')
          this.dialogFormVisible = false
          this.getList()

        })
      } else {

        addmenu(this.form).then((response) => {
          this.$modal.msgSuccess('新增成功')
          this.dialogFormVisible = false
          this.getList()

        })
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // delmenu(row.amenuId).then((res) => {
      //   console.log(res);
      // });
      // console.log(411);
      const productIds = row.amenuId
      this.$modal
        .confirm('是否确认删除产品编号为"' + productIds + '"的数据项?')
        .then(function() {
          return delmenu(productIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      // exportmenu("传参").then((res)=>{
      //   console.log(res);
      // })
      console.log(422)
      // this.download('ops/product/export', {
      //   ...this.queryParams
      // }, `product_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>

<style lang="scss" scoped></style>