<template>
  <div class="navigation">
    <template v-if="NavIconIsShowAdd">
      <div class="head-title">
        导航图标列表
        <el-button @click="NavIconIsShowAdd=false" style="float:right;margin-top: -5px;" size="small" type="primary">
          添加导航图标
        </el-button>
      </div>
      <div class="content">
        <div>
          <div class="searchInput">
            <el-input style="display:inline-block;width:225px;height:30px" placeholder="请输入搜索内容"
              v-model="msg.NavIconName" size="small" clearable @keyup.enter.native="msg.pageIndex=1,getList()">
            </el-input>
            <span class="el-icon-search" style="color:#979dad;font-size:14px;position:relative;top:1px"
              @click="msg.pageIndex=1,getList()"> </span>
          </div>
        </div>
        <el-table :data="dataList" v-loading="loading" border style="width: 100%;margin:20px 0">
          <el-table-column prop="NavIconId" label="编号" width="100">
          </el-table-column>
          <el-table-column prop="NavIconName" width="120" label="名称">
          </el-table-column>
          <el-table-column label="导航图标" width="80">
            <template slot-scope="scope">
              <img :src="scope.row.NavIconImg" style="width:35px;height:35px;">
            </template>
          </el-table-column>
          <el-table-column prop="NavIconUrl" label="导航链接">
          </el-table-column>
          <el-table-column prop="NavIconSort" label="排序" width="180">
          </el-table-column>
          <el-table-column prop="NavIconIsShowStr" label="是否显示" width="120">
          </el-table-column>
          <el-table-column label="操作" width="120">
            <template slot-scope="scope">
              <img @click="EditNavIcon(scope.row)" src="../../assets/img/userman/edit.png" alt="">
              <img @click="RemmoveNavIcon(scope.row)" style="margin-left:10px;" src="../../assets/img/userman/del.png"
                alt="">
            </template>
          </el-table-column>
        </el-table>
        <el-pagination style="text-align:right" background @current-change="handleCurrentChange"
          :page-size="msg.pageSize" layout="prev, pager, next" :total="total">
        </el-pagination>
      </div>
    </template>
    <template v-else>
      <div class="head-title">
        <span @click="NavIconIsShowAdd=true" style="color:rgb(64, 158, 255);cursor:pointer;">导航图标列表</span><span
          style="margin:0 9px;color:#C0C4CC">/</span><span>导航图标编辑</span>
      </div>
      <div class="content">
        <el-form :model="addMsg" :rules="rules" ref="addMsg" label-width="80px" style="padding:0 20px;">
          <el-form-item label="头像">
            <div>
              <el-tooltip class="item" effect="dark" content="建议尺寸:88*88" placement="top-start">
                <el-button size="small" @click="choicImg=true">选择文件</el-button>
              </el-tooltip>
            </div>
            <div class="navNavIconImg">
              <img v-if="addMsg.headImg" :src="addMsg.headImg" alt="" />
              <img v-else src="../../assets/img/default.png" alt="" />
            </div>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="addMsg.name" class="w400" size="small" placeholder="请输入名称" maxlength="20" />
          </el-form-item>
          <el-form-item label="性别">
            <el-radio v-model="addMsg.sex" :label="1">男</el-radio>
            <el-radio v-model="addMsg.sex" :label="2">女</el-radio>
          </el-form-item>
          <el-form-item label="标签">
            <el-tag :key="tag" size="small" v-for="tag in addMsg.tags" closable :disable-transitions="false"
              @close="handleClose(tag)">
              {{tag}}
            </el-tag>
            <!-- <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput"
                size="small" @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
            </el-input> -->
            <el-button class="button-new-tag" size="small" @click="showInput">+ 标签</el-button>
          </el-form-item>
          <el-form-item label="好评率">
            <el-input v-model="addMsg.Praise" class="w400" size="small" placeholder="请输入好评率" maxlength="20" />
          </el-form-item>
          <el-form-item label="预约次数">
            <el-input v-model="addMsg.appointment" class="w400" size="small" placeholder="请输入预约次数" maxlength="20" />
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="addMsg.tel" class="w400" size="small" placeholder="请输入电话" maxlength="20" />
          </el-form-item>
          <el-form-item label="关联产品">
            <el-input v-model="addMsg.AssociatedProducts" class="w400" size="small" placeholder="请输入关联产品"
              maxlength="20" />
          </el-form-item>
          <el-form-item label="职务">
            <el-input v-model="addMsg.position" class="w400" size="small" placeholder="职务" maxlength="20" />
          </el-form-item>
          <el-form-item label="关联门店">
            <el-input v-model="addMsg.AssociatedStores" class="w400" size="small" placeholder="请输入关联门店"
              maxlength="20" />
          </el-form-item>
        </el-form>
      </div>
      <el-button size="small" style="margin-top:20px;padding:9px 25px;" type="primary" @click="submitform('addMsg')">保存
      </el-button>
    </template>
    <el-dialog title="选择链接" :visible.sync="isShowLink" width="800px">
      <chooseMenu ref="chooseMenu">
      </chooseMenu>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="isShowLink=false">取 消</el-button>
        <el-button size="small" type="danger" @click="getMenu()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 选择图片文件 -->
    <el-dialog title="选择文件" :visible.sync="choicImg" width="1240px">
      <ChooseImg @SelectId="SelectId"></ChooseImg>
    </el-dialog>
  </div>
