<template>
  <div class="platform-account">
    <div class="address-list">
      <div class="item" v-for="(v,i) in accountList" :key="i" @click.stop="showAccount(1,v)">
        <div class="name">{{v.name}} {{v.mobile}}</div>
        <div class="shop-name">{{v.shopName}}</div>
        <div class="btns">
          <img @click.stop="showAccount(1,v)" src="../../../static/image/edit-icon.png" alt="">
          <img @click.stop="deleteAccount(v)" src="../../../static/image/delete-icon.png" alt="">
        </div>
      </div>
      <div class="item1" @click.stop="showAccount(0)">
        <img class="img" src="../../../static/image/icon-add.png" alt="">
        <div class="shop-name">添加账号</div>
      </div>
    </div>
    <!-- /新增编辑账号 -->
    <el-dialog :title="isEdit ? '编辑账号': '新增账号'" :visible.sync="centerDialogVisible" width="600px" center append-to-body>
      <el-form ref="form" :model="form" label-width="100px" label-position="left" size="small">
        <el-form-item label="所属门店">
          <el-select v-model="form.shopId" placeholder="请选择关联门店">
            <el-option v-for="v in shopList" :key="v.id" :label="v.name" :value="v.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工称呼" required>
          <el-input clearable v-model="form.name" placeholder="请填写员工称呼"></el-input>
        </el-form-item>
        <el-form-item label="手机号" required>
          <el-input clearable v-model="form.mobile" placeholder="请填写手机号"></el-input>
        </el-form-item>
        <el-form-item label="登录密码">
          <el-input clearable v-model="password" maxlength="11" :placeholder="passwordTips"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="centerDialogVisible=false">取 消</el-button>
        <el-button size="small" type="primary" @click="addAccount">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAccountList,
  deletePlatformAccount,
  addPlatformAccount,
  modifyPlatformAccount,
  shopListNew,
} from "../../api/shop.js";
export default {
  name: "platformAccount",
  data() {
    return {
      isEdit: 0,
      centerDialogVisible: false,
      accountList: [],
      shopList: [],
      form: {
        shopId: "",
        name: "",
        mobile: "",
      },
      password: "",
      passwordTips: "",
    };
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    addAccount() {
      if (!this.form.shopId) {
        return this.$message.error(`请选择账号所属的门店！`);
      }
      if (!this.form.name) {
        return this.$message.error(`请填写员工称呼！`);
      }
      if (!this.$tool.mobile(this.form.mobile)) {
        return this.$message.error(`请填写正确的手机号！`);
      }
      if (this.isEdit) {
        if (this.password) {
          this.form.password = this.password;
        }
        modifyPlatformAccount(this.form).then((res) => {
          if (res.code === 200) {
            this.getAccountList();
            this.$message.success("账号修改成功");
            this.centerDialogVisible = false;
          } else {
            this.$message.error(res.msg);
          }
        });
      } else {
        this.form.password = this.password || "123456";
        addPlatformAccount(this.form).then((res) => {
          if (res.code === 200) {
            this.getAccountList();
            this.$message.success("账号添加成功");
            this.centerDialogVisible = false;
          } else {
            this.$message.error(res.msg);
          }
        });
      }
    },
    showAccount(flag, form) {
      this.isEdit = flag;
      if (flag) {
        this.form = form;
        this.password = form.password;
        if (!this.form.password) {
          delete this.form.password;
        }
        this.passwordTips = !this.password ? "为空时则不修改密码" : "";
      } else {
        this.form = {};
        this.password = "";
        this.passwordTips = "默认密码：123456";
      }
      this.centerDialogVisible = true;
    },
    deleteAccount(v) {
      this.$confirm("此操作将删除该账号, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        center: true,
        type: "warning",
      }).then(() => {
        deletePlatformAccount({ id: v.id }).then((res) => {
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getAccountList();
          } else {
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        });
      });
    },
    getAccountList() {
      getAccountList().then((res) => {
        if (res.code === 200) {
          this.accountList = res.data.data;
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
    getShopList() {
      shopListNew().then((res) => {
        if (res.code === 200) {
          this.shopList = res.data;
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      });
    },
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getAccountList();
    this.getShopList();
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  // 生命周期 - 创建之前
  beforeCreate() {},
  // 生命周期 - 挂载之前
  beforeMount() {},
  // 生命周期 - 更新之前
  beforeUpdate() {},
  // 生命周期 - 更新之后
  updated() {},
  // 生命周期 - 销毁之前
  beforeDestroy() {},
  // 生命周期 - 销毁完成
  destroyed() {},
  // 如果页面有keep-alive缓存功能，这个函数会触发
  activated() {},
};
</script>

<style lang="scss" scoped  type="text/css">
.address-list {
  display: flex;
  flex-wrap: wrap;
  .item {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 280px;
    height: 120px;
    background: #ffffff;
    border-radius: 8px;
    margin: 10px 20px 0 0;
    padding: 35px 20px 0;
    cursor: pointer;
    &:hover {
      background: rgba(255, 255, 255, 0.39);
      box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
    }
    .img {
      width: 80px;
      height: 80px;
    }
    .name {
      font-size: 18px;
      font-weight: 500;
      line-height: 20px;
      color: #333333;
      margin-bottom: 10px;
    }
    .shop-name {
      font-size: 14px;
      font-weight: 400;
      line-height: 20px;
      color: #999999;
    }
    .btns {
      position: absolute;
      top: 10px;
      right: 0;
      img {
        width: 20px;
        height: 20px;
        margin-right: 10px;
      }
    }
  }
  .item1 {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: #ffffff;
    width: 320px;
    height: 155px;
    box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    margin: 10px 20px 0 0;
    cursor: pointer;
    .shop-name {
      font-size: 16px;
      font-weight: 500;
      line-height: 22px;
      color: #333333;
    }
  }
}
</style>