<template>
  <div class="button">
    <el-form ref="form" :model="form" label-width="100px">
      <div class="flex-wrap fr-list">
        <el-form-item class="flex1" label="权限编码">
          <el-input size="small" clearable placeholder="请输入权限编码" v-model="form.auth_ident"></el-input>
        </el-form-item>
        <el-form-item class="flex1" label="图标">
          <el-select size="small" clearable v-model="form.show_icon" placeholder="请选择图标">
            <el-option v-for="(item,index) in icons" :key="index" :label="item.name" :value="item.value">
              <i :class="item.name"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="flex1" label="显示方式">
          <el-select size="small" clearable v-model="form.show_type" placeholder="请选择显示方式">
            <el-option label="名称" value="1"></el-option>
            <el-option label="图标" value="2"></el-option>
            <el-option label="名称和图标" value="3"></el-option>
          </el-select>
        </el-form-item>
      </div>
      <div class="flex-wrap fr-list">
        <el-form-item class="flex1" label="服务类型">
          <el-select size="small" clearable v-model="form.service_type" placeholder="请选择服务类型">
            <el-option label="查询" value="1"></el-option>
            <el-option label="新增" value="2"></el-option>
            <el-option label="修改" value="3"></el-option>
            <el-option label="删除" value="4"></el-option>
            <el-option label="其他" value="5"></el-option>
            <el-option label="数据项" value="6"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="flex1" label="关联数据项">
          <el-select size="small" clearable filterable v-model="form.relation_data" placeholder="请选择关联数据项">
            <el-option v-for="(item,index) in relationData" :key="index" :label="item.columnLabel" :value="item.columnName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="flex1" label="打开方式">
          <el-select size="small" clearable v-model="form.open_method" placeholder="请选择打开方式">
            <el-option label="平铺" value="1"></el-option>
            <el-option label="跳转" value="2"></el-option>
            <el-option label="弹框" value="3"></el-option>
          </el-select>
        </el-form-item>
      </div>
      <div class="flex-wrap fr-list">
        <el-form-item class="flex1" label="传递参数">
          <el-select size="small" multiple filterable collapse-tags clearable v-model="form.pass_parameters" placeholder="请选择传递参数">
            <el-option v-for="(item,index) in passData" :key="index" :label="item.columnLabel" :value="item.columnName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="flex1" label="跳转地址">
          <el-input size="small" clearable placeholder="请输入跳转地址" v-model="form.route_info"></el-input>
        </el-form-item>
        <el-form-item class="flex1" label="窗口标识码">
          <el-input size="small" clearable placeholder="请输入窗口标识码" v-model="form.window_ident"></el-input>
        </el-form-item>
      </div>
      <el-form-item class="flex1" label="权限描述">
        <el-input size="small" type="textarea" :rows="5" placeholder="请输入权限描述" v-model="form.auth_comment"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import pb from '@/layout/mixin/publicFunc'
export default {
  name: 'Button',
  mixins: [pb],
  props: {
    icons: {
      type: Array,
      default: () => []
    },
    authObj: {
      type: Object,
      default: () => { }
    },
    authType: {
      type: String,
      default: ''
    },
    authNum: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      form: {
        show_type: '',
        auth_ident: '',
        show_icon: '',
        service_type: '',
        relation_data: '',
        open_method: '',
        pass_parameters: '',
        route_info: '',
        window_ident: '',
        auth_comment: ''
      },
      relationData: [],
      passData: [],
      formObj: {}
    }
  },
  watch: {
    authType: {
      handler (val) {
        if (val == 'add') {
          this.clearForm()
        }
      },
      immediate: true
    },
    authObj: {
      handler (obj) {
        // console.log('dddd', obj)
        // this.relationData = []
        // this.passData = []
        this.formObj = _.cloneDeep(obj)
        if (!Array.isArray(obj.pass_parameters) && obj.pass_parameters) {
          obj.pass_parameters = obj.pass_parameters.split(',')
        }
        if (obj.up_auth_id) {
          this.getRelationData(obj.up_auth_id)
        }
        this.form = obj
      },
      immediate: true
    },
    authNum: {
      handler (val) {
        if (val) {
          this.getRelationData(val)
        }
      },
      immediate: true
    }
  },
  mounted () {
    this.getRelationData(this.authNum ?? this.form.up_auth_id)
  },
  methods: {
    //清空表单
    clearForm () {
      const data = this.form
      for (const key in data) {
        if (Object.hasOwnProperty.call(data, key)) {
          data[key] = null
        }
      }
    },
    async getRelationData (id = this.form.up_auth_id) {
      // this.form.pass_parameters = ''
      // this.form.relation_data = ''
      const { returnData } = await this.getQueryList(SERVICE_ID.getTableColumnId, { serviceOutPutId: id })
      this.relationData = _.cloneDeep(returnData)
      this.passData = _.cloneDeep(returnData)
    }
  }
}
</script>

<style lang="scss" scoped>
.button {
  .fr-list {
    .el-form-item {
      margin-right: 105px;
      &:last-child {
        margin-right: 0;
      }
    }
  }
  .el-select {
    width: 100%;
  }
}
</style>