<template>
  <div class="app-container post-page">
    <div class="post-page-min" v-if="pageType == 1">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" >

        <el-form-item label="关键字" prop="searchValue">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="请输入职位编码"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="职位状态" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item style="float: right;">
          <el-col :span="1.5" style="margin-right:20px">
            <p class="add-button-one-120"
               @click="handleAdd"
               v-hasPermi="['system:post:add']"
            ><i class="el-icon-plus"></i>新增</p>
          </el-col>
        </el-form-item>
        <el-form-item>
          <p class="inquire-button-one" @click="handleQuery">查询</p>
          <p class="reset-button-one" @click="resetQuery">重置</p>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" border :data="postList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <!--<el-table-column label="职位编号" align="left" prop="postId" />-->
        <el-table-column label="职位编码" align="left" prop="postCode" />
        <el-table-column label="职位名称" align="left" prop="postName" />
        <el-table-column label="职位排序" align="left" prop="postSort" />
        <el-table-column label="状态" align="left" prop="status" :formatter="statusFormat" />
        <el-table-column label="创建时间" align="left" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="240">
          <template slot-scope="scope">
            <div class="button-box">
              <p class="table-min-button"
                 style="margin-right:10px;"
                 @click="handleAuthUser(scope.row)"
              >分配用户</p>
              <p class="table-min-button"
                 style="margin-right:10px;"
                 @click="handleUpdate(scope.row)"
                 v-hasPermiAnd="['system:post:query','system:post:edit']"
              >编辑</p>
              <p class="table-min-button"
                 @click="handleDelete(scope.row)"
                 v-hasPermi="['system:post:remove']"
              >删除</p>
            </div>
          </template>
          <!--<template slot-scope="scope">-->
            <!--<el-button-->
              <!--size="mini"-->
              <!--type="text"-->
              <!--icon="el-icon-edit"-->
              <!--@click="handleUpdate(scope.row)"-->
              <!--v-hasPermi="['system:post:edit']"-->
            <!--&gt;分配职位</el-button>-->
            <!--<el-button-->
              <!--size="mini"-->
              <!--type="text"-->
              <!--icon="el-icon-edit"-->
              <!--@click="handleUpdate(scope.row)"-->
              <!--v-hasPermi="['system:post:edit']"-->
            <!--&gt;修改</el-button>-->
            <!--<el-button-->
              <!--size="mini"-->
              <!--type="text"-->
              <!--icon="el-icon-delete"-->
              <!--@click="handleDelete(scope.row)"-->
              <!--v-hasPermi="['system:post:remove']"-->
            <!--&gt;删除</el-button>-->
          <!--</template>-->
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        layout="total, prev, pager, next, sizes, jumper"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改职位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="职位名称" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入职位名称" maxlength="10" />
        </el-form-item>
        <el-form-item label="职位编码" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入编码名称" maxlength="10" />
        </el-form-item>
        <el-form-item label="职位顺序" prop="postSort">
          <el-input-number v-model="form.postSort" controls-position="right" :min="0"  style="width: 380px"/>
        </el-form-item>
        <el-form-item label="职位状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!--分配用户-->
    <auth-user :postId="postId" v-if="pageType == 2"></auth-user>
  </div>
</template>

<script>
import { listPost, getPost, delPost, addPost, updatePost } from "@/api/system/post";
import authUser from "./authUser.vue";

export default {
  components: {
    authUser,
  },
  name: "Post",
  data() {
    return {
      isShow:true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 职位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        searchValue: null,
        postCode: undefined,
        postName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      pageType:1,
      postId:"",
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "职位名称不能为空", trigger: "blur" },
          { required: true, message: "职位名称不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
        postCode: [
          { required: true, message: "职位编码不能为空", trigger: "blur" },
          { required: true, message: "职位编码不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
        postSort: [
          { required: true, message: "职位顺序不能为空", trigger: "blur" },
          { required: true, message: "职位顺序不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    clickPage(type){
      if(this.pageType != type){
        this.pageType = type;
      }
    },
    /** 查询职位列表 */
    getList() {
      this.loading = true;
      listPost(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 职位状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.searchValue='';
      // this.resetForm("queryForm");
      this.$set(this,'queryParams',{
        pageNum: 1,
        pageSize: 10,
        searchValue:"",
        status:"",
      });
      this.handleQuery();
    },
    /** 分配用户操作 */
    handleAuthUser: function(row) {
      this.postId=row.postId;
      this.pageType=2;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加职位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postId = row.postId || this.ids
      getPost(postId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改职位";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.postId != undefined) {
            updatePost(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId || this.ids;
      this.$confirm('是否确认删除职位?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPost(postIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
  .post-page{
    display: flex!important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    padding:11px 20px 20px!important;
    .post-page-min{
      flex:1;
      display: flex!important;
      flex-direction: column;
      .button-box{
        width:340px;
        display: flex;
      }
    }
  }
</style>
