import userDialog from "@base/views/user/user/dialog.vue";
import tableForm from "@base/components/table/tableForm.vue";
import authDialog from "@base/views/bd/setting/menu_kind/authDialog.vue";
import {tableTreeMixins} from "@base/mixins/tableTree";
import {getBdFlag} from "@base/api/user";
import {getJsxLink, getJsxStatus} from "@base/views/bd/setting/utils/index";

let modules = {};
modules = {
  name: 'menu_kind:list',
  components: {userDialog, tableForm, authDialog},
  mixins: [tableTreeMixins],
  mounted() {
    this.getBdFlag();
    this.initTableList();
  },
  data() {
    let that = this;
    return {
      value10: '',
      dataId: '',
      formData: {
        enabled: null
      },
      vxeOption: {},
      activeName: 'second',
      isBdFlag: true,
      showAuthDialog: false,
      currentMenuKind: {}
    };
  },
  methods: {
    searchEvent() {
      this.$refs['table-m1'].commitProxy('reload');
    },
    resetEvent() {
      this.formData = {};
      this.$refs['table-m1'].commitProxy('reload');
    },
    getSearchParam() {
      return {
        ...this.formData
      };
    },
    initTableList() {
      let that = this;
      let tableOption = {
        vue: this,
        tableRef: 'table-m1',
        tableName: 'bd_menu_kind_list-m1',
        path: USER_PREFIX + '/menuKind/getAllList',
        treeNodeUrl: USER_PREFIX + '/menuKind/getChildren',
        editSaveUrl: USER_PREFIX + '/menuKind/save',
        editDefaultRow: ({row}) => {
          let taPrefix = row?.taPrefix || null;
          return {taPrefix: taPrefix, enabled: true, orders: 99}
        },
        param: row => {
          return this.getSearchParam();
        },
        treeNodeParam: row => {
          return {
            parent: row.id,
            enabled: this.formData.enabled
          };
        },
        editValidate(row, done) {
          if (!row.name) {
            that.$message({
              type: "error",
              message: that.$t1("表单分类名称不能为空")
            })
          } else if (!row.taPrefix) {
            that.$message({
              type: "error",
              message: that.$t1("数据表前缀不能为空")
            })
          } else {
            done();
          }
        },
        columns: [
          {type: 'checkbox', width: 48, resizable: false, fixed: 'left'},
          {
            title: this.$t1('表单分类名称'),
            field: 'name',
            width: 250,
            fixed: 'left',
            treeNode: true,
            editRender: {name: 'input'},
            slots: {
              default: ({row}) => {
                return getJsxLink({
                  content: row.name,
                  onclick: (event) => {
                    event.stopPropagation();
                    this.$refs['table-m1'].toggleTreeExpand(row);
                  }
                });
              }
            }
          },
          {
            field: 'enabled',
            title: this.$t1('是否启用'),
            width: 150,
            editRender: {
              name: '$select',
              props: {clearable: true},
              events: {
                change: () => {
                  this.$forceUpdate();
                }
              },
              options: [
                {
                  label: this.$t1('启用'),
                  value: true
                },
                {
                  label: this.$t1('禁用'),
                  value: false
                }
              ]
            },
            slots: {
              default: ({row}) => {
                if (row.enabled) {
                  return getJsxStatus(null, this.$t1('启用'))
                } else {
                  return getJsxStatus('s-3', this.$t1('禁用'))
                }
              }
            }
          },
          {
            title: this.$t1('序号'),
            field: 'orders',
            width: 150,
            editRender: {name: 'input'},
            slots: {
              edit: "editOrders"
            }
          },
          {field: 'treePathName', title: this.$t1('上级分类'), width: 150},
          {field: 'serviceName', title: this.$t1('服务名称'), width: 150},
          {
            field: 'taPrefix',
            title: this.$t1('数据表前缀'),
            width: 150,
            editRender: {name: 'input'},
            slots: {edit: 'taPrefix'}
          },
          {field: 'menuKindCode', title: this.$t1('表单分类编码'), width: 150},
          {field: 'serviceName', title: this.$t1('服务名'), width: 150},
          {field: 'createDate', title: this.$t1('创建时间'), width: 150},
          {field: 'modifyDate', title: this.$t1('更新时间'), width: 150},
          {width: 300, title: '', fixed: 'right', sortable: false, slots: {default: 'operate'}}
        ],
        searchColumns: [
          {title: this.$t1('表单分类名称'), field: "name", type: "input", common: true},
          {
            title: this.$t1('是否启用'), field: "enabled", type: "select", common: true, itemOption: [
              {label: this.$t1('启用'), value: true},
              {label: this.$t1('禁用'), value: false}
            ]
          }
        ]
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        this.vxeOption = opts;
      });
    },
    jsonImport() {
      this.$jsonImport({
        saveUrl: USER_PREFIX + '/form_develop/importMenuKind',
        showResult: true,
        resultColumns: [
          {title: this.$t1('表单分类名称'), field: 'name', width: 250}
        ],
        callback: () => {
          this.searchEvent();
        }
      });
    },
    jsonExport() {
      this.$jsonExport({
        title: "表单分类",
        targetRef: "table-m1",
        url: USER_PREFIX + "/form_develop/exportMenuKind",
        abcEnabled: false
      })
    },
    getBdFlag() {
      getBdFlag({
        success: res => {
          this.isBdFlag = res.objx == 1
        }
      });
    },
    openAuthDialog({row, rowIndex}) {
      this.currentMenuKind = row;
      this.showAuthDialog = true;
    },
    confirmAuthDialog() {

    }
  }
};
export default modules
