<style>
  .WebSiteTemplate .el-button.is-circle {
    padding: 6px;
  }

  .WebSiteTemplate .preview {
    color: #409eff;
    text-decoration: underline
  }

</style>
<template>
  <!--契约管理-->
  <div class="WebSiteTemplate">
    <div class="query-box">
      <ul>
        <li>
          <label>模板名称:</label>
          <el-input v-model="msg.TemplateName" :placeholder="$t('system.ph_in')" @keyup.native.enter="getData"
            class="w210">
          </el-input>
        </li>
        <li>
          <input type="button" class="hollowFixedBtn" :value="$t('pub.searchBtn')"
            @click="resetPageIndex(),getData()" />
          <input type="button" class="normalBtn" @click="outerVisible=true,clearMsg()" value="添加模板" />
        </li>
      </ul>
    </div>
    <div>
      <el-table :data="dataList" style="width: 100%" v-loading="loading"
        :default-sort="{prop: 'date', order: 'descending'}">
        <el-table-column prop="Id" label="编号" sortable header-align="center" align="center">
        </el-table-column>
        <el-table-column prop="TemplateKey" label="模板Key" header-align="center" align="center">
        </el-table-column>
        <el-table-column prop="TemplateName" label="模板名称" header-align="center" align="center">
        </el-table-column>
        <el-table-column label="操作" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" :content="$t('system.table_edit')" placement="top-start">
              <el-button type="primary" icon="el-icon-edit" circle @click="outerVisible = true,updateData(scope.row)">
              </el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="模板配置" placement="top-start">
              <el-button type="primary" class="iconfont icon-danju1" circle @click="GoToSubPage(scope.row)">
              </el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" :content="$t('system.table_delete')" placement="top-start">
              <el-button type="danger" icon="el-icon-delete" circle @click="deleteData(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background @current-change="handleCurrentChange" :current-page.sync="currentPage"
        layout="total,prev, pager, next, jumper" :page-size='msg.pageSize' :total=total>
      </el-pagination>
    </div>

    <el-dialog title="模板管理" :visible.sync="outerVisible" center width="400px">
      <el-form :model="addMsg" ref="addMsg" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="模板Key">
              <el-input type="text" v-model="addMsg.TemplateKey" maxlength="50" placeholder="模板Key">
              </el-input>
            </el-form-item>
            <el-form-item label="模板名称">
              <el-input type="text" v-model="addMsg.TemplateName" maxlength="50" placeholder="模板名称">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <button class="normalBtn" type="primary" @click="submitForm('addMsg')">{{$t('pub.saveBtn')}}</button> &nbsp;
        <button class="hollowFixedBtn" @click="outerVisible = false">{{$t('pub.cancelBtn')}}</button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        loading: false,
        msg: {
          TemplateName: '', //模板名称
          pageSize: 15,
          pageIndex: 1,
        },
        total: 0,
        currentPage: 1,
        //弹窗
        outerVisible: false,
        dataList: [],
        addMsg: {
          Id: 0, //编号
          TemplateKey: '', //模板Key
          TemplateName: '', //模板名称
        },
      }
    },
    mounted() {
      this.getData();
    },
    methods: {
      //获取配置
      getData() {
        //获取现有线路列表
        this.loading = true;
        this.apipost(
          "ws_get_GetTemplatePageList",
          this.msg,
          res => {
            this.loading = false;
            if (res.data.resultCode == 1) {
              this.dataList = res.data.data.pageData;
              this.total = res.data.data.count
            } else {
              this.loading = false;
            }
          },
          err => {}
        );
      },
      handleCurrentChange(val) {
        //翻页功能按钮
        this.msg.pageIndex = val;
        this.getData();
      },
      resetPageIndex() {
        //查询初始化页码
        this.msg.pageIndex = 1;
        this.currentPage = 1;
      },
      //修改
      updateData(item) {
        this.apipost(
          "ws_get_GetTemplate", {
            Id: item.Id,
          },
          res => {
            if (res.data.resultCode == 1) {
              var tempData = res.data.data;
              this.addMsg.Id = tempData.Id;
              this.addMsg.TemplateKey = tempData.TemplateKey;
              this.addMsg.TemplateName = tempData.TemplateName;
            } else {
              this.Error(res.data.message);
            }
          },
          err => {}
        );
      },
      //删除
      deleteData(item) {
        var that = this;
        that.Confirm("是否删除?", function () {
          that.apipost(
            "ws_post_RemoveTemplate", {
              Id: item.Id
            },
            res => {
              if (res.data.resultCode == 1) {
                that.Success(res.data.message);
                that.getData();
              } else {
                that.Error(res.data.message);
              }
            },
            null
          );
        });
      },
      //清空数据
      clearMsg() {
        this.addMsg.Id = 0;
        this.addMsg.TemplateKey = "";
        this.addMsg.TemplateName = "";
      },
      //新增、修改模板
      submitForm(addMsg) {
        this.apipost(
          "ws_post_SetTemplate", this.addMsg,
          res => {
            if (res.data.resultCode == 1) {
              this.getData();
              this.clearMsg();
              this.Success(res.data.message);
              this.outerVisible = false;
            } else {
              this.Error(res.data.message);
            }
          },
          err => {}
        );
      },
      //跳转子页面
      GoToSubPage(item) {
        this.$router.push({
          name: "WebSiteTemplateDetails",
          query: {
            Id: item.Id,
            blank: 'y',
            tab: '模板配置'
          }
        });
      }
    }
  }

</script>