<!--
  <related-table :relateRow="relateRow"  @closeTable="closeTable"></related-table>
  import relatedTable from '@/components/company/related-table.vue';
 -->
<template>
  <el-dialog title="关联详情" :visible.sync="dialogVisible" width="995px" :before-close="handleClose">
    <div class="overstore-tip">
      <div class="flex flex-space-between">
        <div></div>
        <el-button type="primary" @click="toAddRelate">新建关联</el-button>
      </div>
    </div>
    <div class="m-t-20">
      <el-table class="select-table" ref="multipleTable" height="500" :data="tableData" tooltip-effect="dark" :style="{ width: '100%' }">
        <el-table-column label="导购code" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.clerkCode }}</template>
        </el-table-column>
        <el-table-column prop="" label="导购姓名" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.clerkName }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="导购手机号" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.clerkPhoneNumber }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="关联商户" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.enterpriseName }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="关联门店" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.storeName }}
          </template>
        </el-table-column>
        <el-table-column prop="" label="关联时间" show-overflow-tooltip>
          <template slot-scope="scope">
            <div class="color-606266 line-18">{{ scope.row.createTime | timeStampToYmd }}</div>
            <div class="color-606266 line-18">{{ scope.row.createTime | timeStampToHms }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="" label="操作" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button type="text" @click="toUnbind(scope.row)">解绑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--<div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submitForm('form')">确定</el-button>
    </div>-->
    <add-relate v-if="addShow" :departObj="relateRow" @refreshData="refreshData"></add-relate>
  </el-dialog>
</template>
<script>
import addRelate from '@/components/company/add-relate.vue';
import { getRequest, postRequest } from '@/api/api';
import errMsg from '@/common/js/error';
import showMsg from '@/common/js/showmsg';
import { _debounce } from '@/common/js/public';
export default {
  name: 'related-table',
  props: {
    relateRow: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      dialogVisible: true,
      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0,
      tableData: [],
      addShow: false
    };
  },
  computed: {},
  methods: {
    refreshData(data) {
      const that = this;
      that.addShow = false;
      if (data == 'close') {
        return false;
      }
      that.getTableList();
    },
    cancel() {
      const that = this;
      that.$emit('closeTable', 'close');
    },
    /**
     * 确定保存
     */
    submitForm: _debounce(function(form) {
      const that = this;
      that.postAdd();
    }, 300),
    postAdd() {
      const that = this;
      let params = {
        params: JSON.stringify(that.tableData)
      };
      postRequest('/haoban-manage3-web/staff-batch-edit-department', params)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('添加成功', 'success');
            that.$emit('closeTable', 'close');
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    /**
     * 添加关联
     */
    toAddRelate() {
      const that = this;
      that.addShow = true;
    },
    /**
     * 解绑
     */
    toUnbind(row) {
      const that = this;
      that
        .$confirm('确定解绑当前关联?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          that.postUnbind(row);
        })
        .catch(() => {});
    },
    postUnbind(row) {
      const that = this;
      let para = {
        staffId: row.staffId,
        clerkId: row.clerkId
      };
      postRequest('/haoban-manage3-web/material-del', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            showMsg.showmsg('解绑成功', 'success');
            that.getTableList();
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
    handleClose() {
      const that = this;
      that.tableData = [];
      that.$emit('closeTable');
    },
    /**
     * 路由跳转
     */
    changeRoute(path) {
      this.$router.push(path);
    },

    /**
     * 分页---页码变化
     */
    handleSizeChange(val) {
      const that = this;
      that.pageSize = val;
      that.getTableList();
    },

    /**
     * 分页---当前页变化
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      that.getTableList();
    },
    /**
     * 获取列表数据
     */
    getTableList(val) {
      const that = this;
      let para = {
        staffId: that.relateRow.staffId
      };
      getRequest('/haoban-manage3-web/staff-clerk-relation', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.tableData = resData.result.result || [];
            return false;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    relateRow(newData, oldData) {
      const that = this;
      if (Object.keys(newData).length) {
        that.getTableList();
      }
    }
  },
  mounted() {
    const that = this;
    if (Object.keys(that.relateRow).length) {
      that.getTableList();
    }

    document.documentElement.style.backgroundColor = '#f0f2f5';
  },
  destroyed() {
    document.documentElement.style.backgroundColor = '#fff';
  },
  components: { addRelate }
};
</script>
<style type="text/less" lang="less" scoped>
.span-dot {
  display: inline-block;
  vertical-align: middle;
  width: 6px;
  height: 6px;
  margin-right: 6px;
  border-radius: 3px;
  &.bg-52c41a {
    background: #52c41a;
  }
  &.bg-f5222d {
    background: #f5222d;
  }
}
.bg-82C5FF {
  background: #82c5ff;
}
.color-508CEE {
  color: #508cee;
}
.color-FF585C {
  color: #ff585c;
}
.line-h-18 {
  line-height: 18px;
}
.tooltip-text {
  width: 100%;
  white-space: pre-wrap;
  word-break: break-all;
}
.w-293 {
  width: 293px;
}
.w-191 {
  width: 191px;
}
.el-alert--info {
  background: rgba(240, 245, 255, 1);
  border-radius: 2px;
  border: 1px solid rgba(133, 165, 255, 1);
  -webkit-box-align: flex-start;
  -webkit-align-items: flex-start;
  -ms-flex-align: flex-start;
  align-items: flex-start;
  .el-alert__icon {
    font-size: 12px;
    padding-top: 3px;
  }
}
.my-customer-wrap {
  height: 100%;
}
.right-content {
  /*width: 100%;*/
  padding: 0 20px 20px 20px;
  min-height: calc(100% - 160px);
  .right-box {
    background: #fff;
    min-height: 500px;
    padding: 0px;
    .apps-content {
      .apps-content-right {
        width: 100%;
        padding: 20px;
        background: #fff;
        .overstore-body {
          background: #fff;
        }
        .daily-set-wrap {
          height: 100%;
          background: #fff;
        }
        .app-detail-wrap {
          height: 100%;
          background: #fff;
        }
        .common-set-wrap {
          height: 100%;
          background: #fff;
        }
      }
    }
  }
}
</style>
