<template>
  <div id="decGuideId">
    <div class="dec-home">
      <div class="add-wrap">
        <slot></slot>
        <a-input-search
          placeholder="请输入页面名称"
          enter-button
          @search="onSearch"
        />
        <a-button type="primary" icon="plus" @click="addPage">新增</a-button>
      </div>
      <!-- 微页面列表 -->
      <a-spin :spinning="loading">
        <template v-if="option == 'table'">
          <a-table
            :columns="columns"
            :data-source="showList"
            :pagination="false"
            rowKey="id"
          >
            <span class="table-name" slot="name" slot-scope="name, record">
              {{ name
              }}<a-icon
                type="home"
                theme="filled"
                v-if="record.isDefault == '1'"
              />
            </span>
            <span slot="shops" slot-scope="shopId, record">
              {{ shopObj[shopId] }}
            </span>
            <span
              slot="operation"
              slot-scope="id, record"
              class="ic-opt"
              style="width: 180px"
            >
              <a @click="setHome(record)" :disabled="record.isDefault == 1">
                <a-tooltip placement="top">
                  <template slot="title">
                    <span>设为主页</span>
                  </template>
                  <a-icon type="home" />
                </a-tooltip>
              </a>
              <a v-if="hasTemp" @click="setTemp(record)">
                <a-tooltip placement="top">
                  <template slot="title">
                    <span>设为模版</span>
                  </template>
                  <a-icon type="file" />
                </a-tooltip>
              </a>
              <a @click="editPage(record)">
                <a-tooltip placement="top">
                  <template slot="title">
                    <span>编辑</span>
                  </template>
                  <a-icon type="edit" />
                </a-tooltip>
              </a>
              <a @click="copy(record)">
                <a-tooltip placement="top">
                  <template slot="title">
                    <span>复制</span>
                  </template>
                  <a-icon type="copy" />
                </a-tooltip>
              </a>
              <a @click="delPage(record)" :disabled="record.isDefault == 1">
                <a-tooltip placement="top">
                  <template slot="title">
                    <span>删除</span>
                  </template>
                  <a-icon type="delete" />
                </a-tooltip>
              </a>
            </span>
          </a-table>
        </template>
        <div class="list-wrap" v-else>
          <template v-if="showList.length > 0">
            <div
              class="item-wrap"
              v-for="(item, index) in showList"
              :key="index"
            >
              <div class="item-box">
                <div class="img-wrap">
                  <div class="is-home" v-if="item.isDefault == 1">
                    <a-icon type="home" />主页
                  </div>
                  <div class="is-temp" v-if="hasTemp" @click="setTemp(item)">
                    设为模版
                  </div>
                  <div class="action-wrap">
                    <a-button
                      type="primary"
                      shape="circle"
                      icon="edit"
                      @click="editPage(item)"
                    />
                    <a-button
                      type="primary"
                      shape="circle"
                      icon="home"
                      v-if="item.isDefault != 1"
                      @click="setHome(item)"
                    />
                    <a-button
                      type="primary"
                      shape="circle"
                      icon="delete"
                      @click="delPage(item)"
                    />
                  </div>
                  <img
                    style="width: 100%"
                    v-if="item.pageCover"
                    :src="item.pageCover"
                    alt
                  />
                </div>
                <div class="info-wrap">
                  <div class="info-name">{{ item.pageName }}</div>
                  <div>访问次数：{{ item.visitCount || 0 }}</div>
                  <div>创建时间：{{ splitDate(item.createTime) }}</div>
                  <a-button
                    v-if="item.copy != 'none'"
                    type="primary"
                    size="small"
                    class="set-temp"
                    @click="copy(item)"
                    >复制</a-button
                  >
                </div>
              </div>
            </div>
          </template>
        </div>
      </a-spin>
      <!-- 页码组件 -->
      <div class="pager-wrap">
        <div class="pager-info">
          共 {{ total }} 条记录，第 {{ current + "/" + pages }} 页
        </div>
        <a-pagination
          show-quick-jumper
          :pageSize="size"
          :default-current="current"
          :total="total"
          @change="pageChange"
        />
      </div>
      <!-- 模版选择+设定为模版 -->
      <template v-if="hasTemp">
        <temp-modal
          :api="decAxios.getJsonTemps"
          @choose="ensureJson"
          ref="tempModalRef"
        ></temp-modal>
        <a-modal
          :width="360"
          v-if="temps.options.length > 0"
          v-model="temps.visible"
          title="模版类型选择"
        >
          <div class="dec-flex-list middle">
            <span class="d-left">模版名称</span>
            <div class="d-right">
              <a-input
                placeholder="请输入模版名称"
                style="width: 246px"
                v-model="temps.tempName"
              />
            </div>
          </div>
          <div class="dec-flex-list middle">
            <span class="d-left">模版类型</span>
            <div class="d-right">
              <a-select
                v-model="temps.type"
                style="width: 246px"
                placeholder="请选模版类型"
                allowClear
              >
                <a-select-option
                  v-for="item in temps.options"
                  :key="item.id"
                  :value="item.id"
                  >{{ item.cateName }}</a-select-option
                >
              </a-select>
            </div>
          </div>
          <template slot="footer">
            <div style="text-align: center">
              <a-button @click="temps.visible = false">取消</a-button>
              <a-button type="primary" @click="selecteTemp">确定</a-button>
            </div>
          </template>
        </a-modal>
      </template>
      <!-- 设定店铺选择 -->
      <template>
        <a-modal
          :width="400"
          v-if="shopList.length > 0"
          v-model="shops.visible"
          title="复制目的地选择"
        >
          <div class="dec-flex-list middle">
            <span class="d-left">复制至</span>
            <div class="d-right">
              <a-select
                v-model="shops.selectValue"
                mode="multiple"
                style="width: 246px"
                placeholder="请选择门店"
              >
                <a-select-option v-for="item in shopList" :key="item.value">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </div>
          </div>
          <template slot="footer">
            <div style="text-align: center">
              <a-button @click="shops.visible = false">取消</a-button>
              <a-button type="primary" @click="selecteShop">确定</a-button>
            </div>
          </template>
        </a-modal>
      </template>
    </div>
    <!-- shop页面 -->
    <set-shop
      v-if="shopVisible"
      :option="shopOption"
      :config="config"
      @promise="saveOk"
    ></set-shop>
  </div>
