<!--
 * @Descripttion: 当前试用账号
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2020-11-13 10:59:31
 * @LastEditors: 无尘
 * @LastEditTime: 2020-11-19 10:42:53
-->
<template>
  <div class="common-right">
    <div class="m-b-20 flex flex-space-between">
      <el-input placeholder="请输入操作人/试用人/试用企业/手机号/试用账号"  maxlength="60" v-model="searchInput" class="w-408" @keyup.native="value => toInput(value, searchInput)" clearable @clear="clearInput"></el-input>
      <el-button class="m-l-10" type="primary" @click="editAccount('')">新建账号</el-button>
    </div>
    <el-table class="select-table" ref="multipleTable" v-loading="loading" :data="tableData" tooltip-effect="dark" :style="{ width: '100%' }">
      <el-table-column label="试用人" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.trialAccountName || '--' }}</template>
      </el-table-column>
      <el-table-column label="企业" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.wxEnterpriseName || '--' }}</template>
      </el-table-column>
      <el-table-column label="用户手机号" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.selfPhoneNumber || '--' }}</template>
      </el-table-column>
      <el-table-column label="试用账号" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.trialAccount || '--' }}</template>
      </el-table-column>
      <el-table-column label="账号类型" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.trialType == 1 ? '手机号' : '企微账号' }}</template>
      </el-table-column>
      <el-table-column prop="" label="试用开始时间" show-overflow-tooltip>
        <template slot-scope="scope">
          <div>{{ scope.row.createTime | timeStampToYmd }} {{ !scope.row.createTime ? '--': '' }}</div>
          <div>{{ scope.row.createTime | timeStampToHms }} {{ !scope.row.createTime ? '--': '' }}</div>
        </template>
      </el-table-column>
      <el-table-column label="有效期" show-overflow-tooltip>
        <template slot-scope="scope">{{ scope.row.effectDate | timeStampToYmd }} {{ !scope.row.effectDate ? '不限' : ''}}</template>
      </el-table-column>
      <el-table-column prop="" label="操作" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button type="text" @click="editAccount(scope.row)">编辑</el-button>
          <el-button type="text" @click="tryEnd(scope.row)">结束试用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block common-wrap__page text-right m-t-24" v-if="tableData.length != 0">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>
    <edit-account v-if="showEdit" :edit-row="editRow" @closeEdit="closeEdit" @refreshData="refreshData"></edit-account>
  </div>
</template>

<script>
import editAccount from '@/components/account/edit-account.vue';
import { _debounce } from '@/common/js/public';
import fetch from '@/api/account';

const { getAccountList, setAccountStatus } = fetch;

import handleFormat from '@/filters/index.js';
export default {
  name: 'ProjectSql',
  components: { editAccount },
  data() {
    return {
      userInfo: localStorage.getItem('haobanOperate') ? JSON.parse(localStorage.getItem('haobanOperate')) : { id: '', userMobile: '', realName: '', },
      searchInput: '',
      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0,
      loading: false,
      tableData: [],
      showEdit: false,
      editRow: {}
    };
  },
  // 生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    const that = this;
    that.getTableData();
  },

  // methods
  methods: {
    /**
     * @description: 输入
     * @param {Object} e
     * @param {String} value
     * @author: 无尘
     */
    toInput: _debounce(function(e, value) {
      const that = this;
      that.currentPage = 1;
      that.getTableData();
    }, 500),
    /**
     * @description: 搜索清除
     * @author: 无尘
     */
    clearInput() {
      const that = this;
      that.currentPage = 1;
      that.getTableData();
    },
    /**
     * @description: 分页---页码变化
     * @param {Number} val
     * @author: 无尘
     */
    handleSizeChange(val) {
      const that = this;
      that.currentPage = 1;
      that.pageSize = val;
      that.getTableData();
    },
    /**
     * @description: 分页---当前页变化
     * @param {Number} val
     * @author: 无尘
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      that.getTableData();
    },
    /**
     * @description: 获取项目列表
     * @author: 无尘
     */
    getTableData() {
      const that = this;
      that.loading = true;
      const param = {
        search: that.searchInput,
        type: 0,
        pageNum: that.currentPage,
        pageSize: that.pageSize
      };
      getAccountList(param).then(res => {
        that.loading = false;
        if (!!res.result.result && !!res.result.result.length) {
          res.result.result.forEach(ele => {
            ele.effectDate = handleFormat.timeStampToYmd(ele.effectDate);
          })
        }
        that.tableData = res.result.result || [];
        that.total = res.result.totalCount || 0;
      })
        .catch(function(error) {
          that.loading = false;
        });
    },
    /**
     * @description: 编辑
     * @param {Object} row
     * @author: 无尘
     */
    editAccount(row) {
      const that = this;
      that.showEdit = true;
      that.editRow = !!row ? JSON.parse(JSON.stringify(row)) : {};
    },
    closeEdit(res) {
      const that = this;
      that.showEdit = false;
      that.editRow = {};
      if (!!res) {
        that.currentPage = 1;
        that.getTableData();
      }
    },
    refreshData() {
      const that = this;
      that.currentPage = 1;
      that.getTableData();
    },
    /**
     * @description: 结束试用
     * @param {Object} row
     * @author: 无尘
     */
    tryEnd(row) {
      const that = this;
      that
        .$confirm('是否结束试用?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.postEnd(row);
        })
        .catch(() => {
        });
    },
    postEnd(row) {
      const that = this;
      const param = {
        trialAccountId: row.trialAccountId,
        commitId: that.userInfo.id,
        commitName: that.userInfo.realName
      };
      setAccountStatus(param).then(res => {
        if (res.code == '0000') {
          that.$message({
            message: '结束试用成功',
            type: 'success'
          });
          that.currentPage = 1;
          that.getTableData();
        }else{
          that.$message.error(res.message);
        }
      })
        .catch(function(error) {

        });
    }
  },
  watch: {}
};
</script>
<style lang='less' scoped>
  .m-b-20 {
    margin-bottom: 20px;
  }
  .w-408 {
    width: 408px;
  }
</style>
