<!-- 岸基成员 -->
<template>
  <div class="box-body">

    <div class="search-box">
      <div class="button-common">
        <el-button type="primary" @click="addman">添加成员</el-button>
        <el-button @click="del">批量删除成员</el-button>
      </div>

      <div class="search-common">
        <div class="search-info">
          <span>公司 </span>
          <el-select v-model="page.underCompId" @change="changeDept" clearable placeholder="请选择">
            <el-option v-for="item in companyList" :key="item.compId" :label="item.compName" :value="item.compId" />
          </el-select>
        </div>
        <div class="search-info">
          <span>部门 </span>
          <el-select v-model="page.deptId" @change="getList" clearable placeholder="请选择">
            <el-option v-for="item in deptList" :key="item.deptId" :label="item.deptName" :value="item.deptId" />
          </el-select>
        </div>
        <div class="search-info">
          <el-input class="search-keyword" v-model.trim="page.staffName" maxlength="50" placeholder="请输入成员中文名" clearable @clear="getClear" @keyup.enter.native="getList" />
          <svg-icon icon-class="searchIcon" class="searchIcon" @click="getList" />
        </div>
      </div>

    </div>

    <el-table height="58.5vh" stripe :data="shoreBasedMembersList" v-loading="loadingList" @cell-click="jumpDetail" @selection-change="handleSelectionChange" @cell-mouse-enter="mouseEnter">
      <el-table-column type="selection" width="30" align="center" />
      <el-table-column prop="roleId" label="序号" width="80" align="center">
        <template slot-scope="scope">
          {{(page.currentPage - 1)*page.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="staffName" label="中文名">
        <template slot-scope="scope">
          {{scope.row.staffName | empty }}
        </template>
      </el-table-column>
      <el-table-column prop="majorRole.roleName" label="职务" />
      <el-table-column prop="deptName" label="部门" />
      <el-table-column prop="underCompName" label="公司" />
      <el-table-column prop="staffMobilePhone" label="手机号" />
      <el-table-column prop="staffAccount" label="账号ID" />
      <el-table-column prop="unclick" label="操作" width="100" align="center">
        <yh-operaction :lable="lable" @skip="skp" />
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="page.currentPage" :limit.sync="page.pageSize" @pagination="getList" />

    <!-- 查看 -->
    <el-dialog v-loading="loadingSave" :title="deptTitle" :visible.sync="addCompanyFlag" width="880px" center :close-on-click-modal="false" @close="cancel">
      <div class="two-form">
        <el-form ref="addDeptInfo" :rules="addDeptInfoRules" :model="addDeptInfo">
          <el-form-item label="中文名">
            <el-input maxlength="50" v-model="addDeptInfo.staffName" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="部门名称">
            <el-input maxlength="50" v-model="addDeptInfo.deptName" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="公司/部门/职务">
            <el-input maxlength="50" v-model="deptShortName" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="a" class="aaa">
          </el-form-item>
          <el-form-item label="关联船舶">
            <span v-for="(item, index) in vesselList" :label="item" :key="index" class="dcheckcss">{{item.vesselName}}</span>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import shoreApi from "../../api/organization/shore";
import { staffPageByCondition, remove } from "../../api/shoreBasedMembers";
import yhOperaction from "../../layout/components/operaction.vue";
import Pagination from "../../components/Pagination";

import { getDeptListByCompId } from '../../api/shoreBasedMembers';


export default {
  name: "shoreBasedMembers",
  components: {
    yhOperaction,
    Pagination
  },
  data() {
    return {
      loadingList: false,
      addDeptInfo: {
        vesselBankFlag: "B",
        deptId: ""
      },
      addDeptInfoRules: {
        staffName: [{ required: true, trigger: "blur" }],
        deptName: [{ required: true, trigger: "blur" }],
        deptShortName: [{ required: true, trigger: "change" }]
      },
      page: {
        compId: localStorage.getItem("enterprise-front-companyId"),
        underCompId: "",
        deptId: "",
        enabled: "1",
        deleteFlag: "",
        vesselBankFlag: "B",
        currentPage: 1,
        pageSize: 10
      },
      shoreBasedMembersList: [],
      vesselList: [],
      deptList: [],
      companyList: [],
      deptTitle: "",
      addCompanyFlag: false,
      loadingSave: false,
      msgRow: "",
      name: "",
      total: 0,
      names: [],
      tabellist: {
        staffIds: []
      },
      deptShortName: ""
    };
  },
  computed: {
    lable() {
      return [
        {
          lab: "编辑",
          click: "edit"
        },
        {
          lab: "删除",
          click: "delete"
        }
      ];
    }
  },
  created() {
    this.getList();
    this.basicInfo();
  },
  methods: {
    jumpDetail(row, column) {
      if (column.property !== 'unclick' ) {
        this.$router.push({
          name: "shoreMemberDetail",
          query: { id: row.staffId }
        });
      }
    },
    basicInfo() {
      shoreApi.compAndChildrenInfo().then(res => {
        res[0].children.unshift({
          compId: res[0].compId,
          compName: res[0].compName
        });
        this.companyList = res[0].children;
      });
    },
    changeDept(id) {
      // 部门
      getDeptListByCompId({vesselBankFlag:'B',underCompId:id}).then(response=> {
        this.deptList = response;
      })
      this.getList()
    },
    getList() {
      this.loadingList = true;
      staffPageByCondition(this.page)
        .then(response => {
          this.total = response.total;
          this.shoreBasedMembersList = response.records;

          this.loadingList = false;
        })
        .catch(() => {});
    },
    // 批量删除
    del() {
      if (this.tabellist.staffIds.length === 0) {
        this.$message({
          message: "请先选择要删除的成员！",
          type: "warning",
          showClose: true
        });
        return false;
      }
      this.$confirm(
        "删除后，'" + this.names + "' 用户的所有数据将被清空!",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          remove(this.tabellist)
            .then(() => {
              this.$message({
                message: "删除成功",
                type: "success",
                showClose: true
              });
              this.getList();
            })
            .catch(() => {});
        })
        .catch(() => {});
    },
    mouseEnter(row, column) {
      this.msgRow = row;
    },
    handleSelectionChange(val) {
      this.tabellist.staffIds = [];
      this.names = [];
      for (let i = 0; i < val.length; i++) {
        this.tabellist.staffIds.push(val[i].staffId);
        this.names.push(val[i].staffName);
      }
    },
    // 操作下拉
    skp(data) {
      if (data === "edit") {
        this.getEdit(this.msgRow);
      } else if (data === "delete") {
        this.getDelete(this.msgRow);
      }
    },
    // 编辑
    getEdit(row) {
      this.$router.push({ name: "shoreMemberAdd", query: { id: row.staffId } });
    },
    // 删除
    getDelete(row) {
      this.name = row.staffName;
      this.tabellist.staffIds[0] = row.staffId;
      this.$confirm(
        "删除后，'" + this.name + "' 用户的所有数据将被清空！",
        "提示",
        {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          remove(this.tabellist)
            .then(response => {
              this.$message({
                type: "success",
                message: "删除成功"
              });
              this.getList();
            })
            .catch(err => {
              if (err === "MANAGEMENT_008") {
                this.$message({
                  message: "当前部门不允许被删除!",
                  type: "warning"
                });
              }
            });
        })
        .catch(() => {});
    },
    getClear() {
      this.page.staffName = null;
      this.getList();
    },
    addman() {
      this.$router.push({ name: "shoreMemberAdd" });
    },
    cancel() {
      this.addCompanyFlag = false;
      this.loadingSave = false;
      this.$refs.addDeptInfo.resetFields();
      this.addDeptInfo = {};
    },
  }
};
</script>

<style scoped lang="scss">
.inp {
  width: 200px;
}
.aaa {
  /deep/.el-form-item__label {
    color: #fff !important;
  }
}
</style>