</template>

<script>
import tempModal from "../modal/tempModal.vue";
import setShop from "../shopIndex";
const columns = [
  {
    title: "装修名称",
    dataIndex: "pageName",
    scopedSlots: { customRender: "name" },
  },
  {
    title: "页面描述",
    dataIndex: "pageDesc",
  },
  {
    width: 200,
    title: "创建时间",
    dataIndex: "updateTime",
  },
  {
    width: 200,
    title: "创建人",
    dataIndex: "updateBy",
  },
  {
    width: 180,
    title: "操作",
    dataIndex: "id",
    scopedSlots: { customRender: "operation" },
  },
];
export default {
  name: "shopHome",
  components: {
    tempModal,
    setShop,
  },
  props: {
    config: {
      type: Object,
      default: () => {
        return {};
      },
    },
    shopList: {
      type: Array,
      default() {
        return [];
      },
    },
    option: {
      type: String,
      default: "img",
    },
  },
  data() {
    return {
      loading: false,
      keyword: "",
      size: 10,
      total: 0,
      current: 1,
      pages: 1,
      showList: [], //微页面数据
      decAxios: {},
      hasTemp: false, //是否引入模版模块
      columns: [],
      shopObj: {},
      shops: {
        //复制
        visible: false,
        row: {},
        selectValue: [],
      },
      temps: {
        //设定为模版
        visible: false,
        options: [],
        type: "",
        selectValue: null,
        tempName: "",
      },
      shopVisible: false,
      shopOption: {
        tempId: "",
        setId: "",
        jsonTemp: "",
      },
    };
  },
  created() {
    const configObj = this.config || {};
    console.log(configObj, "configObj");
    this.decAxios = configObj.decAxios || {};
    this.columns = [...columns];
    if (this.shopList.length > 0) {
      this.columns.splice(1, 0, {
        width: 200,
        title: "店铺名称",
        dataIndex: "shopId",
        scopedSlots: { customRender: "shops" },
      });
      this.shopList.forEach((item) => {
        this.shopObj[item.value] = item.label;
      });
    }
    // 获取微页面数据
    this.initData();
    // 获取模版类型数据
    this.decAxios.getTempCates().then((res) => {
      if (res.code == "200") {
        this.hasTemp = true;
        this.temps.options = res.result;
      } else {
        this.hasTemp = false;
        this.temps.options = [];
      }
    });
  },
  methods: {
    initData() {
      this.loading = true;
      if (this.decAxios && this.decAxios.getJsonPages) {
        const params = {
          pageSize: this.size,
          pageNo: this.current,
          keyword: this.keyword,
        };
        this.decAxios.getJsonPages(params).then((res) => {
          if (res.code == "200") {
            this.showList = res.result.records;
            this.total = res.result.total;
            this.pages = res.result.pages;
          } else {
            this.showList = [];
            this.total = 0;
            this.pages = 0;
          }
          this.resetPageSize();
        });
      } else {
        this.loading = false;
        this.showList = [];
        this.total = 0;
        this.pages = 0;
        this.$message.error("config配置不完整");
      }
    },
    resetPageSize() {
      const clientWidth =
        document.getElementById("decGuideId").clientWidth - 20;
      const num = (clientWidth < 302 ? 1 : parseInt(clientWidth / 302)) * 2;
      if (num != this.size && this.total > num) {
        this.size = num;
        this.initData();
      } else {
        this.loading = false;
      }
    },
    onSearch(value) {
      this.current = 0;
      this.pages = 0;
      this.keyword = value;
      this.initData();
    },
    addPage() {
      if (this.hasTemp) {
        this.$refs.tempModalRef.show(this.decAxios.getJsonTemps);
      } else {
        this.ensureJson("");
      }
    },
    editPage(item) {
      this.shopVisible = true;
      this.shopOption = {
        tempId: "",
        setId: item.id,
        jsonTemp: "",
        shopId: item.shopId,
      };
      console.log(this.shopOption, this.shopVisible);
    },
    delPage(row) {
      if (`${row.isDefault}` === "1") {
        this.$message.warning("当前页面为主页不能删除");
      } else {
        this.$confirm({
          title: "删除确认",
          content: `是否确认删除“${row.pageName}”页面`,
          onOk: () => {
            this.decAxios.delJsonPage({ id: row.id }).then((res) => {
              if (res.code == "200") {
                this.$message.success("删除成功");
                this.current = 1;
                this.initData();
              }
            });
          },
        });
      }
    },
    setHome(row) {
      if (`${row.isDefault}` !== "1") {
        this.decAxios.setHomePage({ id: row.id }).then((res) => {
          if (res.code == "200") {
            this.$message.success("成功设置为主页");
            this.initData();
          }
        });
      }
    },
    copy(row) {
      if (row.copy != "none") {
        if (this.shopList.length > 0) {
          //打开复制目的弹窗
          this.shops = {
            visible: true,
            row: row,
            selectValue: [],
          };
        } else {
          this.shopVisible = true;
          this.shopOption = {
            tempId: "",
            setId: "",
            jsonTemp: row.pageLayout,
          };
        }
      }
    },
    setTemp(row) {
      this.temps.selectValue = row;
      this.temps.type = "";
      this.temps.tempName = "";
      this.temps.visible = true;
    },
    selecteTemp() {
      this.temps.visible = false;
      const row = this.temps.selectValue;
      const params = {
        templateName: this.temps.tempName,
        templateImg: row.pageCover,
        templateCateId: this.temps.type,
        templateJson: row.pageLayout,
      };
      this.decAxios.setPageToTemp(params).then((res) => {
        if (res.code == "200") {
          this.$message.success("成功设置成模版");
        }
      });
    },
    selecteShop() {
      //调用复制接口
      this.shops.visible = false;
      const row = this.shops.row;
      const shopId = this.shops.selectValue;
      this.loading = true;
      this.decAxios
        .copyJsonPage({ id: row.id, shopIdList: shopId })
        .then((res) => {
          if (res.code == "200") {
            this.$message.success("复制成功");
          } else {
            this.$message.success("复制失败");
          }
          this.loading = false;
        })
        .catch((err) => {
          this.$message.success("复制失败");
        });
    },
    pageChange(obj) {
      obj && (this.current = obj) && this.initData();
    },
    ensureJson(val) {
      this.shopVisible = true;
      this.shopOption = {
        tempId: val,
        setId: "",
        jsonTemp: "",
        shopId: "",
      };
    },
    splitDate(val) {
      return (val || "").split(" ")[0];
    },
    saveOk(res) {
      // 返回home页面
      if (res.success) this.initData();
      this.shopVisible = false;
      this.shopOption = {
        tempId: "",
        setId: "",
        jsonTemp: "",
        shopId: "",
      };
    },
  },
};
</script>
