<template>
  <basic-container>
    <avue-crud :option="option" v-model:search="search" v-model:page="page" v-model="form" :table-loading="loading" :data="data" :permission="permissionList" :before-open="beforeOpen" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
      <template #menu-left>
        <el-button type="primary" icon="el-icon-plus" v-if="permission.famousStore_add" @click="toEdit()">新增</el-button>
      </template>
      <template #menu="scope">
        <el-button type="text" icon="el-icon-edit" v-if="permission.famousStore_edit" @click="toEdit(scope.row)">编辑</el-button>
        <el-button type="text" icon="el-icon-delete" v-if="permission.famousStore_delete" @click="rowDel(scope.row)">删 除</el-button>
      </template>
      <template #videos="scope">
        <video v-if="scope.row.videos" :src="scope.row.videos" style="width: 200px; height: 112px; border: 1px dashed #d9d9d9" controls></video>
      </template>
    </avue-crud>
    <el-dialog :title="dialogTitle" fullscreen v-model="dialogVisible" :modal-append-to-body="false" :close-on-click-modal="false" @close="closeDialog()" top="5vh" width="70%">
      <edit-article v-if="dialogVisible" :id="currentArticle?currentArticle.id:''" @afterSave="afterSave" @toCancel="closeDialog" />
    </el-dialog>
  </basic-container>
</template>

<script>
  import { getList, getDetail, add, update, remove } from "@/api/ycwh/famousStore";
  import option from "@/option/ycwh/famousStore";
  import { mapGetters } from "vuex";
  import { exportBlob } from "@/api/common";
  import { getToken } from '@/utils/auth';
  import { downloadXls } from "@/utils/util";
  import { dateNow } from "@/utils/date";
  import NProgress from 'nprogress';
  import 'nprogress/nprogress.css';
  //
  import { getLazyTree } from "@/api/base/region";
  import editArticle from "./components/editFamousStore/index.vue"

  export default {
    components: {
      editArticle
    },
    data() {
      return {
        dialogTitle: '',
        currentArticle: null,
        dialogVisible: false,
        //
        form: {},
        query: {},
        search: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: option,
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.famousStore_add, false),
          viewBtn: this.validData(this.permission.famousStore_view, false),
          delBtn: this.validData(this.permission.famousStore_delete, false),
          editBtn: this.validData(this.permission.famousStore_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    created() {},
    methods: {
      getRegionList() {
        getLazyTree("4404").then(res => {
          debugger
        })
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if(this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      handleExport() {
        let downloadUrl = `/famous_dish/famousStore/export-famousStore?${this.website.tokenHeader}=${getToken()}`;
        const {} = this.query;
        let values = {};
        this.$confirm("是否导出数据?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          NProgress.start();
          exportBlob(downloadUrl, values).then(res => {
            downloadXls(res.data, `名菜${dateNow()}.xlsx`);
            NProgress.done();
          })
        });
      },
      beforeOpen(done, type) {
        if(["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;

        let values = {
          ...this.query
        };

        getList(page.currentPage, page.pageSize, values).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
      // 新增文章相关
      toEdit(row) {
        if(row && row.id) {
          this.dialogTitle += '编辑';
          this.currentArticle = row;
        } else {
          this.dialogTitle += '新增'
          this.currentArticle = null
        }
        this.dialogVisible = true;
      },
      closeDialog(needReflash) {
        this.currentArticle = null;
        if(needReflash) {
          this.refreshChange();
        }
        this.dialogVisible = false;
      }
    }
  };
</script>

<style>

</style>