<template>
  <div>
    <div class="app-container" v-if="isList">
      <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" @submit.native.prevent>
        <el-row>
          <el-col :span="18">
            <el-form-item label="员工" prop="staffName">
              <el-input
                v-model="queryParams.params.staffOrStaffName"
                clearable
                @keyup.enter.native="btnSearch"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="float:right">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="btnSearch">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="btnResetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div style="float:right">
        <el-button type="primary" size="mini" @click="btnAddPower">新增</el-button>
      </div>
      <el-table size="mini" height="500px" v-loading="loading" :data="listData" @row-dblclick="btnDetails">
        <!-- <el-table-column label="员工编号" align="center" prop="staff" /> -->
        <el-table-column label="员工姓名" align="center" prop="staffName" />
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="btnDelPower(scope.row)"
            >删除</el-button>
            <el-button
              size="mini"
              type="text"
              @click="btnDetails(scope.row)"
            >详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="paginationClass">
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <detail v-model="isList" v-if="!isList" :enterState="page" :rowId="rowId" :ids="ids" @refresh="refresh" />
    <!-- 添加人员权限对话框 -->
    <el-dialog title="新增" :visible.sync="open" width="500px" append-to-body :show-close="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="员工" prop="staffName">
          <el-popover-select-v2 
            size="mini" 
            v-model="form.staffName" 
            title="员工" 
            valueKey="name"
            referName="CONTACTS_PARAM" 
            :dataMapping="{staff:'id',staffName: 'name'}"
            :source.sync="form"
          >
          </el-popover-select-v2>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="btnSubmitForm">确 定</el-button>
        <el-button size="mini" @click="btnCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import detail from './detail.vue'
import { listPower,addPower,delPower} from "@/api/business/spd/bo/power";
import ElPopoverSelectV2 from "@/components/popover-select-v2"
export default {
  name: "power",
  components: {detail,ElPopoverSelectV2},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      listData: [],
      // id集合
      ids: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params:{
          staffOrStaffName: null,
        },
      },
      //页面显示
      isList: true,
      //页面状态
      page: '',
      //详情id
      rowId: '',
      //新增的表单
      form:{},
      //
      open:false,
      //表单校验
      rules:{
        staffName: [
          { required: true, message: "员工必输", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询绩效列表 */
    getList() {
      this.loading = true;
      listPower(this.queryParams).then(response => {
        this.listData = response.rows;
        this.ids = response.rows.map(item => item.id);
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    btnSearch() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    btnResetQuery() {
      this.resetForm("queryForm");
      this.queryParams.params.staffOrStaffName = null;
      this.btnSearch();
    },
    /** 刷新 */
    refresh(){
      this.resetForm("queryForm");
      this.getList();
    },
    /** 进入详情 */
    btnDetails(row){
      this.rowId = row.id;
      this.page = 'see';
      this.isList = false;
    },
    //新增人员权限
    btnAddPower(){
      this.reset();
      this.open = true;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        staff: null,
        staffName: null,
        tenantId: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    // 取消按钮
    btnCancel() {
      this.open = false;
      this.reset();
    },
    //确定按钮
    btnSubmitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addPower(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    //删除
    btnDelPower(row){
      this.$modal.confirm('是否确认删除？').then(function () {
        return delPower(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>

<style lang="scss" scoped>
.btn_grooup {
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
}
.paginationClass {
  z-index: 500;
  position: fixed;
  bottom: 10px;
  right: 10px;
  width: 100%;
  line-height: var(--footer-height);
  color: #fff;
}
</style>
