<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @change="resetSearch" clearable standout="bg-primary text-white" v-model="msg.PostName"
            label="岗位名称" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" standout="bg-primary text-white" v-model="msg.Status" :options="ShowOpts"
            emit-value map-options label="状态" />
        </div>
      </div>

    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat class="sticky-tow-column-table"
        separator="none" :data="data" :columns="columns" row-key="name">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">岗位信息</div>
          <q-space />
          <div class="page-option">
            <q-btn color="accent" size="sm" icon="add" label="新增岗位" @click="EditPost(null)" />
          </div>
        </template>
        <template v-slot:body-cell-Status="props">
          <q-td :props="props">
            <q-toggle size="sm" color="secondary" :false-value="1" :true-value="0" v-model="props.row.Status"
              title="注意：关闭后，分类将无法正常使用." @input="SetPostStatus(props.row)" />
          </q-td>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="pageCount"
            :input="true" @input="changePage" />
        </template>
        <template v-slot:body-cell-optioned="props">
          <q-td :props="props">
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="EditPost(props.row)" />
          </q-td>
        </template>
      </q-table>
      <post-form v-if="isShowPostForm" :save-obj="postObjOption" @close="closeDeptSaveForm" @success="refreshPage">
      </post-form>
    </div>
  </div>
</template>

<script>
  import {
    getPostPage,
    setPostStatusInfo,
  } from '../../api/system/post'
  import postForm from '../../components/system/post-form'
  export default {
    meta: {
      title: "岗位管理"
    },
    components: {
      postForm,
    },
    data() {
      return {
        currentUrl: "",
        columns: [{
            name: 'PostId',
            label: '岗位编号',
            field: 'PostId',
            align: 'left'
          },
          {
            name: 'PostName',
            label: '岗位名称',
            align: 'left',
            field: "PostName"
          },
          {
            name: 'UpdateByName',
            label: '操作人',
            field: 'UpdateByName',
            align: 'left'
          },
          {
            name: 'UpdateTimeStr',
            label: '创建时间',
            field: 'UpdateTimeStr',
            align: 'left'
          },
          {
            name: 'Status',
            label: '状态',
            align: 'left',
            field: 'Status'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'PostId'
          }
        ],
        data: [],
        loading: true,
        ShowOpts: [{
            label: '全部',
            value: '-1'
          },
          {
            label: '正常',
            value: '0'
          },
          {
            label: '删除',
            value: '1'
          }
        ],
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          PostName: "", //岗位名称
          PostId: 0, //岗位编号
          Status: "-1",
        },
        pageCount: 0,
        isShowPostForm: false,
        postObjOption: null,
      }
    },
    mounted() {
      this.currentUrl = this.$route.path;
      this.queryPostPage();
    },
    methods: {
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.queryPostPage();
      },
      //翻页
      changePage(val) {
        this.msg.pageIndex = val;
        this.queryPostPage();
      },
      //获取岗位分页列表
      queryPostPage() {
        this.loading = true;
        getPostPage(this.msg).then(res => {
          this.loading = false;
          this.data = res.Data.PageData;
          this.pageCount = res.Data.PageCount;
        }).catch(() => {
          this.loading = false;
        })
      },
      //刷新页面
      refreshPage() {
        if (!this.postObjOption) {
          this.msg.pageIndex = 1;
          this.msg.PostName = "";
          this.msg.PostId = 0;
          this.msg.Status = "-1";
        }
        this.queryPostPage();
      },
      //新增修改岗位
      EditPost(obj) {
        if (obj) {
          this.postObjOption = obj;
        } else {
          this.postObjOption = null;
        }
        this.isShowPostForm = true;
      },
      //关闭弹窗
      closeDeptSaveForm() {
        this.isShowPostForm = false;
      },
      //修改岗位状态
      SetPostStatus(obj) {
        var that = this;
        var delMsg = {
          PostId: obj.PostId,
          Status: obj.Status
        };
        var tipMsg = "";
        if (delMsg.Status == 0) {
          tipMsg = "是否启用【" + obj.PostName + "】岗位?";
        } else {
          tipMsg = "是否禁用【" + obj.PostName + "】岗位?";
        }
        this.$q.dialog({
          title: '提示信息',
          message: tipMsg,
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          setPostStatusInfo(delMsg).then(res => {
            if (res.Code == 1) {
              that.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功',
                position: 'top'
              })
              that.queryPostPage();
            }
          })
        }).onCancel(() => {
          obj.Status = obj.Status == 1 ? 0 : 1;
        });
      }
    }
  }

</script>
<style lang="sass">
  @import url('~assets/css/table.sass');

</style>
