<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @change="resetSearch" clearable standout="bg-primary text-white" class="col-6 q-pr-lg q-pr-lg"
            v-model="msg.NavTitle" label="导航标题" maxlength="20" @clear="resetSearch" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" option-value="Id" option-label="Name" standout="bg-primary text-white"
            v-model="msg.NavType" class="col-6 q-pr-lg q-pr-lg" :options="NavTypeList" emit-value map-options
            label="导航类型" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" standout="bg-primary text-white" class="col-6 q-pr-lg q-pr-lg"
            v-model="msg.Status" :options="ShowOpts" emit-value map-options label="状态" />
        </div>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat class="sticky-column-table"
        separator="none" :data="data" :columns="columns" row-key="name">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">导航信息</div>
          <q-space />
          <div class="page-option">
            <q-btn color="accent" class="q-mr-md" size="sm" icon="add" label="新增导航" @click="EditMenu(null)" />
          </div>
        </template>
        <template v-slot:body-cell-BgImg="props">
          <q-td :props="props">
            <img :src="props.row.BgImg" style="width:50px;" />
          </q-td>
        </template>
        <template v-slot:body-cell-Status="props">
          <q-td :props="props">
            <q-toggle size="md" color="primary" :false-value="1" :true-value="0" v-model="props.row.Status"
              title="注意：关闭后，分类将无法正常使用." @input="DeleteMenu(props.row)" />
          </q-td>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="pageCount"
            :input="true" @input="changePage" />
        </template>
        <template v-slot:body-cell-optioned="props">
          <q-td :props="props">
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="EditMenu(props.row)" />
          </q-td>
        </template>
      </q-table>
      <webkit-form v-if="isShowWebkitForm" :save-obj="webkitObjOption" @close="closeWebSaveForm" @success="refreshPage">
      </webkit-form>
    </div>
  </div>
</template>
<script>
  import {
    GetWebNavPage,
    GetNavTypeList,
    RemoveWebNavStatus,
  } from '../../api/system/webkit'
  import webkitForm from '../../components/system/webkit-form'
  export default {
    meta: {
      title: "网站管理"
    },
    components: {
      webkitForm
    },
    data() {
      return {
        columns: [{
            name: 'NavTitle',
            label: '导航标题',
            field: 'NavTitle',
            align: 'left'
          },
          {
            name: 'BgImg',
            label: '背景图',
            field: 'BgImg',
            align: 'left'
          },
          {
            name: 'LinkUrl',
            label: '链接地址',
            field: 'LinkUrl',
            align: 'left'
          },
          {
            name: 'NavTypeName',
            label: '导航类型',
            field: 'NavTypeName',
            align: 'left'
          },
          {
            name: 'Status',
            label: '状态',
            field: 'Status',
            align: 'left'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'MenuId'
          }
        ],
        data: [],
        loading: true,
        ShowOpts: [{
            label: '全部',
            value: -1
          },
          {
            label: '正常',
            value: 0
          },
          {
            label: '删除',
            value: 1
          }
        ],
        //菜单类型
        NavTypeList: [],
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          NavTitle: "",
          SubTitle: "",
          NavType: 0,
          Status: 0,
        },
        pageCount: 0,
        isShowWebkitForm: false,
        webkitObjOption: null,
      }
    },
    mounted() {
      this.initNavType();
      this.getList()
    },
    methods: {
      //初始化下拉框
      initNavType() {
        GetNavTypeList().then(res => {
          if (res.Code == 1) {
            this.NavTypeList = res.Data;
            let obj = {
              Id: 0,
              Name: '不限'
            }
            this.NavTypeList.unshift(obj);
          } else {
            this.$q.notify({
              type: 'negative',
              position: "top",
              message: res.Message
            })
          }
        })
      },
      //修改菜单状态
      DeleteMenu(obj) {
        var that = this;
        var delMsg = {
          Id: obj.Id,
          Status: obj.Status
        };
        var tipMsg = "";
        if (delMsg.Status == 0) {
          tipMsg = "是否启用【" + obj.NavTitle + "】?";
        } else {
          tipMsg = "是否禁用【" + obj.NavTitle + "】?";
        }
        this.$q.dialog({
          title: '提示信息',
          message: tipMsg,
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          RemoveWebNavStatus(delMsg).then(res => {
            if (res.Code == 1) {
              that.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功',
                position: 'top'
              })
              that.getList();
            }
          })
        }).onCancel(() => {
          obj.Status = obj.Status == 1 ? 0 : 1;
        });
      },
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.getList();
      },
      //翻页
      changePage(val) {
        this.msg.pageIndex = val;
        this.getList()
      },
      //获取菜单分页列表
      getList() {
        this.loading = true;
        GetWebNavPage(this.msg).then(res => {
          this.loading = false
          this.data = res.Data.PageData;
          this.pageCount = res.Data.PageCount;
        }).catch(() => {
          this.loading = false
        })
      },
      //刷新页面
      refreshPage() {
        if (!this.webkitObjOption) {
          this.msg.pageIndex = 1;
          this.msg.NavTitle = "";
          this.msg.SubTitle = "";
          this.msg.NavType = 0;
          this.msg.Status = "-1";
        }
        this.getList()
      },
      //新增修改菜单
      EditMenu(obj) {
        if (obj) {
          this.webkitObjOption = obj
        } else {
          this.webkitObjOption = null
        }
        this.isShowWebkitForm = true
      },
      //关闭弹窗
      closeWebSaveForm() {
        this.isShowWebkitForm = false
      },
    }
  }

</script>
<style lang="sass">
  @import url('~assets/css/table.sass')

</style>
