<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container">
          <el-form size="small" label-width="120px" inline>
            <el-form-item label="关键字搜索：">
              <el-input
                v-model="keywords"
                placeholder="可搜索微信号/昵称"
                class="selWidth"
                @keyup.enter.native="getList(1)"
              >
                <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getList(1)" />
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <el-button v-hasPermi="['merchant:mp:live:assistant:save']" size="small" type="primary" @click="handleEdit(0)">
          <i class="add">+</i> 添加助手
        </el-button>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="mini">
        <el-table-column prop="id" label="ID" min-width="150" />
        <el-table-column prop="wechatNickname" label="微信昵称" min-width="150" />
        <el-table-column prop="wechat" label="微信号" min-width="150" />
        <el-table-column prop="createTime" label="创建时间" min-width="200" />
        <el-table-column label="操作" min-width="150" fixed="right">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['merchant:mp:live:assistant:edit', 'merchant:mp:live:assistant:info']"
              type="text"
              size="small"
              class="mr10"
              @click="handleEdit(1, scope.row)"
              >编辑</el-button
            >
            <el-button
              v-hasPermi="['merchant:mp:live:assistant:delete']"
              type="text"
              size="small"
              class="mr10"
              @click="handleDelete(scope.row.id, scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableForm.limit"
          :current-page="tableForm.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
  </div>
</template>
<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import {
  liveAssistantSaveApi,
  liveAssistantListApi,
  liveAssistantUpdateApi,
  liveAssistantDelApi,
} from '@/api/marketing';
import { checkPermi } from '@/utils/permission'; // 权限判断函数
export default {
  name: 'AssistantList',
  data() {
    return {
      listLoading: true,
      tableData: {
        data: [],
        total: 0,
      },
      tableForm: {
        page: 1,
        limit: 20,
        keywords: '',
      },
      keywords: '',
      loading: false,
      keyNum: 0,
    };
  },
  watch: {},
  mounted() {
    if (checkPermi(['merchant:mp:live:assistant:list'])) this.getList('');
  },
  methods: {
    checkPermi,
    handleEdit(isCreate, editDate) {
      const _this = this;
      this.id = editDate ? editDate.id : 0;
      this.$modalParserFrom(
        isCreate === 0 ? '新建小助手' : '编辑小助手',
        'liveAssistant',
        isCreate,
        isCreate === 0
          ? {
              id: 0,
              wechat: '',
              wechatNickname: '',
              assDesc: '',
            }
          : Object.assign({}, editDate),
        function (formValue) {
          _this.submit(formValue);
        },
        (this.keyNum += 1),
      );
    },
    //表单提交
    submit(formValue) {
      const data = {
        id: this.id,
        wechat: formValue.wechat,
        wechatNickname: formValue.wechatNickname,
        assDesc: formValue.assDesc,
      };
      !this.id
        ? liveAssistantSaveApi(data)
            .then((res) => {
              this.$message.success('操作成功');
              this.$msgbox.close();
              this.getList();
            })
            .catch(() => {
              this.loading = false;
            })
        : liveAssistantUpdateApi(data)
            .then((res) => {
              this.$message.success('操作成功');
              this.$msgbox.close();
              this.getList();
            })
            .catch(() => {
              this.loading = false;
            });
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableForm.page = num || this.tableForm.page;
      this.tableForm.keywords = encodeURIComponent(this.keywords);
      liveAssistantListApi(this.tableForm)
        .then((res) => {
          this.tableData.data = res.list;
          this.tableData.total = res.total;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    pageChange(page) {
      this.tableForm.page = page;
      this.getList('');
    },
    handleSizeChange(val) {
      this.tableForm.limit = val;
      this.getList('');
    },
    // 删除
    handleDelete(id, idx) {
      this.$modalSure('删除该直播助手吗？').then(() => {
        liveAssistantDelApi(id).then(() => {
          this.$message.success('删除成功');
          if (this.tableData.data.length === 1 && this.tableForm.page > 1)
            this.tableForm.page = this.tableForm.page - 1;
          this.getList('');
        });
      });
    },
  },
};
</script>

<style scoped lang="scss">
.add {
  font-style: normal;
}
</style>
