<template>
  <div>
    <Card style="height: 95%">
    <template #title>
      <p><Icon type="md-contacts" />{{title}}</p>
      </template>
      <Row style="margin: 0px 0;" type="flex">
        <Col span="24"  order="2" style="text-align: right">
          <div style="float:right">
             <bc-table-ex :dataColumns.sync='dataColumns' :columnsShow.sync="columnsShow" firstColumn="selection"
                         :pageKey="this.$options.name"
                         layout ex></bc-table-ex>
            </div>

            <Poptip confirm transfer size="small"
                    placement="bottom"
                    width="180"
                    title="你确定要同步吗?已经同步过得不会覆盖!"
                    @on-ok="initFunction">
              <Button style="margin-right: 5px;" type="primary">同步初始化功能</Button>
            </Poptip>
          
            <Button style="margin-right: 5px;" @click="functionAdd" type="primary">添加</Button>
        </Col>
      </Row>
      <Table ref="tables" searchable search-place="top" :data="tableData" :columns="columnsShow" size="small"
             :loading="loading" @on-sort-change="sortChange" @on-selection-change="selectedChange" height="460"
             highlight-row @on-current-change="onCurrentChange" style="margin-top: 10px;" border>
        <template #actions slot-scope="{ row }">
          <Button type="primary" size="small" @click="functionView(row)" ghost style="margin-right: 5px">查看</Button>
          <Button type="primary" size="small" @click="functionEdit($event,row)" ghost style="margin-right: 5px">修改
          </Button>
          <Poptip confirm transfer size="small"
                  placement="bottom"
                  width="180"
                  title="你确定要删除吗?"
                  @on-ok="functionDelete(row)">
            <Button type="primary" size="small" ghost>删除</Button>
          </Poptip>
        </template>
      </Table>
      <bcPage ref="function-list-page" @reload-data="reloadData" :dataCount="dataCount" size="small" :pageSize.sync="pageSize" ></bcPage>
      <functionEdit ref="functionEdit"  :show.sync="showModal" :value="modalParams" @reload-data="reloadData"></functionEdit>
    </Card>
  </div>
</template>

<script>
import { getFunctionPage,getFunctionList,deleteFunction,initFunction } from '../../../../api/rbac/function-api'
import functionEdit from './function-modal.vue'
let $this;
export default {
  name: 'functionPage',
  components: {
    functionEdit,
  },
  props : {
    nowObj:Object,
    title:String
  },
  data () {
    return {
      importUrl: '',
      uploadFunction:false,
      searchForm:{},
      showModal: false,
      modalParams: {},
      nowFunction:{},
      dataCount:0,
      pageSize:10,
      columnsShow: [],
      dataColumns: [
              /*{ title: '所属菜单', key: 'menuId', sortable: true, show: true },*/
              /*{ title: '功能编码', key: 'code', sortable: true, show: true },*/
              { title: '类型', key: '',render: (h, params) => h('span', params.row.type==1?'按钮':'其他'), sortable: true, show: true },
              { title: '功能名称', key: 'name', sortable: true, show: true },
              { title: '是否默认授权', key: '',render: (h, params) => h('span', params.row.isDefault==1?'是':'否'), sortable: true, show: true },
              { title: '状态', key: '',render: (h, params) => h('span', params.row.state==1?'正常':'禁用'), sortable: true, show: true },
              { title: '备注', key: 'remark', sortable: true, show: true },
              { title: '操作', key: 'action', width: 200, align: 'center',slot:'actions', show: true }
      ],
      tableData: [],
      selectedData: [],
      loading: false,
      sortParams: [], //[{title: 姓名, orderStr: 降序, sortKey: name, order: -}]
      getList: getFunctionList,
      commonList: [
        /*{id: 1, label: '常用固化查询条件1', findItem: [{field: 'fieldName', judge: 'eq', value: 'fieldValue'}]},
        {id: 2, label: '常用固化查询条件2', findItem: [
                {field: 'fieldName1', judge: 'eq', value: 'fieldValue2'},
                {field: 'fieldName2', judge: 'eq', value: 'fieldValue2'},
                ]},*/

      ],//固化常用列表
      fieldList: [//用户可以自行选择查询的字段列表及配置
        /*{
          label: '姓名',
          field: 'name',
          flags: ['=', 'like', 'isNull', 'isNotNull'],
          fieldType: 'input'
        },s
        {
          label: '身份证',
          field: 'idCard',
          flags: ['=', 'like', 'isNull', 'isNotNull'],
          fieldType: 'input'
        },*/
      ]
    }
  },
  methods: {
    showUpload(){
      this.uploadFunction=true;
    },
    functionView (row) {
      this.showModal = true;
      this.modalParams = {title:'查看',flag:'view',bean:row};
    },
    functionAdd () {
      this.showModal = true;
      this.modalParams = {title: '添加', flag: 'add', bean: null};
    },
    functionEdit (e, row) {
      e.stopPropagation();
      this.showModal = true;
      this.modalParams = {title:'修改',flag:'edit',bean:row};
    },
    functionDelete (row) {
      deleteFunction(row.id).then( data => {
        this.$Message.success( "操作成功！");
        this.tableData.splice(row._index, 1);
      }).catch(err =>{
        this.$Message.error( "操作失败！");
        console.log(err);
      })
    },
        selectedChange(selection) {
      this.selectedData = selection;
    },
    sortChange(column) {
      this.$refs.customSort.sortChange(column);
    },
    exportData(params) {
      this.$refs.tables.exportCsv(params);
    },
    reloadData (pageSize,pageNo) {
      if (!this.nowObj.id)return false;
      //表格加载中效果
      this.loading = true;
      //处理表单查询参数及分页
      if (!pageNo) {
        this.dataCount = 0
      }
      if (!pageSize) {
        pageSize = this.pageSize;
      }
      let page_params = {'pageSize':pageSize,'pageNo':pageNo};
      let params = Object.assign({},page_params,this.searchForm); // 这样不会改变原对象
      params.menuId = this.nowObj.id;//将资源的id加入参数
      getFunctionPage(params).then(res => {
        if (res.data.result.totalSize) {
            this.dataCount = parseInt(res.data.result.totalSize)
        }
        if (res.data.result.datas && res.data.result.datas.length > 0) {
          this.tableData = res.data.result.datas;
          this.tableData[0]._highlight = true;
          this.nowFunction = this.tableData[0];
        } else {
            this.tableData = [];
        }
        this.loading = false;
      }).catch(err => {
        this.tableData = [];
        this.$Message.error('查询失败！')
        console.log(err)
      })
    },
    onCurrentChange (currentRow,oldCurrentRow) {
      this.nowFunction = currentRow;
    },
    initFunction() {
      initFunction(this.nowObj.id,false).then( data => {
        this.$Message.success( "操作成功！");
        this.reloadData();
      }).catch(err =>{
        this.$Message.error( "操作失败！");
        console.log(err);
      })
    }
  },
  mounted () {
    this.reloadData();
    $this = this;
  },
  watch:{
    nowObj:{
      immediate:true,
      handler:function(){
        this.reloadData();
      }
    },
  }
}
</script>

<style>

</style>
