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

let modules = {};
modules = {
  props: {
    currentMenuKind: Object,
    sid:String
  },
  components: {
    userDialog
  },
  computed: {

  },
  async mounted() {
    // await this.initBdEnv();
    this.initTableList();
  },
  data() {
    let that = this;
    return {
      showDialog: true,
      activeName: 'second',
      value10: '',
      dataId: '',
      showEdit: false,
      formData: {},
      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');
    },
    initBdEnv() {
      return getBdEnv({
        success: res => {
          this.isDev = res.objx === "dev"
        }
      });
    },
    initTableList() {
      let that = this;
      let tableOption = {
        vue: this,
        tableRef: 'table-m1',
        tableName: 'user_form_template_auth-list-m1',
        config: {
          height: "auto"
        },
        columns: [
          {type: 'checkbox', width: 48, resizable: false, fixed: 'left'},
          {
            title: this.$t1('用户'),
            field: 'nickName',
            width: 250
          },
          {
            title: this.$t1('编辑'),
            field: 'editAuth',
            width: 150,
            slots: {
              default: "switch"
            }
          },
          {
            width: 47,
            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() {
      let sid = this.sid;
      this.$http({
        // aes: true,
        url: USER_PREFIX + '/form_template_auth/list',
        method: `post`,
        data: {
          sid,
        },
        isLoading: true,
        modalStrictly: true,
        success: res1 => {
          let rows = res1.objx || [];
          this.tableData = rows;
        }
      });
    },
    dialogClose() {
      this.$emit('update:visiable', false)
    },
    dialogConfirm(){
      this.$emit('confirm')
      this.dialogClose()
    },
    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 + `/form_template_auth/save`;
        this.$http({
          // aes: true,
          url: url,
          method: `post`,
          data: {
            sid:this.sid,
            formTemplateAuthDTOs: this.tableData
          },
          isLoading: true,
          success: res => {
            this.$message({
              message: res.content,
              type: 'success',
              duration: 500,
              onClose: t => {
                this.dialogConfirm()
              }
            });
          }
        });
      });
    },
    createRow() {
      let row = {
        nickName: null,
        userId: null,
        editAuth: 1
      }
      return row;
    }
  }
}
export default modules
