import { param2Obj } from '@/utils'

export const commonMixin = {
    data() {
        return {
            // 修改时的数据源
            editParams: null,
        }
    },
    methods: {
        /** 新建 */
        handleAddData() {
            this.editParams = null
            this.dialogConfig.outerTitle = this.addDialogTitle
            this.dialogConfig.outerVisible = true
        },
        /** 删除 */
        handleDeleteData(id, method) {
            this.$modal.confirm('是否确定删除').then(() => {
                method(id).then(res => {
                  this.$modal.msgSuccess('操作成功！')
                  this.$refs.tableRef.getTableList()
                }).catch(err => {})
            })
        },
        /** 编辑 */
        handleEditData(data) {
            this.editParams = data
            this.dialogConfig.outerTitle = this.editDialogTitle
            this.dialogConfig.outerVisible = true
        },
        /** 导入数据 */
        handleImportData() {
            console.log('导入--')
        },
        /** 导出数据 */
        handleExportData(data, url, fileName, queryParams) {
          this.download(
            url,
            {
              ...queryParams,
              dataCondition: data
            },
            `${fileName}_${new Date().getTime()}.xlsx`,
          )
        },
        /** 关闭外侧弹框 */
        closeDialogOuter() {
            this.dialogConfig.outerVisible = false
        },
        /** 关闭内侧弹框 */
        closeDialogInner() {
            this.dialogConfig.innerVisible = false
        },

        /** 关闭弹框  由表单组件通知父组件关闭弹框的自定义事件 */
        closeDialog(type) {
            this.dialogConfig.outerVisible = false
            if(type) {
              this.$refs.tableRef.getTableList()
            }
        },
        /** 手机号脱密处理 */
        phoneCrypto(phoneNumber) {
          let reg = /^(1[3-9][0-9])\d{4}(\d{4}$)/
          let isMobile = reg.test(phoneNumber)
          if (isMobile) {
            return phoneNumber.replace(reg, "$1****$2")
          }
          return phoneNumber
        },
        /** 身份证号脱密处理 */
        idCardCrypto(idCard) {
          let reg = /^(.{6})(?:\d+)(.{4})$/
          return idCard.replace(reg, '\$1******\$2')
        }
    }
}
