<template>
  <div class="print-perm">
    <module-view :propConfig="config" ref="view" v-loading="load" @pagination="pagination" @onRefresh="getList" @resert="queryList" @search="queryList" 
    @modalHandle="modalHandle" @clickHandle="clickHandle" @detail="detail"/>
  </div>
</template>
<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-property-decorator";
import {query,add,update,del} from '@/api/printPermission';
import permissionSelect from "./components/permissionSelect.vue";
@Component({components:{permissionSelect}})
export default class PrintPermission extends Vue {
  load=false;
  isSearch=false
  timeNum = 0;
  config:any={
    attr:{
      calculateH:true
    },
    search:{
      attr:{
        size:'small'
      },
      columns:[
        [{
          span:8,
          label:'外部用户标识',
          prop:'userName',
          component:'user-modal'
        },{
          span:8,
          label:'角色',
          prop:'role',
          component:'by-select',
          compConfig:{
            attr:{
              clearable:true,
              data:[{
                label:'超级管理员',
                value:'admin'
              },{
                label:'操作用户',
                value:'oper_user'
              },{
                label:'打印用户',
                value:'print_user'
              },{
                label:'自定义用户',
                value:'custom_user'
              }]
            }
          }
        }]
      ]
    },
    tool:{
      tools:{
        search:true,
        refresh:true,
        add:true,
        delete:true
      }
    },
    table:{
      attr:{
        size:'mini',
        seq:true,
        checkbox:true
      },
      columns:[{
        title:'外部用户标识',
        isDetail:true,
        field:'userName'
      },{
        title:'角色',
        field:'role',
        width:130,
      },{
        title:'自定义权限',
        field:'customPermission',
        width:130
      },{
        title:'自定义打印次数限制',
        field:'customLimit',
        width:130
      },{
        title:'创建时间',
        field:'createTime',
        width:140
      },{
        title:'操作',
        action:true,
        width:100,
        plugins:[{
          name:'删除',
          event:{
            click:(item:any) => {
            
            }
          }
        }]
      }]
    },
    modal:{
      tool:{
        tools:{
          return:true,
          add:true
        },
      },
      form:{
        attr:{
          size:'small',
          width:'800px',
          labelWidth:'150px',
          rules:{
            userName:[
              { required: true, message: '模版名称不能为空！', trigger: 'blur' }
            ],
            role:[
              {required: true, message: '角色不能为空！', trigger: 'change'}
            ],
            // customPermission:[
            //   {required: true, message: '自定义权限不能为空！', trigger: 'change'}
            // ],
            // customLimit:[
            //   {required: true, message: '自定义打印次数限制不能为空！', trigger: 'blur'}
            // ]
          }
        },
        columns:[
          [{
            label:'外部用户标识',
            prop:'userName',
            component:'user-modal'
          }],
          [{
            label:'角色',
            prop:'role',
            component:'by-select',
            compConfig:{
              attr:{
                clearable:true,
                data:[{
                  label:'超级管理员',
                  value:'admin'
                },{
                  label:'操作用户',
                  value:'oper_user'
                },{
                  label:'打印用户',
                  value:'print_user'
                },{
                  label:'自定义用户',
                  value:'custom_user'
                }]
              }
            }
          }],
          [{
            label:'自定义权限',
            prop:'customPermission',
            component:permissionSelect
          }],
          [{
            label:'自定义打印次数限制',
            prop:'customLimit',
            component:'by-input',
            compConfig:{
              attr:{
                clearable:true,
                type:'integer'
              }
            }
          }]
        ]
      }
    }
  }
  mounted(){
    this.$nextTick(()=>{
      this.getList()
    })
  }
  //分页
  pagination(){
    if(this.isSearch){
      this.queryList();
    }else{
      this.getList()
    }
  }
  //列表请求（只有分页，不包含搜素条件）
  getList(){
    if(!this.$refs.view){
      if(this.timeNum > 5){
        return
      }
      setTimeout(()=>{
        this.getList()
      },500) 
      this.timeNum ++;
      return
    }
    this.isSearch = false;
    let data = (this.$refs.view as any).getPage();
    delete data.total;
    this.requestList(data);
  }
  //列表请求（包含分页和搜素条件）
  queryList(){
    this.isSearch = true;
    let data = (this.$refs.view as any).getQuery();
    delete data.total;
    this.requestList(data);
  }
  requestList(data:any){
    this.load = true;
    query(data).then((res:any) => {
      this.load = false;
      (this.$refs.view as any).setTableValue(res.data.records);
      let page = {
        pageNo: res.data.current, //当前页
        pageSize: res.data.size, //每页条数
        total: res.data.total //总条数
      };
      (this.$refs.view as any).setPage(page)

    }).catch(()=>{
      this.load = false;
    })
  }
  modalHandle(n:string){
    if(n == 'onAdd'){
      (this.$refs.view as any).getFormValidate(this.onAdd)
    }

    if(n == 'onSave'){
      (this.$refs.view as any).getFormValidate(this.onSave)
    }
  }
  //新增
  onAdd(){
    let value = (this.$refs.view as any).getFormValue();
    if(value){
      this.load = true;
      add(value).then((res:any)=>{
        this.load = false;
        this.$message({
          message:res.msg,
          type:'success'
        });
        (this.$refs.view as any).closeModal();
        this.getList();
      }).catch(()=>{
        this.load = false;
      })
    }
  }
  //保存
  onSave(){
    let data = (this.$refs.view as any).getFormValue();
    this.load = true;
    update(data).then((res:any) => {
      this.$message({
        message:res.msg,
        type:'success'
      });
      this.load = false;
      (this.$refs.view as any).closeModal();
      this.pagination();
    }).catch(()=>{
      this.load = false;
    })
  }
  clickHandle(n:string){
    if(n == 'onAdd'){
      this.config.modal.tool.tools.add = true;
      delete this.config.modal.tool.tools.save;
      (this.$refs.view as any).initFormTool();
    }
    if(n == 'onDelete'){
      this.multipleDel();
    }
  }
  //多个删除
  multipleDel(){
    let data = (this.$refs.view as any).getSelectData();
    if(data.length == 0){
      this.$message('请选择需要删除的数据！')
      return
    }
    let ids:Array<any>=[];
    for(const item of data){
      ids.push(item.id);
    }
    this.$confirm('此操作将永久删除选中的数据, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      this.del(ids);
    }).catch(() => {});
  }
  //删除
  del(data:Array<any>){
    this.load = true;
    del({ids:data}).then(()=>{
      this.load = false;
      this.$message({
        message:'删除成功！',
        type:'success'
      })
      this.pagination();
    }).catch(()=>{
      this.load = false;
    })
  }
  //点击详情
  detail(){
    this.config.modal.tool.tools.save = true;
    delete this.config.modal.tool.tools.add;
    (this.$refs.view as any).initFormTool();
  }
}
</script>
<style lang="scss" scoped>
.print-perm{
  height: 100%;
  width: 100%;
  overflow-y: hidden;
}
</style>