import userDialog from "@base/views/user/user/dialog.vue";
import {getJsxBtn} from "@base/views/bd/setting/utils/index";
import {getBdEnv} from "@base/api/user";

let modules = {};
modules = {
  props: {
    currentMenuKind: Object
  },
  components: {
    userDialog
  },
  computed: {
    menuKindId() {
      return this.currentMenuKind.id;
    }
  },
  async mounted() {
    await this.initBdEnv();
    this.initTableList();
  },
  data() {
    let that = this;
    return {
      showDialog: true,
      activeName: 'second',
      value10: '',
      dataId: '',
      showEdit: false,
      formData: {
        nickName: null,
      },
      vxeOption: {},
      showUserDialog: false,
      showPositionDialog: false,
      showSaleOrgDialog: false,
      operateIndex: 0,
      showViewDialog: false,

      tableData: [],
      showUserRowDialog: false,
      showUserAddDialog: false,
      currentRow: null,
      isDev: true

    };
  },
  methods: {
    getFieldDisabled() {

    },
    searchEvent() {
      this.$refs['table-m1'].commitProxy('reload');
    },
    resetEvent() {
      this.formData = {};
      this.$refs['table-m1'].commitProxy('reload');
    },
    openEditDialog(id) {
      this.dataId = !id || typeof id == 'object' ? 0 : id;
      this.activeName = 'first';
      this.showViewDialog = true;
      this.$openEditView('showEdit');
    },
    initBdEnv() {
      return getBdEnv({
        success: res => {
          this.isDev = res.objx == "dev"
        }
      });
    },
    initTableList() {
      let that = this;
      let valMap = {
        0: this.$t1('否'),
        1: this.$t1('是')
      };
      let tableOption = {
        vue: this,
        tableRef: 'table-m1',
        tableName: 'user_fileLibrary-fileAuto-list-m1',
        config: {
          height: "auto"
        },
        columns: [
          {type: 'checkbox', width: 48, resizable: false, fixed: 'left'},
          {
            title: this.$t1('用户'),
            field: 'nickName',
            width: 350,
            slots: {
              default: "user"
            }
          },
          {
            title: this.$t1('编辑'),
            field: 'editAuth',
            width: 150,
            params: {disabled: !this.currentMenuKind.editAuth},
            slots: {
              default: "switch"
            }
          },
          {
            title: this.$t1('导出发布'),
            field: 'exportAuth',
            width: 150,
            params: {disabled: !this.currentMenuKind.exportAuth},
            slots: {
              default: "switch"
            }
          },
          ...(this.isDev ? [{
            title: this.$t1('可选导出发布类型'),
            field: 'selectTypeAuth',
            width: 200,
            params: {disabled: !this.currentMenuKind.selectTypeAuth},
            slots: {
              default: "switch"
            }
          }] : []),

          {
            title: this.$t1('数据表权限'),
            field: 'szTaMbAuth',
            width: 150,
            params: {disabled: !this.currentMenuKind.szTaMbAuth},
            slots: {
              default: "switch"
            }
          },
          {
            title: this.$t1('表单模板权限'),
            field: 'formAuth',
            width: 150,
            params: {disabled: !this.currentMenuKind.formAuth},
            slots: {
              default: "switch"
            }
          },
          {
            title: this.$t1('逻辑脚本权限'),
            field: 'formScriptAuth',
            width: 150,
            params: {disabled: !this.currentMenuKind.formScriptAuth},
            slots: {
              default: "switch"
            }
          },

          {
            width: 100,
            fixed: 'right',
            title: '',
            sortable: false,
            slots: {
              default: ({row, rowIndex, $table}) => {
                return getJsxBtn({
                  iconName: "el-icon-delete",
                  content: this.$t1('删除'),
                  onclick: () => {
                    this.tableData.splice(rowIndex, 1)
                  }
                });
              }
            }
          }
        ]
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        this.vxeOption = opts;
      });
      this.initData();
    },
    initData() {
      this.$http({
        aes: true,
        url: USER_PREFIX + '/menu_kind_auth/list',
        method: `post`,
        data: {
          menuKindId: this.menuKindId,
        },
        isLoading: true,
        modalStrictly: true,
        success: res1 => {
          let tableData = res1.objx || [];
          this.tableData = tableData;

        }
      });
    },
    confirmInsertUser(rows) {
      if (rows.length) {
        let row = rows[0];
        this.formData.userId = row.id;
        this.formData.nickName = row.nickName;
      }
    },
    dialogClose() {
      this.$emit('update:visiable', false)
    },
    deleteRows() {
      let checkRows = this.$refs['table-m1'].getCheckboxRecords(true);
      if (!checkRows.length) {
        this.$message({
          message: this.$t1('请选择需要删除的表单分类权限'),
          type: 'error',
          showClose: true,
          duration: 3000,
        });
        return;
      }
      let keys = checkRows.map(item => item._X_ROW_KEY)
      this.tableData = this.tableData.filter(item => !keys.includes(item._X_ROW_KEY))
    },
    openUserRowDialog(row, rowIndex) {
      this.operateIndex = rowIndex;
      this.showUserRowDialog = true;
    },
    confirmtUserRow(rows) {
      if (rows.length) {
        let row = rows[0];
        let item = this.tableData[this.operateIndex]
        item.userId = row.id;
        item.nickName = row.nickName;
      }
    },
    openUserAddDialog(row, rowIndex) {
      this.showUserAddDialog = true;
    },
    confirmtUserAddDialog(rows) {
      if (rows.length) {
        const tableData = this.tableData;
        const map = {};
        tableData.filter(item => !!item.userId).forEach(function (item) {
          map[item.userId] = 1;
        });
        let items = [];

        rows.forEach((row, index) => {
          let item = this.createRow();
          item.userId = row.id;
          item.nickName = row.nickName;
          if (!map[item.userId]) {
            items.push(item);
          }
        });
        this.tableData.push(...items);
      }
    },
    saveData() {
      this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
        var url = USER_PREFIX + `/menu_kind_auth/save`;
        this.$http({
          aes: true,
          url: url,
          method: `post`,
          data: {
            menuKindId: this.menuKindId,
            menuKindAuthDTOs: this.tableData
          },
          isLoading: true,
          success: res => {
            this.$message({
              message: res.content,
              type: 'success',
              duration: 500,
              onClose: t => {
                this.dialogClose()
              }
            });
          }
        });
      });
    },
    createRow() {
      let editAuth = this.currentMenuKind.editAuth ? 1 : 0;
      let exportAuth = this.currentMenuKind.exportAuth ? 1 : 0;

      let szTaMbAuth = this.currentMenuKind.szTaMbAuth ? 1 : 0;
      let formAuth = this.currentMenuKind.formAuth ? 1 : 0;
      let formScriptAuth = this.currentMenuKind.formScriptAuth ? 1 : 0;

      let row = {
        nickName: null,
        userId: null,
        editAuth,
        exportAuth,
        selectTypeAuth: 0,
        menuKindId: this.menuKindId,

        szTaMbAuth,
        formAuth,
        formScriptAuth
      }
      return row;
    }
  }
}
export default modules
