<template>
  <div class="fullheight">
    <ht-table
      ref="htTable"
      :data="rows"
      :pageResult="pagination"
      :selection="true"
      quick-search-props="authorizeDesc,creator"
      :show-export="false"
      :show-custom-column="false"
      :default-sorter="[{ property: 'create_time_', direction: 'DESC' },]"
      @selection-change="handleSelectionChange"
      @loading="loadData"
    >
      <template v-slot:toolbar>
        <el-button-group>
          <el-button size="small" @click="showDialog()" icon="el-icon-plus">添加</el-button>

          <ht-delete-button
            url="${form}/formAuthorize/v1/deleteByIds?ids="
            :htTable="$refs.htTable"
            style="margin-left:10px;"
             :disabled="isDisabled"
            >删除</ht-delete-button
          >
        </el-button-group>
      </template>
      <template>
        <ht-table-column type="index" width="50" align="center" label="序号" />
        <ht-table-column
          prop="authorizeDesc"
          label="授权说明"
          :show-overflow-tooltip="true"
        >
        <template v-slot="{row}">
            <el-link
              type="primary"
              @click="showDialog(row.id)"
              title="查看详情"
            >{{row.authorizeDesc}}</el-link>
          </template>
        </ht-table-column>

        <ht-table-column
          prop="authorizeModel"
          label="授权对象"
          :filters="modelFilter"
          :show-overflow-tooltip="true"
        >
         <template v-slot="{row}">
            {{getModelsDesc(row.authorizeModel)}}
          </template>
        </ht-table-column>

        <ht-table-column
          prop="creator"
          label="创建人"
          :show-overflow-tooltip="true"
        >
        </ht-table-column>
        <ht-table-column
          prop="createTime"
          label="创建时间"
          :sortable="true"
          :show-overflow-tooltip="true"
        >
        </ht-table-column>

      </template>
    </ht-table>
    <FormAuthorize-manager-edit :modelRightType="modelRightType" :models="models" @onConfirm="$refs.htTable.load()" ref="FormAuthorizeManagerEdit"></FormAuthorize-manager-edit>
  </div>
</template>
<script>
import FormAuthorizeManagerEdit from "./FormAuthorizeManagerEdit.vue";
import api from "@/api/FormAuthorize.js"
export default {
  name: 'FormAuthorizeManager', 
  components:{FormAuthorizeManagerEdit},
  data() {
    return {
      dialogVisible: false,
      rows: [],
      pagination: {
        page: 1,
        pageSize: 50,
        total: 0
      },
      FormAuthorize: {},
      saveMethod: "POST",
      modelRightType:[],
      isDisabled: true,
      selectedList: [],
      models:[{key:'01',value:'建模',catId:'9',listUrl:"${form}/bo/def/v1/list",name:"description"},
        {key:'02',value:'pc表单',catId:'7',listUrl:"${form}/form/form/v1/list",alias:"formKey",querys:[{group: "defaultQueryGroup", operation: "EQUAL", relation: "AND", property: "formType", value: "pc"}]},
        {key:'03',value:'手机表单',catId:'7',listUrl:"${form}/form/form/v1/list",alias:"formKey",querys:[{group: "defaultQueryGroup", operation: "EQUAL", relation: "AND", property: "formType", value: "mobile"}]},
        {key:'04',value:'套打模板',catId:'13',listUrl:"${form}/form/printTemplate/v1/getPrintList?type=word"}, 
        {key:'05',value:'关联查询',catId:'16',listUrl:"${form}/form/customQuery/v1/list"},
        {key:'06',value:'对话框',catId:'17',listUrl:"${form}/form/customDialog/v1/list"},
        // {key:'7',value:'流水号',catId:'18',listUrl:"${portal}/sys/identity/v1/listJson"}, 这2模块后台代码不在表单，先不处理
        // {key:'8',value:'数据字典',typeKey:'DIC',alias:'key',listUrl:"${portal}/sys/dataDict/v1/getAllRootDicts"},
        // {key:'9',value:'标签',catId:'11'},
        {key:'10',value:'自定义组件',listUrl:"${form}/formCustomComponent/v1/query"},
        {key:'11',value:'数据列表',listUrl:"${form}/form/query/querySqldef/query"},
        {key:'12',value:'表单列表',catId:'7',listUrl:"${form}/form/dataTemplate/v1/query"}],
      modelsMap:{},
      modelFilter:[]
    };
  },
  computed: {
	tableHeight() {
	  return this.$baseTableHeight()
	},
  },
  watch: {
    selectedList: {
      handler(val) {
        this.isDisabled = val.length < 1 ? true : false
      },
      deep: true,
    },
  },
  methods: {
    getModelsDesc(modelsStr){
      if (!modelsStr) {
        return '';
      }
      let descArray = []
      let this_ = this
      modelsStr.split(',').forEach(m => {
        descArray.push(this_.modelsMap[m].value)
      });
      return descArray.join(',')
    },
    handleSelectionChange(selection){
      this.selectedList = selection
    },
    showDialog(id) {
      this.$refs.FormAuthorizeManagerEdit.showDialog(id);
    },
    loadData(param, cb) {
      param = param || {}
      param = JSON.parse(JSON.stringify(param))
      if (param.querys) {
        let newQuerys = []
        param.querys.forEach(q => {
          if (q.property == 'authorizeModel') {
            if (q.value.constructor == String) {
              q.value = [q.value]
            }
            q.value.forEach(v => {
              newQuerys.push({group: "authorizeModelGroup",operation: "LIKE",property: "authorizeModel",relation: "OR",value: v});
            });
          }else{
            newQuerys.push(q);
          }
        });
        param.querys = newQuerys
      }
      api.loadData(param||{}).then(
        (resp) => {
          let response = resp.data || resp
          this.rows = response.rows
          this.total = response.total
          this.pagination = {
            page: response.page,
            pageSize: response.pageSize,
            total: response.total
          }
        }
      )
      .finally(() => cb && cb());
    },
    removeValid() {
        if (this.$refs.htTable.selection.length == 0) {
          this.$message({ message: '请至少选择一条记录', type: 'warning' })
          return false
        }
        return true
      },
      handleRemove() {
        const ids = []
        this.$refs.htTable.selection.forEach((item) => {
          ids.push(item.id)
        })
        api.deleteByIds(ids.join(',')).then(() => {
          this.loadData()
        })
      },
  },
  created(){
    api.getModelRightType().then((resp) => {
      this.modelRightType = resp.data.value
    })
    this.models.forEach(mo => {
      this.modelsMap[mo.key] = mo
      this.modelFilter.push({text:mo.value,value:mo.key})
    });
  }
};
</script>