</template>
<script>
  import chooseMenu from "../common/chooseMenu.vue";
  import ChooseImg from "@/components/global/ChooseImg.vue";
  export default {
    data() {
      let validataNavIconImg = (rule, value, callback) => {
        if (this.addMsg.NavIconImg == '') {
          return callback(new Error('请选择导航图标'));
        } else {
          callback();
        }
      }
      return {
        //选择图片
        choicImg: false,
        //是否线下链接弹窗
        isShowLink: false,
        loading: false,
        dataList: [],
        msg: {
          pageIndex: 1,
          pageSize: 15,
          NavIconName: "",
          MallBaseId: 0,
          NavIconIsShow: -1
        },
        total: 0,
        NavIconIsShowAdd: true,
        addMsg: {
          headImg: '', //头像
          name: '', //名称
          sex: 0, //性别
          tags: [], //标签
          Praise: 0, //好评率
          appointment: 0, //预约次数
          tel: '', //电话
          AssociatedProducts: '', //关联产品
          position: '', //职务
          AssociatedStores: '', //关联门店
        },
        rules: {
          NavIconName: [{
            required: true,
            message: "请输入导航名称",
            trigger: "blur"
          }],
          NavIconSort: [{
            required: true,
            message: "请输入导航排序",
            trigger: "blur"
          }],
          NavIconImg: [{
            validator: validataNavIconImg,
            trigger: "blur",
            required: true
          }],
          NavIconUrl: [{
            required: true,
            message: "请选择导航链接",
            trigger: "blur"
          }]
        },
      };
    },
    components: {
      chooseMenu,
      ChooseImg
    },
    created() {

    },
    methods: {
      //删除标签
      handleClose() {

      },
      //新增标签
      showInput() {

      },
      getMenu() {
        //调用子组件方法
        var obj = this.$refs.chooseMenu.getChooseMenu();
        this.addMsg.NavIconUrl = obj.PageUrl;
        this.isShowLink = false;
      },
      handleCurrentChange(val) {
        this.msg.pageIndex = val;
        this.getList();
      },
      getList() {
        this.apipost("/api/Tenant/GetMiniProgrameNaviconPage", this.msg, res => {
          if (res.data.resultCode == 1) {
            this.dataList = res.data.data.pageData;
            this.total = res.data.data.count;
          } else {
            this.Info(res.data.message);
          }
        })
      },
      //选择图片
      SelectId(msg) {
        this.addMsg.NavIconImg = this.getIconLink(msg.url);
        this.choicImg = false;
      },
      submitform(addMsg) {
        //提交创建、修改表单
        this.$refs[addMsg].validate(valid => {
          if (valid) {
            this.saveMsg();
          } else {
            return false;
          }
        });
      },
      //保存
      saveMsg() {
        this.apipost("/api/Tenant/SetMiniProgrameNavicon", this.addMsg, res => {
          if (res.data.resultCode == 1) {
            this.getList();
            this.NavIconIsShowAdd = true;
            this.addMsg.NavIconId = 0;
            this.addMsg.NavIconName = "";
            this.addMsg.NavIconSort = 100;
            this.addMsg.NavIconImg = "";
            this.addMsg.NavIconUrl = "";
            this.addMsg.NavIconIsShow = 0;
          } else {
            this.Info(res.data.message);
          }
        })
      },
      //修改
      EditNavIcon(item) {
        this.apipost("/api/Tenant/GetMiniProgrameNavicon", {
          NavIconId: item.NavIconId
        }, res => {
          if (res.data.resultCode == 1) {
            this.addMsg = res.data.data;
            this.NavIconIsShowAdd = false;
          } else {
            this.Info(res.data.message);
          }
        })
      },
    },
    mounted() {
      this.getList();
    }
  };

</script>
<style>
  .navNavIconImg {
    width: 80px;
    height: 80px;
  }

  .navNavIconImg img {
    width: 100%;
    height: 100%;
  }

  .navigation .blue {
    color: #409EFF;

  }

  .navigation .content .searchInput {
    border: 1px solid #DCDFE6;
    border-radius: 4px;
  }

  .navigation .content .searchInput .el-input__inner {
    border: none;
    outline: none;
    height: 30px;
    line-height: 30px;
  }

  .navigation .content .searchInput {
    line-height: normal;
    display: inline-table;
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    width: 250px;
    margin-right: 20px;
  }

  .navigation .content {
    background: #fff;
    margin-top: 10px;
    padding: 20px;
    box-sizing: border-box;
  }

</style>
